Bash 脚本批量重命名 mp3? [已关闭]

问题描述 投票:0回答:1

我有一个 mp3 音乐库(具有正确的元数据),组织如下:

Music/{artists}/{album}/{title}

例如:

Music/Green Day/American Idiot/<songs in the album>

每个 mp3 的命名如下:

{title} {artists} {album}.mp3

例如:

Jesus of Suburbia Green Day American Idiot.mp3

保持相同的目录结构,我想重命名每个文件,使其标题如下:

{title} - {artists} - {album}.mp3

例如

Jesus of Suburbia Green Day American Idiot.mp3
-->
Jesus of Suburbia - Green Day - American Idiot.mp3

我希望有人知道可以做到这一点的脚本?

感谢您的帮助。

附注

有多位艺术家的歌曲的组织方式有所不同

例如,在音乐库中我们都有

Music/Fall Out Boy/Save Rock And Roll/<most of the songs in the album>

Music/Fall Out Boy,Elton John/Save Rock And Roll/Save Rock And Roll Fall Out Boy,Elton John Save Rock And Roll.mp3

这不是最好的例子,因为它可能会令人困惑,因为这首歌与专辑同名,这就是为什么它在 mp3 的名称中出现两次。

编辑: 根据您的搜索建议,我找到了一个命令:

ffprobe -loglevel error -show_entries format_tags=title,artist,album -of default=noprint_wrappers=1:nokey=1 file.mp3

可以提取我需要的元数据属性。我不确定如何应用它来重命名文件......

linux bash file-rename batch-rename
1个回答
0
投票

根据您的研究,您可以通过执行以下命令从 mp3 文件中获取标题/艺术家/专辑:

ffprobe -loglevel error -show_entries format_tags=title -of default=noprint_wrappers=1:nokey=1 mymp3.mp3

所以我们只需要创建一个函数来通过连接这些字符串来构建 mp3 名称:

function mp3name() {
  local title="$(ffprobe -loglevel error -show_entries format_tags=title -of default=noprint_wrappers=1:nokey=1 "$1")"
  local artist="$(ffprobe -loglevel error -show_entries format_tags=artist -of default=noprint_wrappers=1:nokey=1 "$1")"
  local album="$(ffprobe -loglevel error -show_entries format_tags=album -of default=noprint_wrappers=1:nokey=1 "$1")"
  mkdir -p "$artist/$album"
  local name="$artist/$album/$title - $artist - $album.mp3"
  echo "$name"
  mv "$1" "$name"
}

然后我们可以使用

find
和它的
exec
参数在 mp3 上执行上述函数

export -f mp3name
find -name '*.mp3' -exec bash -c 'mp3name "$@"' bash {} \;

示例 mp3 文件可以取自:https://file-examples.com/index.php/sample-audio-files/sample-mp3-download/

结果:

$ ls
'Impact Moderato - Kevin MacLeod - YouTube Audio Library.mp3'
© www.soinside.com 2019 - 2024. All rights reserved.