【问题标题】:How can I pipe the filename into an ffmpeg command?如何将文件名通过管道传输到 ffmpeg 命令中?
【发布时间】:2020-02-16 23:32:56
【问题描述】:

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

输入和输出可能相同(覆盖),但如果这不可能,是否有办法获取文件名并附加_converted

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

【问题讨论】:

    标签: bash ffmpeg terminal


    【解决方案1】:

    请您尝试一下,因为我没有 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
    

    【讨论】:

    • "$file_converted" 将查找名为 file_converted 的变量 -- 使用 "${file}_converted" 代替,或者更好的是 output_file="${file%.mp3}_converted.mp3" 将扩展保留在末尾。
    • @GordonDavisson,谢谢先生告知,我现在编辑了我的帖子并添加了您的建议代码,干杯。
    【解决方案2】:

    使用 xargs:

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

    见:man xargs

    【讨论】:

    • ffmpeg 从标准输入读取,所以如果xargs 在第一次运行ffmpeg 之前没有读取(/缓冲)整个文件列表,ffmpeg 可能会窃取部分文件列表。 xargs 的某些版本有一个 -o 选项可以解决这个问题,但不是全部。
    • @GordonDavisson:感谢您指出这一点。不幸的是,这里就是这种情况。我删除了选项-y(覆盖)并为转换后的文件添加了前缀。
    • 看起来输出也需要是不同的文件。结果都只有一秒钟。
    • @LeeProbert,我认为它对你没有用,所以删除它。现在已经撤消了,干杯。
    • @LeeProbert:您也可以将 RavinderSingh13 的答案用作 oneliner: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
    猜你喜欢
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多