Skip to content

Instantly share code, notes, and snippets.

@kadamwhite
Last active November 5, 2022 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadamwhite/d0e86caa2db9a49122993cb510de14ae to your computer and use it in GitHub Desktop.
Save kadamwhite/d0e86caa2db9a49122993cb510de14ae to your computer and use it in GitHub Desktop.
ffmpeg command "cheat sheet"

ffmpeg cheat-sheet

Video

Compressing x265

ffmpeg -i INPUT_FILE.mkv -c copy -pix_fmt yuv420p10le -c:v libx265 -preset slow -crf 27 -profile:v main10 OUTPUT_FILE.mkv

Drop to 30fps

ffmpeg -i INPUT -filter:v fps=30 OUTPUT

Scale to 720p

ffmpeg -i INPUT -vf scale=-1:720 OUTPUT

Concatenate files

See this Stack Overflow post

Make a file with the paths to concat (think this can be relative or absolute)

cat mylist.txt
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'

then use

ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4

Audio

Detect Volume

ffmpeg -i input.wav -filter:a volumedetect -f null /dev/null

Then read the output from the command log:

[Parsed_volumedetect_0 @ 0x7f8ba1c121a0] mean_volume: -16.0 dB
[Parsed_volumedetect_0 @ 0x7f8ba1c121a0] max_volume: -5.0 dB

See AudioVolume wiki page for more

Adjust Volume

# Increase track audio by 10dB
-filter:a "volume=10dB"

# Reduce volume by 5dB
-filter:a "volume=-5dB"

# 150% of current volume
-filter:a "volume=1.5"

Mix surround sound down to stereo

-c:a ac3 -ac 2

Set bitrate

-c:a aac -b:a 192k

Reorder audio streams

ffmpeg -i input.mkv -map 0:v:0 -map 0:a:1 -map 0:a:0 -c copy output.mkv

Set default audio stream

# use video stream (0:0)
# make 2nd audio stream the 1st, make 1st audio stream the 2nd
# use the new 1st audio stream as default
# remove the default from the new 2nd audio stream
ffmpeg -i "input.mkv" -map 0:v -map 0:a:1 -map 0:a:0 -disposition:a:0 default -disposition:a:1 none -c copy "output.mkv"

Time Adjustment

Delay or speed up one input

# Make subtitles happen 2s later
ffmpeg -itsoffset 2 -i subtitles.srt -c copy subtitles_delayed.srt

# Make subtitles happen 700ms earlier
ffmpeg -itsoffset -0.7 -i subtitles.srt -c copy subtitles_advanced.srt

Subtitles

Extract SRT Subtitles

ffmpeg -txt_format text -i input_file out.srt

Set Subtitle Language

-metadata:s:a:1 language=jpn

Set Subtitle Label

-metadata:s:a:0 title="English (5.1 Surround)" -metadata:s:a:1 title="English (Stereo)" -metadata:s:a:2 title="Français"

Remove empty "subtitle track" with chapter names in DVD encodes

-dn -map_metadata:c -1

Note: this will remove chapter names in some cases, but unlike -map_chapters -1 it should preserve the chapter markers themselves. See this SO post for more

Change Framerate

To change the output frame rate to 30 fps, use the following command [source]:

ffmpeg -i <input> -filter:v fps=30 <output>

Select default sub track

# Video from file 1, audio from file 2, subs from file 1. show subs by default.
ffmpeg -i INPUT_1.mkv -i INPUT_2.mkv -c copy -map 0:v -map 1:a -map 0:s -disposition:s:0 default -disposition:a:1 default OUTPUT_FILE.mkv

Metadata/info

An_Archivists_Guide_To_Matroska/metadata.md

Add title/description

(from the "Archivist's Guide" linked above)

-metadata KEY="VALUE"

Example:

ffmpeg -f lavfi -i testsrc -t 1 -metadata ARCHIVAL=yes -metadata PRESERVE_THIS=okay -metadata DESCRIPTION="Just color bars" some_metadata.mkv

Add chapters

ffmpeg -i <input> -i chapter-metadata-file -c copy -map_metadata 1 <output>

Example chapter text file contents:

[CHAPTER]
TIMEBASE=1/1000
START=1
END=448000
title=The Pledge

[CHAPTER]
TIMEBASE=1/1000
START=448001
END= 3883999
title=The Turn

See this site for a helpful script to convert from time, title pairs.

What pixel modes does your encoder support?

ffmpeg -h encoder=libx265 | grep pixel

Putting it all together (hypothetical)

ffmpeg -i INPUT.mp4 -i INPUT.en.srt -c copy -map 0:v -map 0:a -map 1:s -pix_fmt yuv420p10le -c:v libx265 -crf 29 -x265-params profile=main10 -disposition:s:0 default "Output.mkv"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment