Skip to content

Instantly share code, notes, and snippets.

@AffanIndo
Last active Nov 30, 2019
Embed
What would you like to do?
FFmpeg Cheat Sheet
# batch convert
for i in *.mp4; do ffmpeg -i "$i" "${i%.mp4}.webm"; done
# convert from one format to another
ffmpeg -i input.mp3 output.wav
# convert from one video format to another ("-qscale" value are 0-10, 0 is the best)
ffmpeg -i input.mp4 -qscale 0 output.webm
# convert video to audio (sometimes this one is better than the custom one below)
ffmpeg -i input.mp4 output.mp3
# convert video to audio (use 128000, 192000, 256000, 320000, or 410000)
ffmpeg -i input.mp4 -b:a 410000 output.mp3
# extract frames from a video
ffmpeg -i input.mp4 output%d.jpg
# convert video to gif ("-r" = frame/milisecond, "-delay" = delay to next image)
# normal speed:
ffmpeg -i input.mp4 -vf scale=320:-1 -r 10 -f image2pipe -vcodec ppm - | convert -delay 10 -loop 0 - output.gif
# 2x speed:
ffmpeg -i input.mp4 -vf scale=320:-1 -r 10 -f image2pipe -vcodec ppm - | convert -delay 5 -loop 0 - output.gif
# 1/2 frame 2x speed:
ffmpeg -i input.mp4 -vf scale=800:-1 -r 5 -f image2pipe -vcodec ppm - | convert -delay 10 -loop 0 - output.gif
# batch:
for i *.mp4; do ffmpeg -i "$i" -vf scale=320:-1 -r 10 -f image2pipe -vcodec ppm - | convert -delay 10 -loop 0 - "${i%.mp4}.gif"; done
# convert gif to video
ffmpeg -i input.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" output.mp4
# joining
# create file list:
for f in ./*.mp4; do echo "file '$f'" >> mylist.txt; done
# concat ("-safe 0" is optional if using relative path):
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
# replace audio in a video (add -shortest if the audio is longer than the video, and vice versa)
# (optionally, specify audio codec by adding "-c:a codecname" after "-c:v copy")
ffmpeg -i input.mp4 -i input.mp3 -c:v copy -map 0:v:0 -map 1:a:0 output.mp4
# cut ("-ss" = start time, "-t" = duration, use them with time e.g. 00:01:23.000 or 83)
ffmpeg -ss [start] -i input.mp4 -t [duration] -c copy output.mp4
# alternatively, use "-to" instead of "-t" if you want to specify end time instead of duration
ffmpeg -i input.mp4 -ss [start] -to [end] -c copy output.mp4
# cutting first few seconds (with re-encode)
ffmpeg -ss [start] -i input.mp4 output.mp4
# compress (use mp2, mp3, or aac for "-acodec")
ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 output.mp4
# mute audio
ffmpeg -i input.mp4 -c copy -an output-nosound.mp4
# mute audio (without re-encode)
ffmpeg -i input.mp4 -vcodec copy -an output-nosound.mp4
# time-lapse (remove silent audio track first, or shorten it with "-an")
ffmpeg -i input.mp4 -an -filter:v "setpts=0.5*PTS" output.mp4
# 60 fps time-lapse from 3 fps (or any small numbered fps) video
ffmpeg -i input.mp4 -an -filter:v "setpts=0.5*PTS" -r 60 output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment