Skip to content

Instantly share code, notes, and snippets.

@oleoneto
Last active June 3, 2022 15:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleoneto/6d8ff05b3f5e7e5d52934be7d879d684 to your computer and use it in GitHub Desktop.
Save oleoneto/6d8ff05b3f5e7e5d52934be7d879d684 to your computer and use it in GitHub Desktop.

FFmpeg Cheatsheet

Generic flags

Flag What Example
-s size
-c code
-f format
-v verbosity
-r framerate
-t duration (seconds)
-metadata key=value
-frames:v limit number of frames
-an no audio
-ar sample rate
-vn no video
-y overwride existing video
-pix_fmt accommodate colorspace

Grap 'n' Copy Live Audio Stream

Use the .mkv extension to allow for fail-safe recordings. Check this for more information.

ffmpeg -i http://example.com/radio.mp3 -c copy /path/to/output/file.mkv

Forward an online audio stream to an RTP server

ffmpeg -re -i http://example.com/radio.mp3 -ar [sample_rate] -f [codec] -c:a copy -f rtp rtp://127.0.0.1:1234

An example sample_rate is 44100. An example codec is pcm_s16le.


Convert between file types

ffmpeg -i input.avi output.mp4

Change video quality

AVI files:

ffmpeg -i <input.avi> -q <quality> <output>

MP4 files:

ffmpeg -i <input.mp4> -crf <quality> <output>

Change bitrates:

Audio

ffmpeg -i <input> -b:a <bitrate> <output>

Video:

ffmpeg -i <input> -b:v <bitrate> <output>

Audio and Video

ffmpeg -i <input> -b:a <bitrate> -b:v <bitrate> <output>

Tweak Volume

ffmpeg -i <input> -filter:a "volume=<multiplier>" <output>

Add Watermark

ffmpeg -i <input.mp4> -i <watermarl.jpg> -filter_complex "overlay=x:y" <output>

Live Audio Playback

ffplay http://myradio.com/stream.mp3

Channel Remapping

# config could is: <left input>-<left output>|<right input>-<right output>
# i.e. 0-0|0-1
ffmpeg -i <input> -filter:a "channelmap=<config>" <output>

Switch left channel to right and right channel to left with the channelmap audio filter:

ffmpeg -i <stereo.mp3> -filter_complex "channelmap=map=FL-FR|FR-FL:channel_layout=stereo" <output.mp3>

Or with the pan audio filer:

ffmpeg -i <stereo.ogg> -af pan=stereo|c0=c1|c1=c0 <output.wav>

Or with -map_channel:

ffmpeg -i <stereo.ogg> -map_channel 0.0.1 -map_channel 0.0.0 <output.wav>

Switch stereo channels

Create a stereo output from one mono input:

ffmpeg -i <input.mp3> -ac 2 <output.m4a>

Create a stereo output from two mono inputs with the join audio filter:

ffmpeg -i <left.mp3> -i <right.mp3> -filter_complex "[0:a][1:a]join=inputs=2:channel_layout=stereo[a]" -map "[a]" <output.mp3>

or

ffmpeg -i left.mp3 -i right.mp3 -filter_complex "[0:a][1:a]amerge=inputs=2[a]" -map "[a]" output.mka

mono -> stereo

Combine 6 mono inputs into one 5.1 (6 channel) output with the join audio filter:

ffmpeg -i <front_left.wav> \
  -i <front_right.wav> \
  -i <front_center.wav> \
  -i <lfe.wav> \
  -i <back_left.wav> \
  -i <back_right.wav> \
  -filter_complex "[0:a][1:a][2:a][3:a][4:a][5:a]join=inputs=6:channel_layout=5.1[a]" \
  -map "[a]" <output.wav>

6xmono -> 5.1

Split a 5.1 channel input into individual per-channel files using the channelsplit audio filter:

ffmpeg -i <input.wav> \
  -filter_complex "channelsplit=channel_layout=5.1[FL][FR][FC][LFE][BL][BR]" \
  -map "[FL]" front_left.wav \
  -map "[FR]" front_right.wav \
  -map "[FC]" front_center.wav \
  -map "[LFE]" lfe.wav \
  -map "[BL]" back_left.wav \
  -map "[BR]" back_right.wav

5.1 -> 6xmono

Combine two stereo inputs into one stereo output with the amerge and pan audio filters:

ffmpeg -i <stereo1.wav> \
  -i <stereo2.wav> \
  -filter_complex "[0:a][1:a]amerge=inputs=2,pan=stereo|c0<c0+c2|c1<c1+c3[a]" \
  -map "[a]" <output.mp3>

Or use -ac 2 instead of the pan audio filter:

ffmpeg -i <stereo1.wav> \
  -i <input2.wav> \
  -filter_complex "[0:a][1:a]amerge=inputs=2[a]" \
  -map "[a]" \
  -ac 2 <output.mp3>

2xstereo -> stereo


Capturing your Desktop / Screen Recording

Official Documentation

macOS

Use the avfoundation device:

ffmpeg -f avfoundation -list_devices true -i ""

# Expected output:
ffmpeg version 4.3 Copyright (c) 2000-2020 the FFmpeg developers
  ...
[AVFoundation indev @ 0x7f8349006000] AVFoundation video devices:
[AVFoundation indev @ 0x7f8349006000] [0] FaceTime HD Camera (Built-in)
[AVFoundation indev @ 0x7f8349006000] [1] Capture screen 0
[AVFoundation indev @ 0x7f8349006000] AVFoundation audio devices:
[AVFoundation indev @ 0x7f8349006000] [0] Loopback Audio
[AVFoundation indev @ 0x7f8349006000] [1] Built-in Microphone

This will enumerate all the available input devices including screens ready to be captured. Once you've figured out the device index corresponding to the screen to be captured, use:

ffmpeg -f avfoundation -i "<screen device index>:<audio device index>" output.mkv

This will capture the screen from and audio from into the output file output.mkv.

Use .mkv so you don't lose the file even if the application crashes.

Lossless Recording

If your CPU is not fast enough, the encoding process might take too long. To speed up the encoding process, you can use lossless encoding and disable advanced encoder options, e.g.:

ffmpeg -video_size 1920x1080 -framerate 30 -f x11grab -i :0.0 -c:v libx264rgb -crf 0 -preset ultrafast output.mkv

-crf 0 tells x264 to encode in lossless mode; -preset ultrafast advises it to do so fast. Note the use of libx264rgb rather than libx264; the latter would do a lossy conversion from RGB to yuv444p.

Streaming

Streaming Guide

Streaming to an RTMP server (MacOS)

ffmpeg -f avfoundation -i 2:0 -c:v libx264 -preset veryfast -tune zerolatency -f flv rtmp://[$host][$streamPath]

You can also grab the contents of a livestream with the following command:

ffmpeg -re -i http://[$host][$streamPath] -c copy -f FORMAT output.mkv

Point-to-point streaming over RTP

ffmpeg -i INPUT -acodec libmp3lame -ar 11025 --f rtp rtp://host:port

Rebroadcast a live source over RTP

ffmpeg -re -i http://[$host][$streamPath] -ar [44100] -f FORMAT -f rtp rtp://[$host]:[$port]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment