Video X264 encoding

Last update : September 17, 2013

I wanted to know the best X264 parameters to encode my personal movies with ffmpeg for my family website. I rendered 375 frames (15 seconds) from the open-source Big-Buck-Bunny image files (360-png) with different settings, starting at frame 1.113. This post refers to my former post about AVC (H264) video settings.

The common parameters for the encoding are :

  • -vcodec libx264
  • -f image2
  • -pix_fmt yuv420p (chroma subsampling : 4:2:0)
  • -tune animation
  • resolution (pal) : 640 x 360 pixels
  • frame rate : 25 fps

1st Test

The ffmpeg settings for the first test series are :

  • -preset veryslow
  • -profile:v baseline
  • -level 3
  • -refs 1

The value of the Constant Rate Factor (CRF) was changed from 20 to 32, in steps of 3. Here are the results :

CRF Filesize (KB) Videostream (Kbps) Bits/(Pixel*Frame)
 20  2.430  1.326  0.230
 23  1.472  802  0.139
 26  877  478  0.083
 29  531  289  0.050
 32  338  183  0.032

ffmpeg_crf

Visually the quality difference between the movies with an CRF = 20 and CRF = 32 is not perceptible. These are snapshots of the two movies :

CRF = 20  Size = 39,6 KB

CRF = 20  Image size = 39,6 KB

CRF = 32 Size = 32,6 KB

CRF = 32  Image size = 32,6 KB

2nd Test

The ffmpeg settings for the second test series are :

  • -crf : 20
  • -profile:v baseline
  • -level 3
  • -refs 1

The three presets veryslow, medium and ultrafast have been used. Here are the results :

Preset Filesize (KB) Videostream (Kbps) Bits/(Pixel*Frame)
veryslow 2.430 1.326 0.230
medium 2.729 1.489 0.258
ultrafast 5.276 2.880 0.500

ffmpeg_preset

Presets are designed to reduce the work needed to generate sane, efficient commandlines to trade off compression efficiency against encoding speed. The default preset is medium. If you specify a preset, the changes it makes will be applied before all other parameters are applied.

The X264 settings of the different presets are :

ultrafast

  • –no-8x8dct
  • –aq-mode 0
  • –b-adapt 0
  • –bframes 0
  • –no-cabac
  • –no-deblock
  • –no-mbtree
  • –me dia
  • –no-mixed-refs
  • –partitions none
  • –rc-lookahead 0
  • –ref 1
  • –scenecut 0
  • –subme 0
  • –trellis 0
  • –no-weightb
  • –weightp 0

veryslow

  • –b-adapt 2
  • –bframes 8
  • –direct auto
  • –me umh
  • –merange 24
  • –partitions all
  • –ref 16
  • –subme 10
  • –trellis 2
  • –rc-lookahead 60

3rd Test

The ffmpeg settings for the third test series are :

  • -preset veryslow
  • -crf : 20
  • -profile:v baseline
  • -level 3

The numer of reference frames was changed to the values 1, 2, 4, 8 and 16. Here are the results :

Ref frames Filesize (KB) Videostream (Kbps) Bits/(Pixel*Frame)
1 2.430 1.326 0.230
2 2.378 1.297 0.225
4 2.203 1.201 0.209
8 2.079 1.134 0.197
16 2.027 1.106 0.192

ffmpeg_ref_frames

4th Test

The ffmpeg settings for the fourth test series are :

  • -crf : 20
  • -profile:v main
  • -level 3

The numer of reference frames was changed to the values 4, 8 and 16 for the two presets veryslow and medium (4 is the minimum number of reference frames of the main profile). Here are the results :

Preset Ref frames Filesize (KB) Videostream (Kbps) Bits/(Pixel*Frame)
 veryslow 4 1.517 826 0.143
 veryslow 8 1.411 768 0.133
 veryslow 16 1.389 756 0.131
 medium 4 1.700 926 0.161
 medium 8 1.636 891 0.155
medium 16 1.607 875 0.152

ffmpeg_ref_frames_x

5th Test

The ffmpeg settings for the fifth test series are :

  • -preset veryslow
  • -crf : 20

The profiles and levels have been changed. Here are the results :

Profile@Level Filesize (KB) Videostream (Kbps) Bits/(Pixel*Frame)
baseline@3.0 2.430 1.326 0.230
main@3.0 1.517 826 0.143
high@3.0 1.405 765 0.133

ffmpeg_profiles

Profiles are not set by default in X264. If a profile is specified, it overrides all other settings, so that a compatible stream will be guaranteed.

The X264 settings of the different profiles are :

baseline

  • –no-8x8dct
  • –bframes 0
  • –no-cabac
  • –cqm flat
  • –weightp 0
  • No interlaced
  • No lossless

main

  • –no-8x8dct
  • –cqm flat
  • No lossless

high

  • No lossless

A level inside a profile specifies the maximum picture resolution, frame rate and bit rate that a decoder may use.

The complete detailed informations about settings are available in the x264.exe inbuild documentation, accessible with the command x264 –fullhelp .

The following list provides some links to websites with more informations about ffmpeg and x264 video encoding :

MPEG-4 Tools

Last update : September 16, 2013

To create and modify MPEG-4 Multimedia files, you need different MPEG-4 tools, e.g. an encoder, a multiplexer and a packager :

MPEG-4 Tools : Video encoder

x264  (Wikipedia) is a free software library (libx264) and application (x264.exe) for encoding video streams into the H.264/MPEG-4 AVC format, and is released under the terms of the GNU GPL. X264 provides best-in-class performance, compression, and features, gives the best quality and has the most advanced psychovisual optimizations. A comparison with other H264 codecs is available at the MSU Graphics & Media Lab (Video Group) of Lomonosov Moscow State University. The leader in this comparison for software encoders is x264, followed by MainConcept, DivX H.264 and Elecard.

X264.exe is a command line tool. A typical command to enter in the Command Prompt Window looks as follows :

x264.exe --crf 18 --ref 3 --bframes 2 --subme 3 --keyint 100 --sar 1:1 --output %1.mkv %1
pause

All available parameters can be listed with the command x264 –fullhelp. The purpose and use of all x264 settings is also explained on the MeWiki website.

The fourcc code of the X264 codec is X264.

MPEG-4 Tools : Multiplexer

To encode videos, x264 is not sufficient. Audio, subtitles and metadata should be added, and all these data need to be multiplexed. Therefore other tools are needed. FFmpeg is one of these tools. FFmpeg is a free software project that produces libraries and programs for handling multimedia data. It includes libavcodec, the leading audio/video codec library and libavformat, an audio/video container mux and demux library. FFmpeg is published under the GNU Lesser General Public License 2.1+ or GNU General Public License 2+, depending on which options are enabled. The ffmpeg component is a command-line tool to convert one video file format to another. X264 is added as an external library to FFmpeg. Zeranoe has great static builds of FFmpeg for Windows with libx264 included. Other useful external libraries are the Fraunhofer AAC library for AAC encoding and the LAME library for MP3 encoding.

A very comprehensive documentation about ffmpeg , the libraries, utilities and tools is available at the FFmpeg website.

MPEG-4 Tools : Packager

A third command-line tool performing some manipulations on ISO media files like mp4 is MP4Box, the multimedia packager from GPAC (Project on Advanced Content). Dynamic Adaptive Streaming over HTTP (DASH) is one example. GPAC officially started as an open-source project in 2003 with the initial goal to develop from scratch, in ANSI C, clean software compliant to the MPEG-4 Systems standard, a small and flexible alternative to the MPEG-4 reference software. The GPAC framework is being developed at École nationale supérieure des télécommunications (ENST) as part of research work on digital media. A general documentation about MP4Box is available at the GPAC website.

MP4Box is a command-line tool, the following GUI’s are available :

  • MeGUI, by several authors (version 2356, released on June 8, 2013)
  • My MP4Box GUI, by Matthew Bodin (version 0.6.0.6, released on January 4, 2013)
  • Java MP4Box Gui, by Rune André Liland (version 1.7, released on May 18, 2013)
  • Yamb, by kurtnoise version 2.1.0.0 beta 2, released on June 29, 2009)

The following list provides links to additional posts about MPEG-4 tools :