将目录中的所有 flv 文件转换为 mp3 的 Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10083498/
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
Bash Script to convert all flv file in a directory to mp3
提问by UnbrandedTech
This is my code so far.
到目前为止,这是我的代码。
#!/bin/bash
#James Kenaley
#Flv to Mp3 directory converter
find /home/downloads -iname "*.flv" | \
while read I;
do
`ffmpeg -i ${I} -acodec copy ${I/%.flv/.mp3}`
echo "$I has been converted"
done
but its picking up white spaces in the names of the flv files and throws a error saying its not in the directory. how do make it use the whole file name and not the just the first word before the space?
但它在 flv 文件的名称中拾取空格并引发错误,指出它不在目录中。如何使它使用整个文件名而不是空格前的第一个单词?
采纳答案by jaxxed
ffmpeg runs in forked threads, so simple batching can give weird behaviours. If you are running ffmpeg in the suggested batch loop, you should control your command and command-error output, so that it doesn't interfere.
ffmpeg 在分叉线程中运行,因此简单的批处理可能会产生奇怪的行为。如果您在建议的批处理循环中运行 ffmpeg,则应控制命令和命令错误输出,以免干扰。
If you run this and are getting every other item converted properly, but errors on the rest, try using this ffmpeg call in the loop:
如果您运行此程序并正确转换所有其他项目,但其余项目出现错误,请尝试在循环中使用此 ffmpeg 调用:
ffmpeg -y -i "${I}" -acodec mp3 -ar 22050 -f wav "${I/%.3gp/.mp3}" > /dev/null & 2> /dev/null
Notice the > dev/null & 2> /dev/null on the end. This pipes the command output, and command error output into oblivion. Then the script works.
注意最后的 > dev/null & 2> /dev/null 。这会将命令输出和命令错误输出传送到遗忘中。然后脚本工作。
One should note too that the program output will look strangely disorganized, with multiple files compressing at the same time. The results will be correct.
还应该注意的是,程序输出看起来会奇怪地杂乱无章,同时压缩多个文件。结果将是正确的。
[EDIT: NOTE THE -y THAT I HAVE, THIS MAKES FFMPEG OVERWRITE EXISTING MP3 FILES]
[编辑:注意我拥有的 -y,这会使 FFMPEG 覆盖现有的 MP3 文件]
回答by Amadan
Try this:
尝试这个:
`ffmpeg -i "${I}" -acodec copy "${I/%.flv/.mp3}"`
回答by Ignacio Vazquez-Abrams
Use quotes. And don't use backquotes.
使用引号。并且不要使用反引号。
ffmpeg -i "${I}" -acodec copy "${I%.flv}".mp3
回答by user unknown
Either call a short script, to do conversion and renaming in one pass:
调用一个简短的脚本,一次性完成转换和重命名:
adhoc.sh:
临时.sh:
$file=""
ffmpeg -i "$file" -acodec copy "${file/%.flv/.mp3}"
call it:
称它为:
find /home/downloads -iname "*.flv" -exec ./adhoc.sh {} ";" -ls
or convert:
或转换:
find /home/downloads -iname "*.flv" -exec ffmpeg -i {} -acodec copy {}.mp3 ";" -ls
and rename later:
并稍后重命名:
rename 's/.flv.mp3/.mp3/' /home/downloads/*.flv.mp3
Rename is part of a perl package which might need installation.
重命名是可能需要安装的 perl 包的一部分。

