用于 ffmpeg 转换的 bash 脚本不循环

标签 bash loops ffmpeg stdin

我有这个 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 ,您可以改用 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.1x 更新到 1.0.x

关于用于 ffmpeg 转换的 bash 脚本不循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16711050/

相关文章:

bash - echo 换行符输出

python - 使用Python中的while循环同时检索和检查值

algorithm - 从 n0 初始帧开始,每 N 帧采样一次视频

php ffmpeg 3gp水印不起作用

python - 我可以在存储视频之前使用 ffmpeg 转换从表单上传的 django 视频吗?

bash - ffmpeg 对文件夹中的每个 mp4 文件应用命令

bash - 在 Ubuntu 中创建快捷方式或脚本

linux - 从 tomcat 日志中提取 ORACLE 错误列表

windows - 在批处理脚本 (Windows) 中使用随机文件名创建 n 个文件 - 我的脚本只创建一个文件

r - 将循环拆分为子循环