使用 bash 在 ffmpeg 中添加带有 space 的 mp3 元数据

Adding mp3 metadata with space in ffmpeg using bash

我有这个 bash 下载 youtube 视频然后使用 youtube-dl 和 ffmpeg 将其转换为 mp3 的脚本。

#!/bin/bash

ytlink=""
outputFileName=""
title=""
artist=""
album=""

while getopts l:o:t:r:b: flag; do
  case "${flag}" in
    l) ytlink="${OPTARG}";;
    o) outputFileName="${OPTARG}";;
    t) title="${OPTARG}";;
    r) artist="${OPTARG}";;
    b) album="${OPTARG}";;
  esac
done

youtube-dl "$ytlink" --add-metadata --extract-audio --audio-format mp3 --output "temp.%(ext)s"

tempFilename="temp.mp3"
outputFileName="$outputFileName.mp3"

args+=("-i" "$tempFilename" "-metadata" "title='$title'" "-metadata" "artist='$artist'" "-metadata" "album='$album'" "-metadata" "comment=Source:$ytlink")
ffmpeg -loglevel debug ${args[@]} -acodec copy "$outputFileName"
rm "$tempFilename"

这个剧本我一言以蔽之就可以了title/artist/album。但是,如果我有一个 space,ffmpeg 会将 space 之前的每个单词解释为另一个参数。这就是我在命令行上使用它的方式:

./yttomp3.sh -l "https://www.youtube.com/watch?v=KwQnSHAilOQ" -o "Lee - Autumn Day" -t "Autumn Day" -r "Lee" -b "(Free) Lo-fi Type Beat - Autumn Day"

ffmpeg的调试输出:

Splitting the commandline.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'.
Reading option '-i' ... matched as input url with argument 'temp.mp3'.
Reading option '-metadata' ... matched as option 'metadata' (add metadata) with argument 'title='Autumn'.
Reading option 'Day'' ... matched as output url.
Reading option '-metadata' ... matched as option 'metadata' (add metadata) with argument 'artist='Lee''.
Reading option '-metadata' ... matched as option 'metadata' (add metadata) with argument 'album='(Free)'.
Reading option 'Lo-fi' ... matched as output url.
Reading option 'Type' ... matched as output url.
Reading option 'Beat' ... matched as output url.
Reading option '-' ... matched as output url.
Reading option 'Autumn' ... matched as output url.
Reading option 'Day'' ... matched as output url.
Reading option '-metadata' ... matched as option 'metadata' (add metadata) with argument 'comment=Source:https://www.youtube.com/watch?v=KwQnSHAilOQ'.
Reading option '-acodec' ... matched as option 'acodec' (force audio codec ('copy' to copy stream)) with argument 'copy'.
Reading option 'Lee - Autumn Day.mp3' ... matched as output url.
Finished splitting the commandline.

我尝试将参数括在脚本的引号中,但它仍然不起作用。我该如何处理?谢谢。

您费尽心思构造一个数组并仔细引用所有字符串...然后您不引用该数组。

你想在 "${args[@]}"

周围加上双引号