Beginners Video Encoding Guide using ffmpeg

Simplest ffmpeg Usage

Converts a WebM video file to MP4:

ffmpeg -i input_file.webm output_file.mp4

To change the output format, simply change the file extension.

How to Change the Video Codec (Video Encoding Method)

Many video file formats are “containers” and wrap an extra layer around the actual video file format. The underlying video can be in many different formats (known as encodings) within the outer container format that encloses the video. Software that writes video and audio data is often known as a “codec”. MKV (Matroska) is a popular and widely supported video container format (the outermost layer) that internally supports multiple video and audio encodings.

You can see all of the video file formats supported by ffmpeg using this command, which will produce a bewilderingly large list:

ffmpeg -codecs

Many of these formats are now out of date or niche formats that are rarely used but ffmpeg supports the whole lot for completeness. To cut this down to some popular options, commonly used video encodings include:

  • h.264 (also known as AVC) - Extremely widely supported by almost all video playing devices.
  • h.265 (also known as HEVC) - Newer encoding method that succeeds h.264 and produces smaller video files at the same quality. It requires more computing power to produce the files (slower to produce). It is now fairly widely supported, but not as widely supported as h.264. Older and cheaper devices may not play it as it requires a faster processor and more complex hardware.
  • VP9 - A Google developed codec often used for video streaming

If in doubt try h.264 which is almost certain to play on any device. H.265 will produce smaller files and is a better option if you are in control of the device the video will be played on and can check in advance that it is compatible. By default ffmpeg will often choose h.264.

This line converts a video file into the Matroska container format using the h.264 codec:

ffmpeg -i input_file.webm -c:v libx264 -crf 23 output_file.mkv

This converts a video into Matroska using the h.265 codec:

ffmpeg -i input_file.webm -c:v libx265 -crf 23 output_file.mkv

Options explanation:

  • -c:v libx265 - Encodes the video stream using the h.265 codec (within an MKV file)
  • -crf 23 - Set’s the video quality. The meaning of the parameter varies with the codec used. A lower number means higher quality but a larger file. A higher number means a smaller file but reduced quality. For h.264 and h.265 the accepted range of values is 0 - 51. When set to zero the quality will be the maximum that is possible but the file will be massive. When set to 51 the quality will be worse (often unwatchably terrible) but the file will be the smallest that is possible with the codec. A good balance is often somewhere in the 20s which will produce reasonable quality at a reasonable file size. The exact number required is a case of trial and error.

How To Select and Encode Specific Audio Tracks

As with video, there are several audio codecs. Popular choices to accompany video include:

  • AC-3 (Dolby Digital) - Widely supported and includes support for surround sound. Often used on DVD and blu-ray video.
  • AAC (Advanced Audio Coding) - A successor to MP3 which produces smaller files at the same quality and includes surround support.
  • FLAC - Lossless encoding for the highest quality but produces larger files
  • Vorbis - an open source audio codec comparable with AAC

If in doubt, Vorbis or AAC are decent choices for most devices. Vorbis can be more compatible with open source video players. AAC is widely used on Apple devices but is also usually available elsewhere. At the time of writing AAC may be encumbered by patents in some countries which can cause support to vary. By default ffmpeg will often use Vorbis.

To encode a video as H.265 and change the audio codec to AAC:

ffmpeg -i input_file.webm -c:v libx265 -crf 23 -c:a aac output_file.mkv

Option explanation:

  • -c:a aac Encode the audio as AAC

By default ffmpeg will select the first audio track found only and will ignore other tracks, however it is common for a video file to have multiple audio tracks e.g. for different languages. To select specific audio tracks use the map option:

In this example we encode a video file as H.265 with AAC audio and choose the 1st and 2nd audio track only, ignoring all the others. This command will result in no subtitles. To select subtitles (if available in the original video), see below:

ffmpeg -i input_file.mkv -c:v libx265 -crf 23 -c:a aac -map 0:v -map 0:a:0 -map 0:a:1 output_file.mkv

  • -map 0:a:0 - The first zero means select the from the first input video file presented on the command line (there is only one file in this case). The second zero means select the first audio track found in that input file (it starts counting tracks from zero). The “a” in the middle means select amongst audio tracks as opposed to video or subtitle tracks.
  • -map 0:a:1 - From the first video file, choose the 2nd audio track
  • -map 0:v - Select all the video tracks found. If you use the map option for audio tracks but do not specify a video track then you will only get the audio output and no video at all. If you manually choose any specific tracks of any type, then you need to specify everything you want from the file manually.

It is often the case that video players will play the first audio track found by default. You may want to make your preferred language appear first so it is played without having to manually choose it each time you play the video file. Ffmpeg writes audio tracks in the order that the map options appear, so place the map option for your preferred audio track first and that will often become the default in your video player.

If you want to encode all audio tracks found regardless you can specify ‘-map 0:a’. This says choose all audio tracks from the first video file listed on the command line.

To find out the number of the audio track(s) you require, open the input video in any video player. Usually there is a menu where audio tracks are chosen. Video players usually list the audio tracks in the audio menu in the order in which they are found in the file so e.g. audio track zero is the first one displayed in the menu.

Selecting Specific Subtitle Tracks Only

Similarly to audio, specific subtitle tracks can be selected using the map option but with “s” for subtitle in the middle of the parameter.

This command encodes the video in H.265, uses AAC audio, selects the 1st and 2nd audio track and then the 4th subtitle track only. As

ffmpeg -i input_file.mkv -c:v libx265 -crf 23 -map 0:v -c:a aac -map 0:a:0 -map 0:a:1 -c:s copy -map 0:s:3 output_file.mkv

Option explanation:

  • -c:s copy - Specifies that the subtitles should be directly copied in whatever format they are encountered in within the original file without any change in the subtitle format.
  • -map 0:s:3 - The 4th subtitle track should be copied from the first video file.

Reducing Interlacing and Combing

Video taken from old standard definition sources can often be subject to “interlacing” or “combing” where even and odd lines in the video are slightly shifted during sequences where the camera is panning or there is fast moving action. To solve this use this option:

-vf yadif=1