ffmpeg Cheatsheet

ffmpeg is an incredibly powerful commandline media tool. Inspecting, decoding, encoding audio and video and its container formats.

If you are unfamiliar with media container formats (.mp4, .mp3, .mkv, etc) and their content stream types, see Introduction to Media Containers.

Format Recommendations

Intention Container Parameters Audio Video
Modern & Rich mkv -c:a libopus -c:v libx265 -pix_fmt yuv420p10le Opus 10-bit H.265
Modern Web webm -c:a libopus -c:v libsvtav1 -pix_fmt yuv420p10le Opus 10-bit AV1
Compatible mp4 -c:a aac -c:v libx264 -pix_fmt yuv420p AAC 8-bit H.264
Web Image webp - -
Web Image (Looped) webp -loop 0 - -
Web Image (Lossless) webp -lossless 1 - -
Reasoning

Modern x265 instead of AV1: While AV1 is preferable over x265, the encoding speed is much slower on current hardware and current (still evolving) encoders.
Webbrowsers do not support H.265, so we accept the slower encoding speed for compatibility.

libsvtav1 over other AV1 encoders: libsvtav1 currently is significantly faster than librav1e and libaom-av1.

Additional alternatives:

Intention Parameters Description
Maximum Compression -c:a libopus -c:v libx265 -pix_fmt yuv420p10le -preset veryslow 10-bit allows for greater compression, and the slow preset allows the encoding process to spend more time to find better compression

Audio Codec Overview

Audio Codec Alternative Name Description
Opus Best choice if compatible; Free and open format
AAC Used in mp4
mp3 MPEG-2 Audio Layer III, MPEG-1 Audio Layer III Traditional most popular personal digital audio format
Vorbis Free and open format

Video Codec Overview

Video Codec Alternative Name
H.264 AVC - Advanced Video Coding, x264, MPEG-4 Part 10
H.265 HEVC - High Efficiency Video Coding, x265, MPEG-H Part 2
AV1 AOMedia Video 1 Open format

Mapping Streams

Mapping is the process of selecting streams from the input streams and how they map to the output.

Stream references are 0-based indexes.

Parameter Description
-map_chapters -1 drop chapters
-map 0:v map all video streams ()
-map 0:v:0 map the first video stream
-map 0:a map all audio streams ()
-map 0:s map all subtitle streams ()

Examples/Combining

Parameters Description
-map_chapters -1 -map 0:v:0 -map 0:a:0 map the first video and audio streams, drop chapters

Lossless Copy

Parameter Description
-c copy Copy stream

-c for codec, -c:a for audio codec, -c:v for video codec

Note that a lossless copy works especially well for copying full streams (e.g. into a different container format, or while editing others), but if you are cutting/extracting a section it is not precise (video is encoded in key and intermediate frames, so specifying a timed section start or end may be offset).

Cutting

For time-based cutting. Time specified in hh:mm:ss.nnn e.g. 1:45 or 1:45.200 or 13.800 etc.

Parameter Description
-ss <time> start time
-to <time> end time
-t <duration> duration (alternative to end)

Burn Subtitles (Soft to Hard subtitles)

Soft subtitles are embedded in the container file. They get interpreted and rendered by the media player. This allows custom styling, fonts and sizes, as well as selecting between multiple subtitle tracks.

Hard subtitles are part of the video stream, of the image data. They are burned onto the video image.

Subtitles

A video file may have multiple subtitle tracks. Subtitle tracks may be marked as default or forced.

Media players may select a subtitle track through forced -> default -> user setting primary language -> system display language.

  • forced - no subtitles in videos primary spoken language, subtitles when a different language is spoken

Resize Video

Using the video filter (-vf) scale with target width:height and -1 for auto determined.

To scale to 480x:

-vf "scale=480:-1"