Gernot Walzl


FFmpeg decodes and encodes video and audio streams.



To encode a video using H.264 (which is the default for MP4),
invoke ffmpeg with the following options:

ffmpeg -i input.mp4 \
  -vf 'scale=1920:1080' -preset veryslow \
  -movflags faststart output.mp4

Encode a video using H.265 by specifying the codec used for video encoding:

ffmpeg -i input.mp4 \
  -vf 'scale=3840:2160' -c:v libx265 \
  -movflags faststart output.mp4

To cut a video from time 00:00:12 to 00:00:32:

ffmpeg -ss 00:00:12 -to 00:00:32 -i input.mp4 \
  -preset veryslow \
  -movflags faststart output.mp4

To extract an image at time 00:00:12:

ffmpeg -ss 00:00:12 -i input.mp4 \
  -frames:v 1 -s 480x270 poster.jpg

To extract the first subtitle track:

ffmpeg -i input.mp4 \
  -map 0:s:0

To just copy a video stream and an audio stream to another container:

ffmpeg -i input.mkv \
  -c:v copy -c:a copy \
  -movflags faststart output.mp4

Two-pass encoding is recommended for targeting an output file size.
To encode a 90-minute video to fit on a 700 MB CD-ROM:

ffmpeg -i input.mp4 -c:v libx264 -b:v 933k -pass 1 \
  -an /dev/null && \
ffmpeg -i input.mp4 -c:v libx264 -b:v 933k -pass 2 \
  -ac 2 -c:a libfdk_aac -b:a 128k output.mp4

Built-in help

ffmpeg -hide_banner -h

To show available codecs that were enabled during configuration:

ffmpeg -codecs

To show options for the encoder libx264:

ffmpeg -hide_banner -h encoder=libx264

Hardware acceleration

The graphics card can be used to accelerate video encoding.
FFmpeg needs to be built with enabled hardware acceleration.

A shell script that builds DEB packages of FFmpeg with NVIDIA GPU acceleration
enabled is found in the files section of this page.

The following command uses the NVIDIA encoder (NVENC) to encode a video
using HEVC (H.265):

ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 \
  -c:v hevc_nvenc -preset hq \
  -movflags faststart output.mp4

Unfortunately, even with high-quality preset, the video quality of hevc_nvenc
is significantly lower compared to a software encoder (e.g. libx265).

Help about the encoder hevc_nvenc is shown using the following command:

ffmpeg -hide_banner -h encoder=hevc_nvenc

Codecs and where they are used

Optical disc data storage

Disc Launch Video codec Max. resolution
Ultra HD Blu-ray 2016 H.265/HEVC 3840 × 2160
Blu-ray Disc 2006 H.264/MPEG-4 AVC 1920 × 1080
DVD 1996 MPEG-2 720 × 576 (PAL) / 720 × 480 (NTSC)


Mozilla's recommendations for everyday videos:

  1. Container: WebM, Video codec: VP9, Audio codec: Opus
    ffmpeg -i input.mp4 \
      -c:v libvpx-vp9 -c:a libopus \
  2. Container: MP4, Video codec: AVC (H.264), Audio codec: AAC
    ffmpeg -i input.mp4 \
      -c:v libx264 -c:a libfdk_aac \
      -movflags faststart output.mp4

YouTube recommended upload encoding settings:

CONTENT.html source 2023-03-17 5.6 KB source 2023-03-17 2.3 KB
by Gernot Walzl
This shell script builds deb packages of ffmpeg with enabled
NVIDIA hardware acceleration for video encoding. source 2023-03-20 811 B
by Gernot Walzl
Encodes all video files in a directory using ffmpeg.
Usage: ./ INPUT_DIRECTORY OUTPUT_DIRECTORY source 2023-03-04 560 B
by Gernot Walzl
This script uses ffmpeg to concatenate video files.
Usage: ./ input1.mp4 input2.mp4 input3.mp4 ...