Bash脚本用于FFmpeg转换不循环。

3

我有一个用于批量转换mp4文件的bash脚本:

#!/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版本是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

1
ls dr*.mp4 | grep -v -E "\.[^\.]+\." | sed "s/.mp4//g" 的输出是什么? - slhck
2个回答

3
不要解析ls的输出,可以使用globbing代替。您还应该引用变量以考虑文件名中可能存在的空格:

(参考链接)

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

关于你得到的错误,根据变更日志-nostdin选项被添加到ffmpeg 1.0中,因此你需要将ffmpeg安装从0.1x升级到1.0.x


1

我遇到了一个与while循环有关的问题,原因是我在其中一个ffmpeg命令上缺少了-nostdin标志。我认为由于read从标准输入读取数据,因此在其中放置一个ffmpeg命令会吃掉一些数据。在我的情况下,我的while循环如下:

find /tmp/dir -name '*-video' | while read -r file; do
    # note: I forgot -nostdin on the ffmpeg command
    ffmpeg -i "$file" -filter:v "amazing_filtergraph" out.mp4
done

我会得到一个关于找不到tmp/dir/1-video的错误(请注意路径开头缺少了斜杠)。一旦我添加了 -nostdin,问题就得到解决。

还要注意,在您的while循环中,您基本上总是要使用-r标志,否则可能会出现意外的换行符延续。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接