我有这个 bash 脚本用于批量转换一些 mp4 文件:
#!/bin/bash
ls dr*.mp4 | grep -v -E "\.[^\.]+\." | sed "s/.mp4//g" | while read f
do
TARGET="$f.ffmpeg.mp4"
if ! [ -f $TARGET ]
then
echo $TARGET
ffmpeg -nostdin -i $f.mp4 -s 320x180 -vc h264 -acodec copy -f mp4 -y $TARGET
fi
TARGET="$f.ffmpeg.flv"
if ! [ -f $TARGET ]
then
echo $TARGET
ffmpeg -nostdin -i $f.mp4 -s 320x180 -acodec copy -y $TARGET
fi
TARGET="$f.jpg"
if ! [ -f $TARGET ]
then
echo $TARGET
ffmpeg -nostdin -i $f.ffmpeg.mp4 -ss 0 -vframes 1 -f image2 $TARGET
fi
TARGET="$f.ffmpeg.ogv"
if ! [ -f $TARGET ]
then
echo $TARGET
ffmpeg -nostdin -i $f.mp4 -s 320x176 -ar 11025 -acodec libvorbis -y $TARGET
fi
done
它运行一次但将输入文件名转换为 4 种不同的格式,但不会循环到下一个输入文件名。 我试图打乱各种转换的顺序,但脚本仍然只为一个文件名运行一次。 我尝试使用 -nostdin 标志运行 ffmpeg,但它说
"Unrecognized option 'nostdin'"
ffmpeg 版本是 ffmpeg 版本 0.10.6-6:0.10.6-0ubuntu0jon1~lucid2 - 我只是从 http://ppa.launchpad.net/jon-severinsson/ffmpeg/ubuntu 更新了 ffmpeg 包并且找不到更新的版本。基本系统是
Distributor ID: Ubuntu
Description: Ubuntu 10.04.1 LTS
Release: 10.04
Codename: lucid
最佳答案
Don't parse the Output of ls
,您可以改用 glob
bing。您还应该引用您的变量以说明文件名中可能存在的空格:
for input in dr*.mp4; do
output=${input%.mp4}.ffmpeg.mp4
[ -f "${output}" ] || ffmpeg -nostdin -i "${input}" -s 320x180 -vc h264 -acodec copy -f mp4 -y "${output}"
output=${input%.mp4}.ffmpeg.flv
[ -f "${output}" ] || ffmpeg -nostdin -i "${input}" -s 320x180 -acodec copy -y "${output}"
[...]
done
至于你得到的错误,根据ChangeLog -nostdin
选项已添加到 ffmpeg 1.0
中,因此您需要将 ffmpeg
安装从 0.1x
更新到 1.0.x
。
关于用于 ffmpeg 转换的 bash 脚本不循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16711050/