centos6.5编译ffmpeg

来源:互联网 发布:新速特软件站增强版 编辑:程序博客网 时间:2024/06/08 18:24

源码包下载:http://download.csdn.net/detail/renhuaniang/8438125


 Compile FFmpeg on CentOS

Contents

  1. Get the Dependencies
  2. Compilation & Installation
  3. Additional Libraries
  4. Updating
  5. Reverting changes made by this guide
  6. If You Need Help
  7. Also See

This guide is based on a minimal installation of the latest CentOS release, and will provide a local, non-system installation of FFmpeg with support for several external encoding libraries. These instructions should also work for recent Red Hat Enterprise Linux (RHEL) and Fedora. This is a non-invasive guide and undoing all steps is simple and is shown at the end of this page.


Get the Dependencies

Note: The # indicates that the command should be executed as superuser or root.

Get the dependencies. These are required compiling, but you can remove them when you are done if you prefer (except make; it should be installed by default and many things depend on it).

# yum install autoconf automake gcc gcc-c++ git libtool make nasm pkgconfig zlib-devel

In your home directory make a new directory to put all of the source code into:

mkdir ~/ffmpeg_sources

Compilation & Installation

Note: 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 libvorbis is not needed, then skip that section and then remove --enable-libvorbis from the Install FFmpeg section.

Yasm

Yasm is an assembler used by x264 and FFmpeg.

cd ~/ffmpeg_sourcesgit clone --depth 1 git://github.com/yasm/yasm.gitcd yasmautoreconf -fiv./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"makemake installmake distclean

libx264

H.264 video encoder.

Requires ffmpeg to be configured with --enable-gpl --enable-libx264.

cd ~/ffmpeg_sourcesgit clone --depth 1 git://git.videolan.org/x264cd x264./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-staticmakemake installmake distclean

libfdk_aac

AAC audio encoder.

Requires ffmpeg to be configured with --enable-libfdk_aac (and --enable-nonfree if you also included --enable-gpl).

cd ~/ffmpeg_sourcesgit clone --depth 1 git://git.code.sf.net/p/opencore-amr/fdk-aaccd fdk-aacautoreconf -fiv./configure --prefix="$HOME/ffmpeg_build" --disable-sharedmakemake installmake distclean

libmp3lame

MP3 audio encoder.

Requires ffmpeg to be configured with --enable-libmp3lame.

cd ~/ffmpeg_sourcescurl -L -O http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gztar xzvf lame-3.99.5.tar.gzcd lame-3.99.5./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --disable-shared --enable-nasmmakemake installmake distclean

libopus

Opus audio decoder and encoder.

Requires ffmpeg to be configured with --enable-libopus.

cd ~/ffmpeg_sourcesgit clone git://git.opus-codec.org/opus.gitcd opusautoreconf -fiv./configure --prefix="$HOME/ffmpeg_build" --disable-sharedmakemake installmake distclean

libogg

Ogg bitstream library. Required by libtheora and libvorbis.

cd ~/ffmpeg_sourcescurl -O http://downloads.xiph.org/releases/ogg/libogg-1.3.2.tar.gztar xzvf libogg-1.3.2.tar.gzcd libogg-1.3.2./configure --prefix="$HOME/ffmpeg_build" --disable-sharedmakemake installmake distclean

libvorbis

Vorbis audio encoder. Requires libogg.

Requires ffmpeg to be configured with --enable-libvorbis.

cd ~/ffmpeg_sourcescurl -O http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.4.tar.gztar xzvf libvorbis-1.3.4.tar.gzcd libvorbis-1.3.4./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-sharedmakemake installmake distclean

libvpx

VP8/VP9 video encoder.

Requires ffmpeg to be configured with --enable-libvpx.

cd ~/ffmpeg_sourcesgit clone --depth 1 https://chromium.googlesource.com/webm/libvpx.gitcd libvpx./configure --prefix="$HOME/ffmpeg_build" --disable-examplesmakemake installmake clean

FFmpeg

cd ~/ffmpeg_sourcesgit clone --depth 1 git://source.ffmpeg.org/ffmpegcd ffmpegPKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --enable-gpl --enable-nonfree --enable-libfdk_aac --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264makemake installmake distcleanhash -r

Compilation is now complete and ffmpeg (also ffprobeffserverlame, and x264) should now be ready to use. The rest of this guide shows how to update or remove FFmpeg.

Tip: Keep the ffmpeg_sources directory and all contents if you intend to update as shown below. Otherwise you can delete this directory.


Additional Libraries

Other external libraries that you may require (but probably do not). These must be installed before you compile ffmpeg.

libfreetype

Font rendering library. Required for the drawtext video filter.

Requires ffmpeg to be configured with --enable-libfreetype.

# yum install freetype-devel

libtheora

Theora video encoder. Requires libogg. A more modern alternative is VP8 or VP9 (see libvpx section).

Requires ffmpeg to be configured with --enable-libtheora

cd ~/ffmpeg_sourcescurl -O http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.gztar xzvf libtheora-1.1.1.tar.gzcd libtheora-1.1.1./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-examples --disable-shared --disable-sdltest --disable-vorbistestmakemake installmake distclean

Updating

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

rm -rf ~/ffmpeg_build ~/bin/{ffmpeg,ffprobe,ffserver,lame,vsyasm,x264,yasm,ytasm}# yum install autoconf automake gcc gcc-c++ git libtool make nasm pkgconfig zlib-devel

Update x264

cd ~/ffmpeg_sources/x264make distcleangit pull

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

Update libfdk_aac

cd ~/ffmpeg_sources/libfdk_aacmake distcleangit pull

Then run ./configuremake, and make install as shown in the Install libfdk_aac section.

Update libvpx

cd ~/ffmpeg_sources/libvpxmake cleangit pull

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

Update FFmpeg

cd ~/ffmpeg_sources/ffmpegmake distcleangit pull

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


Reverting changes made by this guide

rm -rf ~/ffmpeg_build ~/ffmpeg_sources ~/bin/{ffmpeg,ffprobe,ffserver,lame,vsyasm,x264,yasm,ytasm}# yum erase autoconf automake gcc gcc-c++ git libtool nasm pkgconfig zlib-develhash -r

If You Need Help

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


Also See

  • H.264 Video Encoding Guide
  • AAC Audio Encoding Guide
Last modified 3 months ago


FFmpeg and H.264 Encoding Guide

Contents

  1. Constant Rate Factor (CRF)
  2. Two-Pass
  3. Lossless H.264
  4. Overwriting default preset settings
  5. Additional Information & Tips
  6. FAQ
  7. Additional Resources

The goal of this guide is to inform new users how to create a high-quality H.264 video using the encoder x264.

There are two rate control modes that are usually suggested for general use: Constant Rate Factor (CRF) or Two-Pass ABR. The rate control is a method that will decide how many bits will be used for each frame. This will determine the file size and also how quality is distributed.

If you need help compiling and installing libx264 see one of our FFmpeg and x264 compiling guides.

Constant Rate Factor (CRF)

This method allows the encoder to attempt to achieve a certain output quality for the whole file when output file size is of less importance. This provides maximum compression efficiency with a single pass. Each frame gets the bitrate it needs to keep the requested quality level. The downside is that you can't tell it to get a specific filesize or not go over a specific size or bitrate.

1. Choose a CRF value

The range of the quantizer scale is 0-51: where 0 is lossless, 23 is default, and 51 is worst possible. A lower value is a higher quality and a subjectively sane range is 18-28. Consider 18 to be visually lossless or nearly so: it should look the same or nearly the same as the input but it isn't technically lossless.

The range is exponential, so increasing the CRF value +6 is roughly half the bitrate while -6 is roughly twice the bitrate. General usage is to choose the highest CRF value that still provides an acceptable quality. If the output looks good, then try a higher value and if it looks bad then choose a lower value.

Note: The CRF quantizer scale mentioned on this page only applies to 8-bit x264 (10-bit x264 quantizer scale is 0-63). You can see what you are using by referring to theffmpeg console output during encoding (yuv420p or similar for 8-bit, and yuv420p10le or similar for 10-bit). 8-bit is more common among distributors.

2. Choose a preset

A preset is a collection of options that will provide a certain encoding speed to compression ratio. A slower preset will provide better compression (compression is quality per filesize). This means that, for example, if you target a certain file size or constant bit rate, you will achieve better quality with a slower preset. Similarly, for constant quality encoding, you will simply save bitrate by choosing a slower preset.

The general guideline is to use the slowest preset that you have patience for. Current presets in descending order of speed are: ultrafast,superfastveryfastfaster,fastmediumslowslowerveryslowplacebo. The default preset is medium. Ignore placebo as it is not useful (see FAQ). You can see a list of current presets with -preset help (see example below), and what settings they apply with x264 --fullhelp.

You can optionally use -tune to change settings based upon the specifics of your input. Current tunings include: filmanimationgrainstillimagepsnrssim,fastdecodezerolatency. For example, if your input is animation then use the animation tuning, or if you want to preserve grain then use the grain tuning. If you are unsure of what to use or your input does not match any of tunings then omit the -tune option. You can see a list of current tunings with -tune help, and what settings they apply withx264 --fullhelp.

Another optional setting is -profile:v which will limit the output to a specific H.264 profile. This can generally be omitted unless the target device only supports a certain profile (see Compatibility). Current profiles include: baselinemainhighhigh10high422high444. Note that usage of -profile:v is incompatible with lossless encoding.

To list all possible internal preset and tunes:

ffmpeg -f lavfi -i nullsrc -c:v libx264 -preset help -f mp4 -

Note: Windows users may need to use NUL instead of - as the output.

3. Use your settings

Once you've chosen your settings apply them for the rest of your videos if you are encoding more. This will ensure that they will all have similar quality.

CRF Example

ffmpeg -i input -c:v libx264 -preset slow -crf 22 -c:a copy output.mkv

Note that in this example the audio stream of the input file is simply stream copied over to the output and not re-encoded.


Two-Pass

This method is generally used if you are targeting a specific output file size and output quality from frame to frame is of less importance. This is best explained with an example. Your video is 10 minutes (600 seconds) long and an output of 50 MB is desired. Since bitrate = file size / duration:

(50 MB * 8192 [converts MB to kilobits]) / 600 seconds = ~683 kilobits/s total bitrate683k - 128k (desired audio bitrate) = 555k video bitrate

Two-Pass Example

ffmpeg -y -i input -c:v libx264 -preset medium -b:v 555k -pass 1 -c:a libfdk_aac -b:a 128k -f mp4 /dev/null && \ffmpeg -i input -c:v libx264 -preset medium -b:v 555k -pass 2 -c:a libfdk_aac -b:a 128k output.mp4

Note: Windows users should use NUL instead of /dev/null.

As with CRF, choose the slowest preset you can tolerate.

In pass 1 specify a output format (with "-f") that matches the output format in pass 2. Also in pass 1, specify the audio codec used in pass 2; in many cases "-an" in pass 1 will not work.

See Making a high quality MPEG-4 ("DivX") rip of a DVD movie. It is an MEncoder guide, but it will give you an insight about how important it is to use two-pass when you want to efficiently use every bit when you're constrained with storage space.


Lossless H.264

You can use -qp 0 or -crf 0 to encode a lossless output. Use of -qp is recommended over -crf for lossless because 8-bit and 10-bit x264 use different -crf values for lossless. Two useful presets for this are ultrafast or veryslow since either a fast encoding speed or best compression are usually the most important factors. Most non-FFmpeg based players will not be able to decode lossless (but YouTube can), so if compatibility is an issue you should not use lossless.

Lossless Example (fastest encoding)

ffmpeg -i input -c:v libx264 -preset ultrafast -qp 0 output.mkv

Lossless Example (best compression)

ffmpeg -i input -c:v libx264 -preset veryslow -qp 0 output.mkv

Overwriting default preset settings

You can overwrite default preset settings with the x264opts option, the x264-params option, or by using the libx264 private options (see ffmpeg -h encoder=libx264). This is not recommended unless you know what you are doing. The presets were created by the x264 developers and tweaking values to get a better output is usually a waste of time.

Example:

ffmpeg -i input -c:v libx264 -preset slow -crf 22 -x264opts keyint=123:min-keyint=20 -c:a copy output.mkv

Additional Information & Tips

ABR (Average Bit Rate)

ffmpeg -i input -c:v libx264 -b:v 1000k output.mp4

This provides something of a "running average" target, with the end goal that the final file match this number "overall on average" (so basically, if it gets a lot of black frames, which cost very little, it will encode them with less than the requested bitrate, but then the next few seconds of (non-black) frames it will encode at very high quality, to bring the average back in line). Using 2-pass can help this method to be more effective. You can also use this in combination with a "max bit rate" setting in order to prevent some of the swings.

CBR (Constant Bit Rate)

There is no native CBR mode, but you can "simulate" a constant bit rate setting by tuning the parameters of ABR:

ffmpeg -i input -c:v libx264 -b:v 4000k -minrate 4000k -maxrate 4000k -bufsize 1835k out.m2v

In the above example, -bufsize is the "rate control buffer" so it will enforce your requested "average" (4000k in this case) across each 1835k worth of video. So basically it is assumed that the receiver/end player will buffer that much data so it's ok to fluctuate within that much.

Of course, if it's all just empty/black frames then it will still serve less than that many bits/s but it will raise the quality level as much as it can, up to the crf level.

CRF with maximum bit rate

You can also also use a crf with a maximum bit rate by specifying both crf *and* maxrate settings, like

ffmpeg -i input -c:v libx264 -crf 20 -maxrate 400k -bufsize 1835k output.mp4

This will effectively "target" crf 20, but if the output exceeds 400kb/s, it will degrade to something less than crf 20 in that case.

Low Latency

libx264 offers a -tune zerolatency option. See the StreamingGuide.

Compatibility

All devices

If you want your videos to have highest compatibility with target devices (older iOS versions or all Android devices):

-profile:v baseline -level 3.0

This disables some advanced features but provides for better compatibility. Typically you may not need this setting (and therefore avoid using -profile:v and -level), but if you do use this setting it may increase the bit rate quite a bit compared to what is needed to achieve the same quality in higher profiles.

iOS

iOS Compatability (source)ProfileLevelDevicesOptionsBaseline3.0All devices-profile:v baseline -level 3.0Baseline3.1iPhone 3G and later, iPod touch 2nd generation and later-profile:v baseline -level 3.1Main3.1iPad (all versions), Apple TV 2 and later, iPhone 4 and later-profile:v main -level 3.1Main4.0Apple TV 3 and later, iPad 2 and later, iPhone 4s and later-profile:v main -level 4.0High4.0Apple TV 3 and later, iPad 2 and later, iPhone 4s and later-profile:v high -level 4.0High4.1iPad 2 and later, iPhone 4s and later, iPhone 5c and later-profile:v high -level 4.1High4.2iPad Air and later, iPhone 5s and later-profile:v high -level 4.2
  • This table does not include any additional restrictions which may be required by your device.
  • -level may not set the correct number of reference frames (-refs) if you're using an older build. See ticket #3307.

Apple Quicktime

Apple QuickTime only supports YUV planar color space with 4:2:0 chroma subsampling (use -pix_fmt yuv420p) for H.264 video. For information on additional restrictions seeQuickTime-compatible Encoding.

Blu-ray

See Authoring a professional Blu-ray Disc with x264.

Pre-testing your settings

Encode a random section instead of the whole video with the -ss and -t options to quickly get a general idea of what the output will look like.

  • -ss: Offset time from beginning. Value can be in seconds or HH:MM:SS format.
  • -t: Output duration. Value can be in seconds or HH:MM:SS format.

faststart for web video

You can add -movflags +faststart as an output option if your videos are going to be viewed in a browser. This will move some information to the beginning of your file and allow the video to begin playing before it is completely downloaded by the viewer. It is not required if you are going to use a video service such as YouTube.


FAQ

Will two-pass provide a better quality than CRF?

No, though it does allow you to target a file size more accurately.

Why is placebo a waste of time?

It helps at most ~1% compared to the veryslow preset at the cost of a much higher encoding time. It's diminishing returns: veryslow helps about 3% compared to the slowerpreset, slower helps about 5% compared to the slow preset, and slow helps about 5-10% compared to the medium preset.

Why doesn't my lossless output look lossless?

Blame the RGB to YUV color space conversion. If you convert to yuv444 it should still be lossless (which is the default now).

Will a graphics card make x264 encode faster?

Not necessarily. x264 supports OpenCL for some lookahead operations. There are also some proprietary encoders that utilize the GPU, but that does not mean they are well optimized, though encoding time may be faster; and they might be worse than vanilla x264, and possibly slower. Regardless, FFmpeg today doesn't support any means of GPU encoding, outside of libx264.

Encoding for dumb players

You may need to use -pix_fmt yuv420p for your output to work in QuickTime and most other players. These players only supports the YUV planar color space with 4:2:0 chroma subsampling for H.264 video. Otherwise, depending on your source, ffmpeg may output to a pixel format that may be incompatible with these players.


Additional Resources

  • x264 Settings - MeWiki
  • x264 Encoding Suggestions - MeWiki
  • Constant Rate Factor Guide
Last modified 7 months ago



0 0