Skip to content

Instantly share code, notes, and snippets.

@unicornist
Last active August 25, 2021 17:26
Show Gist options
  • Save unicornist/5cee802090eaffba13780fcb1a2e2b1a to your computer and use it in GitHub Desktop.
Save unicornist/5cee802090eaffba13780fcb1a2e2b1a to your computer and use it in GitHub Desktop.

Convert Video

Just Convert Format:
ffmpeg -i input.avi -c:v libx265 output.mp4
ffmpeg -i input.avi -c:v libx264 output.mp4
ffmpeg -i input.mp4 -c:v flv output.flv
Convert to H265 with smaller size and medium quality:
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -vf "scale=iw/2:ih/2" -c:a copy output.mp4
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -vf "scale=720:-1" -c:a copy output.mp4
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -vf "scale=720:trunc(ow/a/2)*2" -c:a copy output.mp4
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -vf "scale=trunc(oh*a/2)*2:720" -c:a copy output.mp4

Trim Video without Re-Encoding

ffmpeg -i input.mp4 -ss 3:45 -t 1:21 -c copy output.mp4

Convert Any Audio to MP3-128k

ffmpeg -i input.wav -ab 128 output.mp3

Extract All frames of Video

Fast / Lossless / Space-hogger:
ffmpeg -i input.mp4 -r 1/1 $filename%03d.bmp
Slow / Lossy / Space-efficient:
ffmpeg -i input.mp4 -r 1/1 $filename%03d.jpg
SuperSlow / Lossless / Space-efficient:
ffmpeg -i input.mp4 -r 1/1 $filename%03d.png

Extract Nth frame of Video

ffmpeg -i input.mp4 -vf "select=eq(n\,34)" -vframes 1 output.png

Add a Black Box/Rectangle to Video

ffmpeg -i input.mp4 -vf "drawbox=x=iw-320:y=ih-240:w=320:h=240:color=black:t=fill" output.mp4

Windows Notes:

  • you need %%, instead of %03d
  • don't use the quotes around -vf option's value, e.g. select=....
  • for drawbox filter use t=max if you are on an older ffmpeg version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment