bash 循环执行“ffmpeg”命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21634088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute "ffmpeg" command in a loop
提问by Mohammad Torfehnezhad
I have three .wav
files in my folder and I want to convert them into .mp3
with ffmpeg.
我的.wav
文件夹中有三个文件,我想.mp3
用 ffmpeg将它们转换成。
I wrote this bash script, but when I execute it, only the first one is converted to mp3.
我写了这个 bash 脚本,但是当我执行它时,只有第一个被转换为 mp3。
what should I do to make script keep going through my files?
我应该怎么做才能让脚本继续浏览我的文件?
This is the script:
这是脚本:
#!/bin/bash
find ./ -name "*.wav" -print | while read f
do
name=${f:2:${#f}-6}
cmd='ffmpeg -i "$name.wav" -ab 320k -ac 2 "$name.mp3"'
eval $cmd
done
采纳答案by Reinstate Monica Please
回答by Shammel Lee
Use the -nostdin
flag in the ffmpeg command line:
-nostdin
在 ffmpeg 命令行中使用该标志:
ffmpeg -nostdin -i "$name.wav" -ab 320k -ac 2 "$name.mp3"
See the -stdin
/-nostdin
flags in the ffmpeg documentation ? https://ffmpeg.org/ffmpeg.html
看到ffmpeg 文档中的-stdin
/-nostdin
标志了吗?https://ffmpeg.org/ffmpeg.html
回答by chepner
If you do need find
(for looking in subdirectories or performing more advanced filtering), try this:
如果您确实需要find
(用于查找子目录或执行更高级的过滤),请尝试以下操作:
find ./ -name "*.wav" -exec ffmpeg -i "{}" -ab 320k -ac 2 '$(basename {} wav)'.mp3 \;
Piping the output of find
to the while
loop has two drawbacks:
将 的输出通过管道find
传送到while
循环有两个缺点:
- It fails in the (probably rare) situation where a matched filename contains a newline character.
ffmpeg
, for some reason unknown to me, will read from standard input, which interferes with theread
command. This is easy to fix, by simply redirecting standard input from/dev/null
, i.e.find ... | while read f; do ffmpeg ... < /dev/null; done
.
- 它在匹配文件名包含换行符的(可能很少见)情况下失败。
ffmpeg
,出于某种我不知道的原因,将从标准输入读取,这会干扰read
命令。这很容易解决,只需从 重定向标准输入/dev/null
,即find ... | while read f; do ffmpeg ... < /dev/null; done
。
In any case, don't store commands in variable names and evaluate them using eval
. It's dangerous and a bad habit to get into. Use a shell function if you really need to factor out the actual command line.
在任何情况下,不要将命令存储在变量名中并使用eval
. 进入它是危险的,也是一个坏习惯。如果您确实需要排除实际的命令行,请使用 shell 函数。