3

我有这个 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
4

2 回答 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

至于您收到的错误,根据ChangeLog中添加的-nostdin选项ffmpeg 1.0,因此您需要将ffmpeg安装从更新0.1x1.0.x

于 2013-05-30T12:29:21.300 回答
1

我在 while 循环中遇到了同样的问题,这是因为我在-nostdin我的一个 ffmpeg 命令上丢失了标志。我认为是因为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标志,否则可能会发生一些意外的换行符延续。

于 2015-09-12T04:24:16.763 回答