Ubuntu安装FFmpeg编码出MP4文件的方法

http://wiki.ubuntu.org.cn/UbuntuSkills

Ubuntu安装FFmpeg编码出MP4文件的方法

2011-09-06 16:42 佚名 linux公社 字号: T |  T
一键收藏,随时查看,分享好友!

FFmpeg是Ubuntu下强大的视频、音频编解码工具。 x264可以编码出高质量的h.264视频,iPad必须要h.264编码或mpeg-4编码的视频才能正常播放。Ubuntu官方源默认是不提供h.264编码的,这就需要我们手工编译安装FFmpeg的h.264支持,使之能够编码出MP4文件。

AD:2013云计算架构师峰会课程资料下载

 

FFmpegUbuntu下强大的视频、音频编解码工具。 x264可以编码出高质量的h.264视频,iPad必须要h.264编码或mpeg-4编码的视频才能正常播放。

由于版权方面尚未确定,虽然全国很多网站都在使用h.264编码,但将来很有可能会被版权问题所困扰,所以Ubuntu官方源默认是不提供h.264编码的,这就需要我们手工编译安装FFmpeg的h.264支持,使之能够编码出MP4文件。

Ubuntu下安装、使用和升级FFmpeg的方法如下

1、安装支持包

  1. #libMP3lame  
  2.  
  3. sudo apt-get remove libMP3lame-dev  
  4.  
  5. sudo apt-get install nasm  
  6.  
  7. wget  
  8.  
  9. tar xzvf lame-3.98.4.tar.gz  
  10.  
  11. cd lame-3.98.4  
  12.  
  13. ./configure --enable-nasm --disable-shared  
  14.  
  15. make  
  16.  
  17. sudo checkinstall --pkgname=lame-ffmpeg --pkgversion="3.98.4" --backup=no --default --deldoc=yes 

#卸载现有的 x264、libx264-dev、FFmpeg

  1. sudo apt-get remove ffmpeg x264 libx264-dev 

#安装所有的 FFmpeg 和 x264 支持包

  1. sudo apt-get update  
  2.  
  3. sudo apt-get install build-essential subversion git-core checkinstall yasm texi2html libfaac-dev libMP3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libvorbis-dev libvpx-dev libx11-dev libxfixes-dev libxvidcore-dev zlib1g-dev 

2、安装x264

  1. cd  
  2.  
  3. git clone git://git.videolan.org/x264.git  
  4.  
  5. cd x264  
  6.  
  7. #./configure  
  8.  ./configure --enable-static --enabled-shared
  9. make  
  10.  
  11. sudo checkinstall --pkgname=x264 --pkgversion "2:0.`grep X264_BUILD x264.h -m1 | cut -d' ' -f3`.`git rev-list HEAD | wc -l`+git`git rev-list HEAD -n 1 | head -c 7`" --backup=no --deldoc=yes --fstrans=no --default 

3、安装FFmpeg

  1. cd  
  2.  
  3. svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg  
  4.  
  5. cd ffmpeg  
  6.  
  7. ./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid 
  8. #--enable-x11grab  
  9.  
  10. gcc使用正确的版本http://forums.debian.net/viewtopic.php?f=10&t=103908
  11. http://www.linuxquestions.org/questions/linux-software-2/managed-environment-ffmpeg-install-4175461262/
  12. make  
  13.  
  14. sudo checkinstall --pkgname=ffmpeg --pkgversion "4:SVN-r`LANG=C svn info | grep Revision | awk '{ print $NF }'`" --backup=no --deldoc=yes --fstrans=no --default  
  15.  
  16. hash x264 ffmpeg ffplay 

4、安装qt-faststart(可选)

这是个重要的x264再编码工具, 它的作用是使得视频不必等待下载完成即可开始播放。

使用方法:

  1. qt-faststart input.foo output.foo 

安装方法:

  1. cd ~/ffmpeg  
  2.  
  3. make tools/qt-faststart  
  4.  
  5. sudo checkinstall --pkgname=qt-faststart --pkgversion "4:SVN-r`LANG=C svn info | grep Revision | awk '{ print $NF }'`" --backup=no --deldoc=yes --fstrans=no --default install -D -m755 tools/qt-faststart /usr/local/bin/qt-faststart 

5、基本安装完成,你可以保留 ~/x264、~/ffmpeg目录以便将来升级时使用。

6、升级 FFmpeg 和 x264

  1. sudo apt-get remove ffmpeg x264 libx264-dev libvpx  
  2.  
  3. cd ~/x264  
  4.  
  5. make distclean  
  6.  
  7. git pull  
  8.  
  9. cd ~/ffmpeg  
  10.  
  11. make distclean  
  12.  
  13. svn update  
  14.  
  15. ./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libMP3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab  
  16.  
  17. make  
  18.  
  19. sudo checkinstall --pkgname=ffmpeg --pkgversion "4:SVN-r`LANG=C svn info | grep Revision | awk '{ print $NF }'`" --backup=no --deldoc=yes --fstrans=no --default  
  20.  
  21. hash x264 ffmpeg ffplay 

7、使用 FFmpeg and x264

容易的输出高质量视频的方法是使用FFmpeg中libx264的预设模式。你可以手工调整一些选项 (例如窗口大小: -s 640×480) 。 另外你可以参考 libx264 预设模式在线列表或再 /usr/local/share/ffmpeg 目录中查找帮助。

One-pass CRF (Constant Rate Factor) 使用预设模式:slow. One-pass CRF是经常使用的常规编码方式. 调整 -crf 可以改变输出质量. 数字越小输出质量越高同时输出尺寸越大. 可使用的数字范围是:18 ~ 28。

  1. ffmpeg -i input.avi -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre slow -crf 22 -threads 0 output.MP4 

Two-Pass encode 使用预设模式: fast. 想要精确设置目标编码率、文件大小的时候会使用到这个编码模式。

  1. ffmpeg -i input.avi -pass 1 -vcodec libx264 -vpre fast_firstpass -b 512k -bt 512k -threads 0 -f rawvideo -an -y /dev/null && ffmpeg -i input.avi -pass 2 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre fast -b 512k -bt 512k -threads 0 output.MP4 

iPod/iPad 640×480,使用预设值: slow 和 ipod640:

  1. ffmpeg -i input.avi -acodec libfaac -aq 100 -ac 2 -vcodec libx264 -vpre slow -vpre ipod640 -crf 26 -map_meta_data 0:0 -vf scale=640:-1 -threads 0 output.MP4 

总结:

欲知更多有关Ubuntu安装FFmpeg编码出MP4文件的方法,请点击Ubuntu如何转换mp4与x264和h.264

 

 

https://trac.ffmpeg.org/wiki/UbuntuCompilationGuide

 

Compile FFmpeg on Ubuntu, Debian, or Mint

 

 

This guide for supported releases of UbuntuDebian, and Linux Mint will provide a local install of the latest FFmpeg tools and libraries including several external encoding and decoding libraries (codecs). This will not provide a system installation and therefore will not interfere with anything such as repository packages. Recent static builds are also available for lazy people or those who are unable to compile.


Get the Dependencies

Copy and paste the whole code box for each step.

sudo apt-get update
sudo apt-get -y install autoconf automake build-essential git libass-dev libgpac-dev \
  libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libx11-dev \
  libxext-dev libxfixes-dev pkg-config texi2html zlib1g-dev
mkdir ~/ffmpeg_sources
  • Server users can omit the ffplay and x11 dependencies: libsdl1.2-dev libva-dev libvdpau-dev libx11-dev libxext-dev libxfixes-dev.
  • Ubuntu 10.04 Lucid users will need to install the git-core package instead of the git package. Lucid lacks the package libva-dev. This can be ignored.

Compilation & Installation

One advantage to compiling is that you can build ffmpeg to your liking. If you do not require certain encoders you may skip the relevant section and then remove the appropriate ./configure option in FFmpeg. For example, if libopus is not needed, then skip that section and then remove --enable-libopus from the Install FFmpeg section.

Yasm

Yasm is an assembler used by x264 and FFmpeg.

Ubuntu 13.10, Ubuntu 13.04, and Mint 15 users can alternatively install the yasm package from the repository instead of compiling.

cd ~/ffmpeg_sources
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar xzvf yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install
make distclean
. ~/.profile

x264

H.264 video encoder. See the x264 Encoding Guide for more information and examples.

cd ~/ffmpeg_sources
git clone --depth 1 git://git.videolan.org/x264.git
cd x264
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
make
make install
make distclean

Note: You can download the nightly  x264 source snapshot as an alternative to using git.

fdk-aac

AAC audio encoder. See the AAC Encoding Guide for more information and examples.

cd ~/ffmpeg_sources
git clone --depth 1 git://git.code.sf.net/p/opencore-amr/fdk-aac
cd fdk-aac
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
make distclean

Note: You can download a  fdk-aac source snapshot as an alternative to using git.

libmp3lame

MP3 audio encoder.

Most users can install the repository package:

sudo apt-get install libmp3lame-dev

Ubuntu 10.04 and Debian Squeeze users must compile:

sudo apt-get install nasm
cd ~/ffmpeg_sources
wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar xzvf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure --prefix="$HOME/ffmpeg_build" --enable-nasm --disable-shared
make
make install
make distclean

libopus

Opus audio decoder and encoder.

Ubuntu 13.10, Ubuntu 13.04, and Mint 15 users can alternatively install the libopus-dev package from the repository instead of compiling.

cd ~/ffmpeg_sources
wget http://downloads.xiph.org/releases/opus/opus-1.0.3.tar.gz
tar xzvf opus-1.0.3.tar.gz
cd opus-1.0.3
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
make distclean

libvpx

VP8/VP9 video encoder and decoder. See the vpx (WebM) Encoding Guide for more information and examples.

Ubuntu 13.10, Ubuntu 13.04, and Mint 15 users can alternatively install the libvpx-dev package from the repository instead of compiling.

cd ~/ffmpeg_sources
git clone --depth 1 http://git.chromium.org/webm/libvpx.git
cd libvpx
./configure --prefix="$HOME/ffmpeg_build" --disable-examples
make
make install
make clean

Note: You can download a  libvpx source snapshot as an alternative to using git.

ffmpeg

Note: Server users should remove --enable-x11grab from the following command:

cd ~/ffmpeg_sources
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
export PKG_CONFIG_PATH
./configure --prefix="$HOME/ffmpeg_build" \
  --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" \
  --bindir="$HOME/bin" --extra-libs="-ldl" --enable-gpl --enable-libass --enable-libfdk-aac \
  --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx \
  --enable-libx264 --enable-nonfree --enable-x11grab
make
make install
make distclean
hash -r

Note: You can download the nightly FFmpeg source snapshot as an alternative to using git.


Finish

  • Installation is now complete and ffmpeg is now ready for use. See the x264 Encoding Guide for examples.
  • If you do not see FFmpeg developers in your ffmpeg console output then something went wrong and you're probably using the  fake "ffmpeg" from the repository.
  • HTML formatted documentation is available in ~/ffmpeg_build/share/doc/ffmpeg.
  • You can keep the ffmpeg_sources directory if you plan on updating later. See Updating FFmpeg below for more details followed by instructions for reverting all changes made by this guide.

Updating FFmpeg

Development of FFmpeg is active and an occasional update can give you new features and bug fixes. First, remove (or move) the old files and then update the dependencies:

rm -rf ~/ffmpeg_build ~/bin/{ffmpeg,ffprobe,ffserver,vsyasm,x264,yasm,ytasm}
sudo apt-get update
sudo apt-get -y install autoconf automake build-essential git libass-dev libgpac-dev \
  libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libx11-dev \
  libxext-dev libxfixes-dev pkg-config texi2html zlib1g-dev

Server users can omit: libsdl1.2-dev libva-dev libvdpau-dev libx11-dev libxext-dev libxfixes-dev.

x264

cd ~/ffmpeg_sources/x264
make distclean
git pull

Now run ./configuremake, and make install as shown in the Install x264 section.

fdk-aac

cd ~/ffmpeg_sources/fdk-aac
make distclean
git pull

Now run ./configuremake, and make install as shown in the Install fdk-aac section.

libvpx

cd ~/ffmpeg_sources/libvpx
make clean
git pull

Now run ./configuremake, and make install as shown in the Install libvpx section.

ffmpeg

cd ~/ffmpeg_sources/ffmpeg
make distclean
git pull

Now run ./configuremake, and make install as shown in the Install FFmpeg section.


Reverting Changes Made by This Guide

To remove ffmpeg, x264, and dependencies installed for this guide:

rm -rf ~/ffmpeg_build ~/ffmpeg_sources ~/bin/{ffmpeg,ffprobe,ffserver,vsyasm,x264,yasm,ytasm}
sudo apt-get autoremove autoconf automake build-essential git libass-dev libgpac-dev \
  libmp3lame-dev libopus-dev libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev \
  libvorbis-dev libvpx-dev libx11-dev libxext-dev libxfixes-dev texi2html zlib1g-dev
hash -r

If You Need Help

Feel free to ask questions at the #ffmpeg IRC channel or the ffmpeg-user mailing list.


Also See

External Links

猜你喜欢

转载自wangxiaoxu.iteye.com/blog/1979373