regex Bash脚本或函数来重命名mp3文件

5rgfhyps  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(88)

想批量重命名mp3文件到以下方案my-artist_my-title.mp3
例如:假设我有mp3文件:of the song Underground‐Čoček, by Goran Bregović。但文件名为s3 24)3ü6 Dț67 .mp3(例如,因为它在驱动器和recovered with testdisk上丢失。假设mp3元数据正确,脚本应将文件名更改为:goran-bregovic_underground‐cocek.mp3
理想情况下,我希望有一个bash script,我会触发:

$ rename-mp3 directory-with-subdirectories-with-mp3-files

字符串
搜索类似的解决方案,我发现this函数。我修改了它,它的作品,* 部分 *!标记为#?的正则表达式修改不起作用。

function rename-mp3() {       # input is an mp3 file: i.e. find -name 's3  24)3ü6   Dț67  .mp3' -exec bash -c 'rename-mp3 "$@"' bash {} \;
                                # extract metadata from file
  artist=`ffprobe -loglevel error -show_entries format_tags=artist -of default=noprint_wrappers=1:nokey=1 "$1"`
  title=`ffprobe -loglevel error -show_entries format_tags=title -of default=noprint_wrappers=1:nokey=1 "$1"`
  artist="${artist/\&/}"        #? delete all non-numeral, non-latin, and non-space characters (&, %, !, ', ä, ö, ü, ă, ț, etc.)
  title="${title/\&/}"          #? delete all non-numeral, non-latin, and non-space characters (&, %, !, ', ä, ö, ü, ă, ț, etc.)
  artist="${artist// +/\-}"     #? replace all spaces with "-", if more then one in sequence substitute only one "-"
  title="${artist// +/\-}"      #? replace all spaces with "-", if more then one in sequence substitute only one "-"
  filename="$artist_$title.mp3" # paste artist and title together
  filename="${name,,}"          # lowercase everything
  echo "$filename"              # display new filename
  cp "$1" "renamed/$name"       # copy renamed file to the renamed folder
}

z9ju0rcb

z9ju0rcb1#

我使用一个类似的脚本(但实际上更复杂)来排序我所有的音频文件。
根据你的剧本:

#! /usr/bin/env bash

declare -r input_filename="$1"
declare -r output_dirname="$2"

function rationalize() {
    local s="$1"
    # extglob option is necessary for pattern matching in bash expansions
    shopt -s extglob
    # delete all non-ascii characters
    s="${s//[^[:ascii:]]/}"
    # keep only all alpha, numeral and space characters
    s="${s//[^[:alnum:]|[:space:]]/}"
    # replace all spaces with "-", if more then one in sequence substitute only one "-"
    s="${s//+([[:space:]])/-}"
    shopt -u extglob
    printf "%s" "$s"
}

function get_tag() {
    local -r tag="$1"
    local -r filename="$2"
    local value=$(ffprobe -loglevel error -show_entries format_tags="$tag" -of default=noprint_wrappers=1:nokey=1 "$filename")
    value=$(rationalize "$value")
    printf "%s" "$value"
}

function copy_audiofile() {
    local -r from_filname="$1"
    local -r to_dir="$2"
    # get filename extension
    local -r ext="${from_filname##*.}"
    # get artist for file
    local artist=$(get_tag "artist" "$from_filname")
    # get title for file
    local title=$( get_tag "title" "$from_filname")
    # paste artist and title together
    local filename_to="${artist}_$title.$ext"
    # lowercase everything
    filename_to="${filename_to,,}"
    # display new from_filname
    echo "Rename '$from_filname' to '$filename_to'"
    # copy renamed file to the renamed folder
    cp "$1" "$to_dir/$filename_to"
}

copy_audiofile "$input_filename" "$output_dirname"

字符串

相关问题