Cheatsheets

Personal collection of cheatsheets.

FFmpeg

FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video.

Index

General

Check version.

ffmpeg -version

Convert audio and video.

ffmpeg -i <input> <output>

Muxing

List available muxers and demuxers.

ffmpeg -formats

Remux a mkv into mp4.

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

Mux the video from input0 and audio from input1 to output.

ffmpeg -i <input0> -i <input1> -c copy -map 0:0 -map 1:1 -shortest <output>

Transcoding

List available encoders and decoders.

ffmpeg -encoders
ffmpeg -decoders

Decode input.

ffmpeg -c:a <codec> -c:v <codec> -i <input> <output>

Encode output.

ffmpeg -i <input> -c:a <codec> -c:v <codec> <output>

x264

This section focuses on the x264 transcoder.

Encode a video.

ffmpeg -i <input> -preset <preset> -crf <crf> <output>

Preset.

ultrafast
superfast
veryfast
faster
fast
medium – default preset
slow
slower
veryslow
placebo – ignore this as it is not useful

You can see a list of current presets with -preset help. If you have the x264 binary installed, you can also see the exact settings these presets apply by running x264 --fullhelp.

NVENC/NVDEC

This section focuses on the NVIDIA transcoding.

Encode a video.

ffmpeg -vsync 0 -hwaccel cuvid -hwaccel_device 0 -c:v h264_cuvid -i <input> -c:a copy -c:v h264_nvenc -b:v 5M <output>

Editing

Trim

ffmpeg -ss <start> -t <duration> -i <input> -c copy <output>

Loop

Loop a video n times.

ffmpeg -stream_loop <n> -i <input> -c copy <output>

Speed Up/Slow Down

This section describes how to change the speed of a stream.

Change the speed of a video stream using the setpts video filter (requires re-encoding).

ffmpeg -i <input> -vf "setpts=1.0*PTS" <output>

Change the speed of an audio stream using the atempo audio filter.

ffmpeg -i <input> -af "atempo=1.0" <output>

Delay

Delay audio.

ffmpeg -i <input> -itsoffset <time> -i <input> -map 0:v -map 1:a -c:v copy -c:a copy <output>

Delay video.

ffmpeg -i <input> -itsoffset <time> -i <input> -map 1:v -map 0:a -c:v copy -c:a copy <output>

Volume

Increase audio volume.

ffmpeg -i <input> -af "volume=1.5" -c:v copy <output>
ffmpeg -i <input> -af "volume=10dB" -c:v copy <output>

Mute audio in an interval.

ffmpeg -i <input> -af "volume=enable='between(t,5,10)':volume=0" -c:v copy <output>

Rotate

Rotate 90 degrees clockwise.

ffmpeg -i <input> -vf "transpose=1" <output>

Rotate 180 degrees clockwise.

ffmpeg -i <input> -vf "transpose=2,transpose=2" <output>

Transpose.

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

Scale

Resize a video.

ffmpeg -i <input> -s <resoluton> -c:a copy <output>

Scale a video.

ffmpeg -i <input> -c:a copy -vf scale=720:-1 <output>

Some codecs, like libx264 requires even values, odd values result in the error width or height not divisible by 2. To scale with a given width or height value use scale="1280:trunc(ow/a/2)*2" or scale="trunc(oh*a/2)*2:720".

Slice

Extract all the input frames.

ffmpeg -i <input> out%d.png

Extract a specified number of frames per second.

ffmpeg -i <input> -r <fps> out%d.png

Extract frames at a specific time.

ffmpeg -i <input> -ss <time> -vframes <frames> out%d.png

Create a video slideshow from images.

ffmpeg -r <ips> -i img%03d.png -c:v libx264 -vframes <fps> -pix_fmt <format> <output>

GIF

This section describes how to create a high quality GIF.

Create a GIF using color palettes.

ffmpeg -i <input> -filter_complex "[0:v] palettegen" palette.png
ffmpeg -i <input> -i palette.png -filter_complex "[0:v][1:v] paletteuse" out.gif

ffmpeg -i <input> -vf "split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" out.gif

ffmpeg -i <input> -vf "split[s0][s1];[s0]palettegen=stats_mode=single[p];[s1][p]paletteuse=new=1" out.gif

Draw

This section has examples of the video filter drawtext.

Draw frame number and timestamp.

ffplay -i <input> -vf "drawtext=text='frame %{frame_num}%{pts\:hms}': x=(w-tw)/2: y=h-(2*lh): fontcolor=white: fontsize=20: box=1: boxcolor=black: boxborderw=5"

Metadata

Remove metadata.

ffmpeg -i <input> -map_metadata -1 <output>

Streaming

This section has examples of the supported transport protocols.

RTP

Play a stream.

ffmpeg -protocol_whitelist file,udp,rtp -i <sdp> <output>
ffplay -i <sdp> -protocol_whitelist file,udp,rtp

ffmpeg -protocol_whitelist rtp,udp -i "rtp://<ip>:<port>" <output>
ffplay -protocol_whitelist rtp,udp -i "rtp://<ip>:<port>"

Serve a stream.

ffmpeg -re -thread_queue_size 4 -i <input> -strict 2 -acodec copy -vn -f rtp rtp://<ip>:<audio_port> -vcodec copy -an -f rtp rtp://<ip>:<video_port> -sdp_file <sdp>

Serve an RTP MPEG-TS stream.

ffmpeg -re -i <input> -c:v copy -c:a copy -f rtp_mpegts -sdp_file <sdp> "rtp://<ip>:<port>"

RTSP

Play a stream.

ffplay -i rtsp://@<ip>:<port>/<path/to/stream>.<sdp|mp4|mov|avi|etc>

Record to file.

ffmpeg -i rtsp://@<ip>:<port>/<path/to/stream>.<sdp|mp4|mov|avi|etc> -acodec copy -vcodec copy <output>

RTMP

Publish a stream.

ffmpeg -re -i <input> -acodec copy -vcodec copy -f <flv|webm|etc> rtmp://<ip>:1935/<application>/<stream_name>/<stream_key>

Play a stream.

ffplay -i rtmp://<ip>:1935/<application>/<stream_name>
ffplay -i "rtmp://<ip>:1935/<application>/<stream_name> live=1"

Record to file.

ffmpeg -i rtmp://<ip>:1935/<application>/<stream_name> -acodec copy -vcodec copy <output>

HLS

Play a stream.

ffplay -i http://<ip>:<port>/<application>/<stream_name>.m3u8

Download a stream.

ffmpeg -protocol_whitelist file,http,https,tcp,tls -i http://<ip>:<port>/<application>/<stream_name>.m3u8 -c copy -bsf:a aac_adtstoasc <output>

Convert an input to .m3u8 list and .ts packets.

ffmpeg -i <input> -codec: copy -start_number 0 -hls_time 10 -hls_list_size 0 -f hls output.m3u8

DASH

Play a stream.

ffplay -i http://<ip>:<port>/<application>/<stream_name>.mpd

SRT

🚩 FFmpeg needs to be compiled with --enable-libsrt.

Listen for connections to send a stream.

ffmpeg -i <input> srt://:9710?mode=listener
ffmpeg -i <input> srt://:9710?mode=listener&latency=500
ffmpeg -i <input> -c:v libx264 -f mpegts srt://:9710?mode=listener&latency=500

Play a stream.

ffplay -i srt://192.168.1.1:9710?mode=caller

Receive a stream.

ffmpeg -i srt://192.168.1.1:9710?mode=caller <output>

NDI

🚩 NDI removed from FFmpeg due to a license violation.

List the available NDI sources.

ffmpeg -f libndi_newtek -find_sources 1 -i dummy

Play a NDI stream.

ffplay -f libndi_newtek -i "NDI Input Stream"

Stream an input to a NDI stream.

ffmpeg -re -i <input> -f libndi_newtek -pix_fmt uyvy422 "NDI Output Stream"

Stream a NDI stream to an output.

ffmpeg -f libndi_newtek -i "NDI Input Stream" <output>

Synchronization

Synchronize audio and video.

ffmpeg -vsync drop -fflags +discardcorrupt -i <input> <output>
ffplay -sync ext -fflags +discardcorrupt -framedrop -i <input>

Video sync method vsync.

Latency

Minimize live stream latency.

ffmpeg -fflags nobuffer -flags low_delay -reorder_queue_size 0 -i <input> <output>
ffplay -i <input> -fflags nobuffer -flags low_delay -reorder_queue_size 0

Devices

This section describes the input and output devices provided by the libavdevice library.

DirectShow

Windows DirectShow input devices.

List devices.

ffmpeg -list_devices true -f dshow -i dummy

List options.

ffmpeg -list_options true -f dshow -i audio="Microphone"
ffmpeg -list_options true -f dshow -i video="Camera"

Play camera.

ffplay -f dshow -i audio="Microphone":video="Camera"

Record to file.

ffmpeg -f dshow -i audio="Microphone":video="Camera" <output>
ffmpeg -f dshow -video_size <resolution> -framerate <fps> -pixel_format <pixel_format> -i video="Camera" <output>
ffmpeg -f dshow -video_size 1280x720 -framerate 30 -pixel_format yuyv422 -i video="Camera" output.mp4

Options

Option Description
-an Blocks all audio streams from being filtered or being automatically selected or mapped for any output.
-vn Blocks all video streams from being filtered or being automatically selected or mapped for any output.
-sn Blocks all subtitle streams from being filtered or being automatically selected or mapped for any output.
-codec:a
-c:a
-acodec
Select the encoder or decoder for the audio stream.
-codec:v
-c:v
-vcodec
Select the encoder or decoder for the video stream.
-filter:a
-af
Create an audio filtergraph.
-filter:v
-vf
Create a video filtergraph.
-r Sets the frame rate.
-re Reads input at native frame rate. By default ffmpeg attempts to read the input as fast as possible.
-f Forces input or output file format. The format is normally auto detected for input files and guessed from the file extension for output files, so this option is not needed in most cases.