Image sequence into H.264

Problem

Transcode an image sequence by using the H.264 codec for dissemination purposes.

Solution

ffmpeg \
    -f image2 \
    -framerate 24 \
    -i input_file_%06d.ext \
    -c:v libx264 \
    -preset veryslow \
    -qp 18 \
    -pix_fmt yuv420p \
    output_file

General command

ffmpeg \
    -f image2 \
    -framerate frames_per_second \
    -i input_file_regex.ext \
    -c:v libx264 \
    -preset preset_value \
    -qp quantisation_parameter \
    -pix_fmt yuv420p \
    output_file

Command syntax

ffmpeg
starts the command
-f image2
forces the image file de-muxer for single image files
-framerate frames_per_second
sets the frame rate
-i input_file_regex.ext
path, name and extension of the input files
-c:v libx264
The library libx264 re-encodes the video stream using the H.264 video codec.
-preset preset_value
A slower encoding preset means a better compression rate.
-qp quantisation_parameter
A quantisation parameter of 18 means a «visually lossless» compression.
-pix_fmt yuv420p
The pixel format for «YUV» colour space with 4:2:0 chroma subsampling and planar colour alignment is chosen for best compatibility.
output_file.mov
path, name and extension of the output file

Discussion

The parameters witch apply to the input files must precede them. Therefore the option -f image2 must precede the image sequence given as input.

The frame rate of sound film is 24 fps (frames per second) and the default frame rate of image2 is 25 fps.

The regex %06d matches six digits long numbers, possibly with leading zeroes. This allows to read in ascending order, one image after the other, the full sequence inside one folder. The command must of course match the naming convention actually used. And for image sequences starting at 086400 (captured at 24 fps with a timecode starting at 01:00:00:00) or at 090000 (captured at 25 fps with a timecode starting at 01:00:00:00), add the flag -start_number 086400 or -start_number 090000 before -i input_file_%06d.ext.

The extension for TIFF files is .tif or maybe .tiff; the extension for DPX files is .dpx (or eventually .cin for old Cineon files). Other file formats are possible.

The video codec is specified by -codec:video, which may be abbreviated as -c:v (or -codec:v or -c:video). We advise to avoid the alias -vcodec.

Possible -preset values for the H.264 codec include:

  • veryslow
  • slow
  • medium
  • fast
  • veryfast

Slower encoding means better compression rate.

You can use the parameter -crf 18 (constant rate factor) instead of -qp 18 (quantisation parameter) which gives a similar «visually lossless» result. The range of the quantiser scale for crf and qp is from 0 to 51, where 0 is lossless, approximately 18 is «visually lossless», 23 is the default value and 51 is worst possible. Most of the non-FFmpeg-based players cannot decode H.264 files holding lossless content.

By default the library libx264 will use the chroma subsampling scheme that matches closest the input file’s chroma subsampling. This can result in the «YUV» colour space with 4:4:4 or 4:2:2 or 4:2:0 chroma subsampling. Many of the non-FFmpeg-based players cannot decode H.264 files having a different chroma subsampling than 4:2:0. Therefore, in order to allow possibly all players to read the file, we suggest use to the yuv420p pixel format for dissemination purposes. And, as sadly usual in the computer world, «YUV» stands for the colour space Y′CBCR and not for Y′UV, which is used for PAL video.

Often the MP4 container is choses for wrapping H.264, but others are possible.

For advanced users

yuv420p is an 8-bit pixel format, which is common among distributors. For a bit depth of 10, which is common in video post-production, the pixel format yuv420p10le may be used, yet the library libx264 must be compiled in 10-bit mode for this. By the way, in this case the range of the quantiser scale for -crf and -qp is from 0 to 63.

If ffmpeg is linked to libx264 dynamically rather than statically, then it will adapt automatically to the bit depth supported by the run-time libx264, regardless of the version used at build time. This way it’s possible to work either 8-bit or 10-bit in each ffmpeg command; however you cannot combine 8-bit and 10-bit in the same command, you need two commands.


2018–07–21