将任意文件格式的视频文件转换为 MPEG4/H.264?

将任意文件格式的视频文件转换为 MPEG4/H.264?

我想将大量各种格式的视频文件转换为.mp4文件(容器 MPEG-4,编解码器 H.264)。我想在 Ubuntu 机器上执行此操作,仅使用命令行工具,并且我愿意安装来自、main和的软件包。restricteduniversemultiverse

理想情况下,我希望能够做到......

for VIDEO_FILE in *; do
  some_conversion_program $VIDEO_FILE $VIDEO_FILE.mp4
done

...并将我的所有视频文件转换.mp4为容器 MPEG-4 和编解码器 H.264 的格式。

您将如何在 Ubuntu 机器上解决这个问题?我需要安装哪些软件包?

答案1

您需要安装这些:

sudo apt-get install ffmpeg libavcodec-unstripped-52 libavdevice-unstripped-52 libavformat-unstripped-52 libavutil-unstripped-50 libpostproc-unstripped-51 libswsclale-unstripped-0 x264

在 Karmic、Lucid 和 Maverick 中,您应该用“extra”替换“unstripped”,但由于存在过渡包,所以这种方法也有效。

然后您可以使用以下脚本:

for i in *.avi; do ffmpeg -i "$i" -f mp4 "`basename "$i" .avi`.mp4";done

您可以使用以下选项来设置分辨率、视频编解码器、音频编解码器和音频质量:

for i in *.avi; do ffmpeg -i "$i" -s 352x288 -vcodec libx264 -vpre default -acodec libmp3lame -ab 192k -ac 2 -ar 44100 -f mp4 "`basename "$i" .avi`.mp4";done

答案2

对于 Ubuntu 14.04 及更高版本

您将需要 libav-tools 和 ubuntu-restricted-extras 中的 avconv。如果您尚未安装它们,可以使用以下命令安装它们:

sudo apt-get install libav-tools ubuntu-restricted-extras

该脚本应该可以解决问题,但是假设该文件夹中只有视频文件,否则可能会发生意想不到的后果。

#!/bin/bash
echo "This script will attempt to encode by re-encoding the video stream and copying the audio stream placing all files in the current directory into a mp4 video container of the same name as the sources. The new filename will be derived from a basename (everything before the source file last '.') and an extension (everything after the source file last '.'). Target names will be 'basename'.mp4. Sources matching the target name will be renamed with a .bak extension prior to processing for safety. a CRF of 25 is harcoded in by preference but feel free to adjust as you desire."
echo 
echo "You must choose the preset of your choice with a tradeoff of speed vs. quality"
echo  "(veryfast recommended for decent speed and decent quality)"
echo "type  a preset and press enter or bail and enter to quit now. Preset choices are:"
echo "ultrafast superfast veryfast faster fast medium slow slower slowest"
read preset
echo "you chose $preset"
if [ "$preset" != "bail" ]
then
for f in *.* 
    do
    name=$(echo "$f" | sed 's/\.[^\.]*$//')
    ext=$(echo "$f" | sed 's/^.*\.//')
    target="$name.mp4"
    echo target = $target
        if  [ "$f" = "$target" ];
        then
            echo "$f=$target so moving source to backup file"
            mv "$f" "$f.bak";
            if [ "$?" != "0" ]; then echo "error renaming $f" && exit
            fi 
            avconv -i "$f.bak" -c:a copy -c:v libx264 -preset "$preset" -crf 25 "$target"
            if [ "$?" != "0" ]; then echo "error processing $f.bak" && exit
            fi 
        else 
            avconv -i "$f" -c:a copy -c:v libx264 -preset "$preset" -crf 25 "$target"
            if [ "$?" != "0" ]; then echo "error processing $f" && exit
            fi 
        fi
    done
fi

相关内容