Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A working cheat sheet for ffmpeg
#use case: converting image sequence to .mp4 video
#official documentation:
# An image sequence follows the pattern, defaultCanvas0-000000000.png (9 zeros)
# video codec is libx264
# pixel format yuv420p
# output file is named out.mp4
ffmpeg -i 'defaultCanvas0-%9d.png' -c:v libx264 -pix_fmt yuv420p out.mp4
#if you want your image sequence to start on a different number than index 0, use the -start_number parameter
ffmpeg -start_number 000000060 -i 'defaultCanvas-%9d.png' -c:v libx264 -pix_fmt yuv420p out.mp4
#if you want to use ffmpeg to generate an animated gif from an image sequence
# -vf : video filter
#scale=[pixels] : -1 scale the frame and preserve aspect ratio. In this case we are scaling the width to 500px
# if you want to scale the height and preserve aspect ratio, scale=-1:500
ffmpeg -start_number 000000090 -i 'defaultCanvas0-%9d.png' -vf scale=500:-1 out.gif
#more info on high quality gifs with ffmpeg
#another helpful tool for creating animated gifs with frame control is imageMagick:
# Making an Image Sequence from a video - from
ffmpeg -i video.webm image-%03d.png
#This will extract 25 images per second from the file video.webm and save them as image-000.png, image-001.png, image-002.png up to image-999.png. If there are more than 1000 frames then he last image will be overwritten with the remaining frames leaving only the last frame.
#The encoding of the images and of the video is inferred from the extensions. The framerate is 25 fps by default. The images width and height is taken from the video.
#Extract one image per second:
ffmpeg -i video.webm -vf fps=1 image-%03d.png
#Extract one image from a specific time:
ffmpeg -i video.webm -ss 00:00:10 -vframes 1 thumbnail.png
#looping through directory of gifs, resizing and converting each to mp4
echo "looping through all files in $WD"
#try replacing * with *.mov, to filter by file extension
for f in *.gif; do
echo "$f"
ffmpeg -stream_loop 2 -i "$f" -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" "looped-$f" -y;
ffmpeg -i "looped-$f" -pix_fmt yuv420p -vf "scale='max(600,iw)':-1" "$f".mp4 -y
rm "looped-$f"
echo "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment