如何将文件名通过管道传递给ffmpeg命令?

时间:2019-10-20 19:58:01

标签: bash ffmpeg terminal

我想在终端中运行以下命令: 文件夹中每个ffmpeg -i <input-file> -ac 2 -codec:a libmp3lame -b:a 48k -ar 16000 <output-file.mp3>文件上的mp3

输入和输出可能相同(覆盖),但是如果不可能,是否可以采用文件名并附加_converted呢?

我不是bash专家,但我知道我可能需要使用变量将ls命令的结果传递到ffmpeg命令中?

2 个答案:

答案 0 :(得分:2)

请问您可以尝试执行以下操作吗,因为我没有ffpmeg命令,因此无法对其进行测试。这样可以将输出保存到相同的Input_file本身,最好在测试文件夹中对其进行测试,并且对结果满意后可以在实际文件夹中运行。

for file in *.mp3
do
   ffmpeg -i "$file" -ac 2 -codec:a libmp3lame -b:a 48k -ar 16000 "temp" && mv "temp" "$file"
done

根据OP,您想将输出_converted字符串带入输出文件名,然后尝试执行以下操作。

for file in *.mp3
do
   output_file="${file}_converted"
   ffmpeg -i "$file" -ac 2 -codec:a libmp3lame -b:a 48k -ar 16000 "$output_file"
done

或按照@Gordon Davisson先生的评论使用。

for file in *.mp3
do
   output_file="${file%.mp3}_converted.mp3"
   ffmpeg -i "$file" -ac 2 -codec:a libmp3lame -b:a 48k -ar 16000 "$output_file"
done

答案 1 :(得分:1)

使用xargs:

printf "%s\0" * | xargs -0 -I {} ffmpeg -i {} -ac 2 -codec:a libmp3lame -b:a 48k -ar 16000 converted_{}

请参阅:man xargs