Dec 27, 2008

HOWTO: quick reference on audio & video encoding with ffmpeg

The purpose of this article is to serve as a quick reference on how to use ffmpeg for audio and video encoding. Boy can these ffmpeg flags be tricky to remember, especially codec names: they can really drive me crazy :)

Let's cover audio encoding first (MPEG, MP3, AAC, 3GPP AMR, OGG, FLAC & WMA). Then, we'll talk about video (MPEG, FLV, XVID, MP4, Theora, WMV, H.264 & MKV), with a tiny bit of benchmarking as well. Finally, we'll try some heavy duty video compression for mobile devices.

BTW, all these examples work (!) with the latest ffmpeg and all output files have been successfully tested with VLC 0.9.8a.

1) Audio encoding

The audio.wav sample file is PCM audio (pcm_sb16le) inside a WAV container.

Some generic audio options:
  • -f: the output file format (i.e container)
  • -acodec: the codec to use for encoding
  • -ab: the bitrate in bits per second (e.g. 64000, 64k, etc)
  • -ar: the sampling rate in Hertz (e.g. 22000, 44100, etc)
  • -ac: the number of channels (e.g. 1 for mono, 2 for stereo, etc)

MPEG-1 layer 2 audio

% ffmpeg -i audio.wav -acodec mp2 -ab 192k audio.mp2

MPEG-1 layer 3 audio, Constant Bit Rate (CBR)

% ffmpeg -i audio.wav -acodec libmp3lame -ab 192k audio.mp3

MPEG-1 layer 3 (MP3) audio, Variable Bit Rate (VBR)

% ffmpeg -i audio.wav -acodec libmp3lame -aq [0-255] audio.mp3
0: highest quality (weird, I know).

AAC audio, Constant Bit Rate (CBR)

% ffmpeg -i audio.wav -f adts -acodec libfaac -ab 192k audio.aac

AAC audio, Variable Bit Rate (VBR)

% ffmpeg -i audio.wav -f adts -acodec libfaac -aq [0-255] audio.aac

0:lowest quality, 255:highest quality

3GPP AMR Narrow band (AMR-NB), 8KHz, 6.7Kbit/s

% ffmpeg -i audio.wav -f 3gp -acodec libamr_nb -ac 1 -ar 8000 -ab 6.7k audio.3gp

3GPP AMR Wideband (AMR-WB), 16KHz, 23.85Kbit/s

% ffmpeg -i audio.wav -f 3gp -acodec libamr_wb -ac 1 -ar 16000 -ab 23.85k audio.3gp

OGG Vorbis, Constant Bit Rate (CBR)

% ffmpeg -i audio.wav -f ogg -acodec libvorbis -ab 192k audio.ogg

OGG Vorbis, Variable Bit Rate (VBR)

% ffmpeg -i audio.wav -f ogg -acodec libvorbis -aq [0-255] -ab 192k audio.ogg

0:lowest quality, 255:highest quality

FLAC

% ffmpeg -i audio.wav -acodec flac audio.flac

Windows Audio Media v2

% ffmpeg -i audio.wav -acodec wmav2 -ab 256k audio.wma

2) Video encoding


The video.mpg sample file is a 3-minute video clip : 352x288 resolution, MPEG-2 PS container, MPEG-1 video (1700 Kbit/s), MPEG-1 layer 2 audio (224 Kbit/s) --> 41.7 MB.

Here, just for reference, I will also look at the encoding time and at the size of the output file. FYI, I'm running these tests on a 1.83GHz Intel Core 2 CPU.

Some generic video options:
  • -f: the output file format (i.e container)
  • -vcodec: the codec to use for encoding
  • -b: the bitrate in bits per second (e.g. 1024k)
  • -r: the rate in frames per second (e.g. 25)
  • -async 1: synchronize the start of the audio and video streams

MPEG-2 PS container, MPEG-2 video, audio untouched

% ffmpeg -i video.mpg -f mpeg -vcodec mpeg2video -acodec copy video2.mpg

'copy' can be used to leave the audio / video source stream as is.

FLV container, H263 video, MPEG-1 layer 3 (MP3) audio

% ffmpeg -i video.mpg -f flv -vcodec flv -r 25 b 1024k -acodec libmp3lame -ab 128k -ac 2 video.flv

--> 20 seconds, 6.4 MB

AVI container, XVID video, MPEG-1 layer 3 (MP3) audio

% ffmpeg -i video.mpg -f avi -vcodec libxvid -r 25 -b 1024k -acodec libmp3lame -ab 128k -ac 2 video.avi

--> 29 seconds, 24.7 MB.

MPEG4 container, MPEG-4 video (Simple profile, L1), AAC-LC audio

% ffmpeg -i video.mpg -f mp4 -vcodec mpeg4 -r 25 -b 1024k -acodec libfaac -ab 128k -ac 2 -async 1 video.mp4

--> 22 seconds, 24.6 MB

OGG container, Theora video, Vorbis audio

% ffmpeg -i video.mpg -f ogg -vcodec libtheora -r 25 -b 1024k -acodec libvorbis -ab 128k -ac 2 -async 1 video.ogg

--> 1 minute 24 seconds, 24.4 MB

AVI container, Windows Media Video 8 (WMV9), Windows Media Audio 2 (WMA2)

% ffmpeg -i video.mpg -f avi -vcodec wmv2 -r 25 -b 1024k -acodec wmav2 -ab 128k -ac 2 -async 1 video.avi

-->18 seconds, 24.7 MB

MP4 container, H.264/AVC (Baseline profile, L2.1) video, AAC-LC audio

% ffmpeg -i video.mpg -f mp4 -vcodec libx264 -r 25 -b 1024k -acodec libfaac -ab 128k -ac 2 -async 1 video.mp4

--> 1 minute 19 seconds, 25.5 MB

Matroska container (MKV), H.264/AVC (Baseline profile, L2.1) video, AAC-LC audio

% ffmpeg -i video.mpg -f matroska -vcodec libx264 -r 25 -b 1024k -acodec libfaac -ab 128k -ac 2 -async 1 video.mkv

--> same as above: only the container differs

Or you could have reused the MP4 file above to perform a simple re-muxing of the audio and video streams into a MKV container :

% ffmpeg -i video.mp4 -f matroska -vcodec copy -acodec copy video.mkv

3) Mobile video encoding

Now, let's try some heavy duty compression!

3GPP container, H.263 video, AMR-WB audio

% ffmpeg -i video.mpg -f 3gp -vcodec h263 -b 256k -r 15 -acodec libamr_wb -ac 1 -ar 16000 -ab 23.85k -async 1 video.3gp

--> 15 seconds, 6.1 MB

Let's resize the video to qcif (176x144) and degrade the video stream. Since this is a music video, I can't really degrade the audio stream any further.

% ffmpeg -i video.mpg -f 3gp -s qcif -vcodec h263 -b 96k -r 12 -acodec libamr_wb -ac 1 -ar 16000 -ab 23.85k -async 1 video.3gp

--> 13.2 seconds, 2.7 MB

Let's degrade video even more.

% ffmpeg -i video.mpg -f 3gp -s qcif -vcodec h263 -b 48k -r 10 -acodec libamr_wb -ac 1 -ar 16000 -ab 23.85k -async 1 video.3gp

--> 12.5 seconds, 1.7 MB... and still enjoyable at less than 100 Kbit/s. That's 20x less than the original video.

OK, enough for now. I'll probably keep adding to this page to help my failing memory :) There's also plenty more to explore, especially on the x264 side, but that's it for today!

2 comments:

  1. Hi Juju,

    Indeed with H264 you will explore new horizons of the video compression.
    Limited to the baseline profile on the mobile device (because of the CPU load required on the other profiles), you will enjoy a BIG increase in the quality at equivalent bitrates.
    And nowadays all new phones support h264.
    definitely the codec to use!

    BTW: happy new year! :)
    Boz

    ReplyDelete
  2. Thanks for posting all these. FFmpeg can feel like a black magic app at times. So many codecs and formats all with their own particular flags, etc. I really appreciate it!

    ReplyDelete