It is often convenient to display a set of images as a video, to quickly view a large set of images, to embed in a presentation, or otherwise communicate. A set of images may have a natural order, or be better viewed randomly.
This guide illustrates how to create a video from a directory of images using the open-source command-line tool ffmpeg, with preprocessing steps via Anchor.
ffmpeg has particular requirements to convert images to a video:
- Image filenames contain an integer sequence (zero padded) e.g.
- Images are 2D.
- Images have identical width, height and bit-rate.
If these conditions are already true, no preprocessing is needed, and one can skip to the final section of the tutorial.
Otherwise, Anchor can be used to transform the images accordingly.
Outputting the image sequence
-on command-line option will ensure all files are outputted in a sequence.
To write images in a automatically-created subdirectory of a parent:
anchor -i path_to_input_directory/ -t copy -o path_to_parent_output_directory/ -on
To write images in a newly-created specific output directory:
anchor -i path_to_input_directory/ -t copy -oo path_to_specific_output_directory/ -on
Randomizing the image order
To add randomized order to images, add the
-is command-line option to shuffle the inputs.
anchor -i path_to_input_directory/ -is -t copy -oo path_to_specific_output_directory/ -on
Images with different sizes
With images of varying sizes, use the
resize predefined task instead of
copy, setting the desired size with the -ps option.
anchor -i path_to_input_directory/ -t center -ps 800x600 -oo path_to_specific_output_directory/ -on
Creating the video with ffmpeg
If not already installed:
- Download ffmpeg.
- Unzip to a convenient location on the file-system.
bin/directory to the system
PATHvariable, so that it can be called on a shell as
Encoding the images as a video
Open a shell in the images directory. See how to run a predefined task for tips on how to do this.
ffmpeg with a command similar to the following:
ffmpeg -framerate 2 -i img%03d.png -c:v libx264 -vf fps=2 -pix_fmt yuv420p out.mp4
Change the pattern in
-i img%03d.pngto reflect the filename pattern, replacing the
3with the width of numeric sequence (the total number of digits including leading zeros to describe the number).
-framerateoption to the number of frames per second, to control the speed of the video.
-vf fps=it is recommended to use the same number as
>=1. Otherwise simply use
mp4format (or a higher number to be less jumpy, but create a larger file size).
out.mp4to the desired output-path and format type of the created video e.g.
framerate >=1shows one or more images every second. A
framerate <1shows each image for longer than a second e.g.
-framerate 1/4gives 4 seconds per image.
.wmvfiles are friendly to use in Microsoft Powerpoint, but is problematic with a
-framerateoption should always be placed to the left of the
-ioption in the command.
ffmpeg is very sensitive to parameter changes, and can be buggy, so please see its Slideshow documentation to try different parameters if undesired video output occurs.