Skip to content

Instantly share code, notes, and snippets.

@Eddy-Barraud
Last active September 19, 2023 07:53
Show Gist options
  • Save Eddy-Barraud/901463021a00486e3005a2cf99ad4e84 to your computer and use it in GitHub Desktop.
Save Eddy-Barraud/901463021a00486e3005a2cf99ad4e84 to your computer and use it in GitHub Desktop.
ffmpeg commands reminder

Print videos informations and streams

For all informations : ffmpeg -i input.mkv

For informations on streams : ffprobe -v error -show_entries stream=index,codec_name,codec_type input.mkv

[STREAM]
index=0
codec_name=h264
codec_type=video
[/STREAM]
[STREAM]
index=1
codec_name=ac3
codec_type=audio
[/STREAM]

Convert video with x264 encoder

ffmpeg -i input -c:v libx264 -crf 23 -maxrate 1M -bufsize 2M output.mp4

0<crf<51 (default: 23) (lossless: 0)

To only change video bitrate/quality but keep all streams (audios,subs) :

ffmpeg -i input.avi -map 0 -c:v libx264 -b:v 3M -maxrate 4M -bufsize 5M -crf 18 -c:a copy -c:s copy output.mkv

Easy way --> choose a preset

ffmpeg -i input.mp4 -c:v libx264 -preset superfast output.mp4

Optimize video for fast streaming :

ffmpeg -i INPUT.mp4 -c copy -movflags +faststart STREAMABLE_OUTPUT.mp4

Using a bitrate range and a preset :

ffmpeg -i input.avi -map 0 -c:v libx264 -b:v 3M -maxrate 4M -bufsize 5M -preset fast -movflags +faststart -c:a copy -c:s copy output.mkv

Using a bitrate range and a good CRF :

ffmpeg -i input -map 0 -c:v libx264 -b:v 3M -maxrate 4M -bufsize 10M -crf 22 -movflags +faststart -c:a copy -c:s copy output

Using VP9 encoder

ffmpeg -i input.mp4 -c:v libvpx-vp9 -minrate 1M -b:v 3M -maxrate 4M -crf 30 -movflags +faststart -row-mt 1 output.webm

Using 2 pass HEVC encoder

ffmpeg -y -i $file -c:v libx265 -b:v 3M -x265-params pass=1 -an -f mp4 /dev/null && \
ffmpeg -y -i $file -c:v libx265 -b:v 3M -x265-params pass=2 -c:a aac -b:a 128k $(basename -s .mkv $file).opt.mp4

Split video to images

ffmpeg -i video.flv image%d.jpg

Extract mp3 from video

ffmpeg -i video1.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 audio3.mp3

Crop a video without re-encoding

ffmpeg -i input -ss 00:31:00 -to 00:41:21 -c copy output

Create a black screen video with only audio

ffmpeg -i input.mp3 -f lavfi -i color=color=black:size=640x480:rate=24 -map 1 -map 0:a -c:a copy -shortest output.mp4

other links :

ffmpeg official website :

Hardware acceleration :

List availables : ffmpeg -hwaccels -hide_banner

Recommended for decoding : ffmpeg -hwaccel d3d11va

List available codecs : ffmpeg -codecs

Recommended NVidia accelerated codec : ffmpeg -i input -v:c h264_nvenc output

Fast lossless optimization :

ffmpeg -hwaccel d3d11va -threads 16 -i input -c:v h264_nvenc -rc:v vbr_hq -cq:v 19 -b:v 5000k -maxrate:v 12500k -profile:v high output

Controled bitrate and high quality

ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i input -c:v h264_nvenc -preset slow -tune hq -b:v 5M -bufsize 5M -maxrate 10M -qmin 0 -g 250 -bf 3 -b_ref_mode middle -temporal-aq 1 -rc-lookahead 20 -i_qfactor 0.75 -b_qfactor 1.1 output

Using ffmpeg Filters

Concatenate videos

For video only (w/o audio)

ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex "[0:0][1:0]concat=n=2:v=1:a=0" 1and2.mp4

For both, video and audio

ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex "[0][1]concat=n=2:v=1:a=1" 1and2.mp4

Sometimes it's necesary to change SAR to 1:1, just use setsar=1 filter. To do a simple copy and avoid re-encode :

$ cat list
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'

$ ffmpeg -f concat -safe 0 -i list -c copy output.mp4

Crop a rectangle inside a video

ffmpeg -i full.mp4 -filter:v 'crop=width:height:x:y' cropped.mp4

Transform a vertical video (smartphone) to horizontal video with blur

9:16 --> 16:9

Using ffmpeg filters. 1st, split videos, crop and blur the background from original, then overlay original and rescale

Dot notation :

                [original]
 input --> split -------------------------------> overlay --> output
        │                                          ^
        │[copy]                           [blurred]│
        └──────> crop ──> scale ──> gblur ─────────┘

Source : https://stackoverflow.com/a/54618683

ffmpeg -i vertical.mp4 -vf 'split[original][copy];[copy]scale=ih*16/9:-1,crop=h=iw*9/16,gblur=sigma=20,eq=brightness=-0.1[blurred];[blurred][original]overlay=(W-w)/2:(H-h)/2,scale=1920:1080' horizontal.mp4

Note : CPU intensive !

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