使用 ffmpeg 将文件夹中的 mp3 文件对合并在一起

使用 ffmpeg 将文件夹中的 mp3 文件对合并在一起

我文件夹中有大量 mp3 文件,我想将每对(第 1 部分和第 2 部分)合并为一个 mp3 文件。所有文件都采用相同的格式。

我知道我可以对一对文件执行此操作:

ffmpeg -i "concat:01 Snow White Part 1.mp3|01 Snow White Part 2.mp3" -acodec copy "01 Snow White.mp3"

..但是我该如何对整个文件夹进行此操作?这是文件夹内容:

01 Snow White Part 1.mp3
01 Snow White Part 2.mp3
02 Jack and the Beanstalk Part 1.mp3
02 Jack and the Beanstalk Part 2.mp3
03 The Wizard of Oz Part 1.mp3
03 The Wizard of Oz Part 2.mp3
04 Thumbelina Part 1.mp3
04 Thumbelina Part 2.mp3
05 Puss in Boots Part 1.mp3
05 Puss in Boots Part 2.mp3
06 The Lions Glasses Part 1.mp3
06 The Lions Glasses Part 2.mp3
07 The Snow Queen Part 1.mp3
07 The Snow Queen Part 2.mp3
08 Alibaba and the Forty Thieves Part 1.mp3
08 Alibaba and the Forty Thieves Part 2.mp3
09 The Emperor's New Clothes Part 1.mp3
09 The Emperor's New Clothes Part 2.mp3
10 Little Red Riding Hood Part 1.mp3
10 Little Red Riding Hood Part 2.mp3

答案1

假设您在 Linux 上使用 bash,并且假设所有文件对都按您列出的方式编号,我将使用 for 循环遍历这些数字。

for c in {1..10}; do
  c=$(printf '%02.f' "$i")
  fnames=$(find . -maxdepth 1 -name "${i}*" | sort)
  parts=$(wc -l <<< "$fnames")
  if [ "$parts" -gt 1 ]; then 
    fnameout="${fnames%% Part 1*}.mp3"
    ffmpeg -i "concat: $f" -acodec copy "$fnameout"
  fi
done

如果有任何文件未配对,它们将被忽略。出于测试目的,我会echo在 ffmpeg 之前添加,然后制作最后一行,done > test.txt这样我就可以检查是否有错误或亮点。

我希望这有帮助。

答案2

这是我在 macOS/zsh 中所做的。我相信还有更简单的方法

PART1FILES=( )
for f in *1.mp3; do 
  PART1FILES+=($f)
done
PART2FILES=( )
for f in *2.mp3; do 
  PART2FILES+=($f)
done


INDEX=0
for i in $PART1FILES; do 
    FILE1=$i
    FILE2=$PART2FILES[INDEX+1]
    NEWFILE="${FILE1:0:-11}.mp3"
    ffmpeg -i "concat:$FILE1|$FILE2" -acodec copy "$NEWFILE"
    let INDEX=${INDEX}+1
done

相关内容