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

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

理想情况下,我希望能够……

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

…并且我的所有video文件都是.mp4格式的容器MPEG-4和编解码器H.264。

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

你需要安装这些:

 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中你应该用“额外的”取代“未经剥离”,但由于存在过渡包,这也是有效的。

然后你可以使用一个脚本:

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

您可以使用这些选项来设置分辨率,video编解码器和音频编解码器以及音频质量:

 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 

适用于Ubuntu 14.04及更高版本

您将需要来自libav-tools和ubuntu-restricted-extras的avconv。 如果您没有使用命令,则可以安装它们:

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

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

 #!/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