FFmpeg cheat sheet

A collection of handy FFmpeg CLI commands.

Download FFmpeg: https://www.ffmpeg.org/download.html

Full documentation: https://www.ffmpeg.org/ffmpeg.html

Basic conversion

ffmpeg -i input.mp4 output.avi

Remux an MKV file into MP4

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

High-quality encoding

Adjust the output quality by configuring the crf (Constant Rate Factor) parameter. A lower crf value results in higher quality, with a possible range of 0 to 51. The standard setting is 23, but for visually lossless compression, set it to -crf 18. Adjust the encoding speed with the preset option. More information at:https://trac.ffmpeg.org/wiki/Encode/H.264.

ffmpeg -i input.mp4 -preset slower -crf 18 output.mp4

Trimming

Without re-encoding:

ffmpeg -ss [start] -i input.mp4 -t [duration] -c copy output.mp4
  • -ss specifies the start time, e.g. 00:01:23.000 or 83 (in seconds)
  • -t specifies the duration of the clip (same format).
  • The latest versions of ffmpeg include a -to option for specifying the stop time.
  • Using the -c copy option, ffmpeg can duplicate the initial video, audio, and subtitle streams from the source to the destination file without re-encoding, preserving the original quality and significantly reducing processing time.
  • The latest versions of ffmpeg include the -to option to specify the end time of the output.
  • Using the -c copy option, ffmpeg can copy the initial video, audio, and subtitle streams from the source to the destination without re-encoding, preserving the original quality and significantly reducing processing time.

With re-encoding:

If you exclude the -c copy option, ffmpeg will transcode the audio and video to adhere to the desired format. To ensure high-quality output, refer to the x264 Encoding Guide for video encoding best practices, and the AAC Encoding Guide for audio.

For example:

ffmpeg -ss [start] -i input.mp4 -t [duration] -c:v libx264 -c:a aac -strict experimental -b:a 128k output.mp4

Mux video and audio from another video

To copy the video from in0.mp4 and audio from input1.mp4:

ffmpeg -i in0.mp4 -i input1.mp4 -c copy -map 0:0 -map 1:1 -shortest output.mp4
  • Using -c copy, the streams are stream copied rather than re-encoded, preserving the original quality. To re-encode instead, refer to the FFmpeg Wiki on H.264 Encoding.
  • Using the -shortest flag ensures that the duration of the resulting output aligns with the shortest input stream's length.
  • See the -map option documentation for more info.

Concat demuxer

First, make a text file.

file 'input1.mp4' file 'input1.mp4' file 'input3.mp4' file 'input4.mp4'

Then, run ffmpeg:

ffmpeg -f concat -i list.txt -c copy output.mp4

Delay audio/video

Delay video by 3.84 seconds:

ffmpeg -i input.mp4 -itsoffset 3.84 -i input.mp4 -map 1:v -map 0:a -vcodec copy -acodec copy output.mp4

Delay audio by 3.84 seconds:

ffmpeg -i input.mp4 -itsoffset 3.84 -i input.mp4 -map 0:v -map 1:a -vcodec copy -acodec copy output.mp4

Burn subtitles

Use the libass library (make sure your ffmpeg install has the library in the configuration --enable-libass).

First convert the subtitles to .ass format:

ffmpeg -i sub.srt sub.ass

Then add them using a video filter:

ffmpeg -i input.mp4 -vf ass=sub.ass output.mp4

Extract the frames from a video

To extract all frames from between 1 and 10 seconds, and also between 15 and 20 seconds:

ffmpeg -i input.mp4 -vf select='between(t,1,10)+between(t,15,20)' -vsync 0 out%d.png

To extract one frame per second only:

ffmpeg -i input.mp4 -fps=1 -vsync 0 out%d.png

Rotate a video

Rotate 90 clockwise:

ffmpeg -i input.mov -vf "transpose=1" output.mov

For the transpose parameter you can pass:

  • 0 = 90CounterCLockwise and Vertical Flip (default)
  • 1 = 90Clockwise
  • 2 = 90CounterClockwise
  • 3 = 90Clockwise and Vertical Flip

Use -vf "transpose=2,transpose=2" for 180 degrees.

Download "Transport Stream" video streams

  1. Locate the playlist file, e.g. using Chrome, press F12, and then Network > Filter: m3u8
  2. Download and concatenate the video fragments:
ffmpeg -i "playlist_path.m3u8" -c copy -bsf:a aac_adtstoasc output.mp4

If an error "Protocol 'https not on whitelist 'file,crypto'!" error occurred, add the protocol_whitelist option:

ffmpeg -protocol_whitelist "file,http,https,tcp,tls" -i "playlist_path.m3u8" -c copy -bsf:a aac_adtstoasc output.mp4

Mute some of the audio

To replace the first 30 seconds of audio with silence:

ffmpeg -i input.mp4 -vcodec copy -af "volume=enable='lte(t,30)':volume=0" output.mp4

To replace all audio between 1'30" and 1'40" with silence:

ffmpeg -i input.mp4 -vcodec copy -af "volume=enable='between(t,90,100)':volume=0" output.mp4

Deinterlace

Deinterlacing using "yet another deinterlacing filter".

ffmpeg -i input.mp4 -vf yadif output.mp4

Create a video slideshow from images

Parameters: -r marks the image framerate (inverse time of each image); -vf fps=25 marks the true framerate of the output.

ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p output.mp4

Extract images from a video

  • Extract all frames: ffmpeg -i input.mp4 thumb%04d.jpg -hide_banner
  • Extract a frame each second: ffmpeg -i input.mp4 -vf fps=1 thumb%04d.jpg -hide_banner
  • Extract only one frame: ffmpeg -i input.mp4 -ss 00:00:10.000 -vframes 1 thumb.jpg

Display the frame number on each frame

ffmpeg -i input.mov -vf "drawtext=fontfile=arial.ttf: text=%{n}:
            x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000099:
            fontsize=72" -y output.mov

Metadata: Change the title

ffmpeg -i input.mp4 -map_metadata -1 -metadata title="Awesome Title" -c:v copy -c:a copy output.mp4

Tools

https://ffmpeg.lav.io/ is an interactive resource to compose FFmpeg actions.