Skip to content

Instantly share code, notes, and snippets.

@phenotypic
Last active March 4, 2024 01:42
Show Gist options
  • Save phenotypic/65f3991e74495a6964bf43c628d616c6 to your computer and use it in GitHub Desktop.
Save phenotypic/65f3991e74495a6964bf43c628d616c6 to your computer and use it in GitHub Desktop.
FFmpeg cheat sheet

FFmpeg commands

A handy cheat sheet for various FFmpeg commands I use frequently.

H.265/HEVC encoding

ffmpeg -i input -c:v libx265 -c:a copy output
  • For Apple compatibility, add -tag:v hvc1
  • For Apple hardware acceleration, replace -c:v libx265 with -c:v hevc_videotoolbox (faster but produces lower quality video and larger file sizes)
  • Lower the constant rate factor (default 28) and use a slower preset (default medium) for better quality and compression e.g. -crf 25 -preset slow
  • If there are audio issues, replace -c:a copy output with -c:a aac -ac 2 for stereo audio or -c:a eac3 for multi-channel audio. Consider specifying the audio bitrate too, e.g. -b:a 224k

AV1 Encoding

ffmpeg -i input -c:v libaom-av1 -c:a copy output

File formats

Convert file to mp4:

ffmpeg -i input.mkv -codec copy output.mp4
  • Add -c:s mov_text to copy text-based subtitles (cannot convert subs like dvd_subtitle to text)
  • Add -tag:v hvc1 to translate Apple HEVC compatibility (see Translation)

Soft-coded subtitles

Add .srt file to video:

ffmpeg -i input.mp4 -i subs.srt -c copy -c:s mov_text -metadata:s:s:0 language=eng output.mp4
  • -metadata:s:s:0 language=eng can be omitted

Add multiple .srt files to video:

ffmpeg -i input.mp4 -i english.srt -i japanese.srt -c copy  -map 0:v:0 -map 0:a:0 -map 1 -map 2 -c:s mov_text -metadata:s:s:0 language=eng -metadata:s:s:1 language=jpn output.mp4
  • -metadata:s:s:0 language=eng -metadata:s:s:1 language=jpn can be omitted

Copy multiple pre-existing subtitles

ffmpeg -i input.mkv -c copy -map 0:v:0 -map 0:a:0 -map 0:s:9 -map 0:s:19 -c:s mov_text output.mp4
  • Metadata is also copied from original
  • 0:s:8 and 0:s:19 represent subtitle streams 9 and 20 respectively
  • Can be used in concert with HEVC translation by adding -tag:v hvc1
  • Consider -map 0:v and -map 0:a instead of -map 0:v:0 and -map 0:a:0 respectively

Translation

Translate basic H.265/HEVC file to be Apple-compatible:

ffmpeg -i input -codec copy -tag:v hvc1 output.mp4
  • Audio stream may require altering to be compatible:
    • Stereo: -c:a aac -ac 2
    • Multi-channel: -c:a eac3 (may require -filter_complex "channelmap=channel_layout=5.1" -c:a aac)
  • Add -c:s mov_text to copy text-based subtitles (cannot convert subs like dvd_subtitle to text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment