Bookmark and Share



FFmpeg on Linux

ffmpeg_logo.png







FFmpeg

FFmpeg is a set of libraries such as libavcodec, libavformat, and libavutil. We can read and write audio/video file formats and decompress and compress their contents.

  • It's the most widely used in Linux distributions.
  • It's also compatible with Windows and Mac.
  • It's mainly created as the culmination of free and open source encoding and decoding libraries that are out there. This makes it one-stop shopping for encoding and decoding pretty much any type of video.
  • libavcodec - an audio/video codec library used by several other projects.
  • libavformat - an audio/video container mux and demux library
  • ffmpeg - command line program for transcoding multimedia files.

FFmpeg - install

On linux (Fedora 18):

  1. Get a snapshot from FFmpeg Download and Source Code Repository Access
    or download the ffmpeg here: FFmpeg 1.2 "Magic" (ffmpeg-1.2.tar.bz2).
  2. tar xvjf ffmpeg-1.2.tar.bz2
  3. ./configure in the ffmpeg diretory.
  4. make
  5. make install
  6. $ which ffmpeg
    /usr/local/bin/ffmpeg
    $ which ffprobe
    /usr/local/bin/ffprobe

Actually, to use other libraries such as theora, I installed the ffmpeg this way:

  1. Make ffmpeg_sources directory.
    $ mkdir ~/ffmpeg_sources
       
  2. Get ffmpeg from http://www.ffmpeg.org/download.html
    $ cd ffmpeg_sources
    $ git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
       
  3. We can use additional libraries. Follow the compilation guide from http://ffmpeg.org/trac/ffmpeg/wiki/CentosCompilationGuide
    In my case, they are theora and vorbis.
  4. Configure and make
    $ cd ~/ffmpeg_sources
    $ git clone --depth 1 git://source.ffmpeg.org/ffmpeg
    $ cd ffmpeg
    $ PKG_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" --extra-libs="-ldl" --enable-gpl --enable-nonfree 
    --enable-libvorbis --enable-libtheora 
    --enable-libvpx --enable-libx264
    $ make
    $ make install
       



Transcoding

Original video: h264_720p_hp_5.1_6mbps_ac3_planet.mp4




From mp4 to mov
$ ~/bin/ffmpeg -i h264_720p_hp_5.1_6mbps_ac3_planet.mp4 -q:v 0 -strict -2 h264_720p_hp_5.1_6mbps_ac3_planet.mov
ffmpeg version 1.2 Copyright (c) 2000-2013 the FFmpeg developers
  built on Apr 17 2013 11:17:26 with gcc 4.7.2 (GCC) 20121109 (Red Hat 4.7.2-8)
  configuration: 
  libavutil      52. 18.100 / 52. 18.100
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.104 / 54. 63.104
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 42.103 /  3. 42.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'h264_720p_hp_5.1_6mbps_ac3_planet.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 1
    compatible_brands: isomavc1
    creation_time   : 2011-05-07 22:51:02
    encoder         : My MP4Box GUI 0.5.5.4 
  Duration: 00:01:52.83, start: 0.000000, bitrate: 7052 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 6602 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc
    Metadata:
      creation_time   : 2011-05-07 22:51:02
      handler_name    : Imported with GPAC 0.4.6-DEV (internal rev. 5)
    Stream #0:1(und): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 448 kb/s
    Metadata:
      creation_time   : 2011-05-07 22:51:03
      handler_name    : Imported with GPAC 0.4.6-DEV (internal rev. 5)
Output #0, mov, to 'h264_720p_hp_5.1_6mbps_ac3_planet.mov':
  Metadata:
    major_brand     : isom
    minor_version   : 1
    compatible_brands: isomavc1
    encoder         : Lavf54.63.104
    Stream #0:0(und): Video: mpeg4 (mp4v / 0x7634706D), yuv420p, 1280x720, q=2-31, 200 kb/s, 24k tbn, 23.98 tbc
    Metadata:
      creation_time   : 2011-05-07 22:51:02
      handler_name    : Imported with GPAC 0.4.6-DEV (internal rev. 5)
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, 5.1(side), fltp, 128 kb/s
    Metadata:
      creation_time   : 2011-05-07 22:51:03
      handler_name    : Imported with GPAC 0.4.6-DEV (internal rev. 5)
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> mpeg4)
  Stream #0:1 -> #0:1 (ac3 -> aac)
Press [q] to stop, [?] for help
frame= 2707 fps= 17 q=0.0 Lsize=  283666kB time=00:01:52.90 bitrate=20581.9kbits/s dup=2 drop=0    
video:282637kB audio:972kB subtitle:0 global headers:0kB muxing overhead 0.020263%





From mp4 to ogv
$ ~/bin/ffmpeg -i h264_720p_hp_5.1_6mbps_ac3_planet.mp4 
-codec:v libtheora -qscale:v 7 
-codec:a libvorbis -qscale:a 5 
h264_720p_hp_5.1_6mbps_ac3_planet.ogv

  • -qscale:v -video quality.
    Range is 0-10, where 10 is highest quality. 5-7 is a good range to try. If we omit -qscale:v(or the alias -q:v) then ffmpeg will use the default -b:v 200k which will most likely provide a poor quality output.
  • -qscale:a -audio quality.
    Range is 0-10, where 10 is highest quality. 3-6 is a good range to try. Default is -qscale:a 3.
  • Note: if we omit -codec:a libvorbis then ffmpeg will default to the native FFmpeg flac audio encoder for ogg/ogv output container.
  • See http://ffmpeg.org/trac/ffmpeg/wiki/TheoraVorbisEncodingGuide

ogv video: h264_720p_hp_5.1_6mbps_ac3_planet.ogv






From mp4 to webm
$ ~/bin/ffmpeg -i h264_720p_hp_5.1_6mbps_ac3_planet.mp4 
-c:v libvpx -b:v 1M -c:a libvorbis 
h264_720p_hp_5.1_6mbps_ac3_planet.webm

  • libvpx offers a variable bitrate mode by default. In this mode, it will simply try to reach the specified bit rate on average, e.g. 1 MBit/s. This is the target bitrate.
  • We should choose a higher bit rate if we want better quality. Note that we shouldn't leave out the -b:v option as the default settings will produce mediocre quality output.
  • See http://ffmpeg.org/trac/ffmpeg/wiki/vpxEncodingGuide

webm video: h264_720p_hp_5.1_6mbps_ac3_planet.webm





Audio/Video Mux/Demux

Sample video: bunny.mov


Audio Demux
ffmpeg -analyzeduration 2147480000 -i bunny.mov -ar 48000 -ac 2 -y audio.wav 


Audio Encode
neroAacEnc -cbr 128000 -lc -if audio.wav -of audio.mp4 


Video Demux
ffmpeg -analyzeduration 2147480000 -i bunny.mov -r 25 -s 1280x720 -aspect 16:9 -f yuv4mpegpipe -pix_fmt yuv420p -vsync 1 -y video.y4m


Video Encode with 2 pass
x264 --pass 1 --bitrate 1200 --vbv-maxrate 2400 --vbv-bufsize 6000 --stats ./x264_2pass.log -o /dev/null video.y4m
x264 --pass 2 --bitrate 1200 --vbv-maxrate 2400 --vbv-bufsize 6000 --stats ./x264_2pass.log -o video.h264 video.y4m


Mux Video/Audio

We can mux using MP4Box:

MP4Box -new video.mp4 -add video.h264 -add audio.mp4 -fps 25

or using ffmpeg:

ffmpeg -i video.h264 -i audio.mp4 -vcodec copy -acodec copy -y video.mp4

Video output: video.mp4