Skip to content

Instantly share code, notes, and snippets.

@city96
Created February 28, 2023 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save city96/8e20c367ba25327fe737486a33c2e4dc to your computer and use it in GitHub Desktop.
Save city96/8e20c367ba25327fe737486a33c2e4dc to your computer and use it in GitHub Desktop.
FFmpeg video to/from image sequence

FFmpeg video to/from image sequence

Placeholders:

  • raw.mp4 - Your input video
  • out.mp4 - Your output video
  • -r 30 - The framerate of your videos.
  • %04d - The format of your image sequence.
  • images - The folder with your image sequence

Video -> Image sequence

Dump all video frames into a folder.

Set the number of zeroes as needed (%04d->0001.png,%06d->000001.png, etc).

ffmpeg -i raw.mp4 images\%04d.png

Image sequence -> video [lossless]

For further processing / as input for other software.

ffmpeg -r 30 -i images\%04d.png -c:v libx264 -crf 15 out.mp4

Image sequence -> video [lossless+audio]

Same as above, but take the audio track from the original video.

Replace raw.mp4 to use different audio track source.

ffmpeg -i raw.mp4 -r 30 -i images\%04d.png -map 0:a:0 -map 1:v:0 -c:v libx264 -crf 15 out.mp4

Image sequence -> video [lossy+audio]

Useful for exporting your video. Adjust quality by changing -crf 21.

Higher values = worse video, sensible values are 18-32.

ffmpeg -i raw.mp4 -r 30 -i images\%04d.png -map 0:a:0 -map 1:v:0 -c:v libx264 -pix_fmt yuv420p -crf 21 out.mp4

Image sequence -> webm video [lossy+audio]

Change -b:v 2M to match the filesize you need. Formula is [target size in MB]/[video length in seconds]*8.

You can also just use -crf like the command above.

See the wiki for more options.

ffmpeg -i raw.mp4 -r 30 -i images\%04d.png -map 0:a:0 -map 1:v:0 -c:a libvorbis -c:v libvpx-vp9 -pix_fmt yuv420p -b:a 96K -b:v 2M out.webm

Image sequence -> video [alpha to background color]

Replace transparency with a specific color:

Can't find the stackoverflow answer I got parts of this from.

Adjust resolution/framerate for the color plane.

ffmpeg -f lavfi -i color=c=white:s=768x768:r=30 -r 30 -i images\%04d.png -filter_complex "[0:v][1:v]overlay=shortest=1,format=yuv420p[out]" -map "[out]" -c:v libx264 -crf 15 out.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment