How to create a video from images using FFmpeg?

Is it possible to use ffmpeg create a video from a set of sequences, where the number does not start from zero?

For example, I have some images [test_100.jpg, test_101.jpg, test_102.jpg, ..., test_200.jpg], and I want to convert them to a video. I tried the following command, but it didn't work (it seems the number should start from zero):

ffmpeg -i test_%d.jpg -vcodec mpeg4 test.avi

Any advise?


Solution 1:

There is no need to rename files if using the -start_number switch like so:

ffmpeg -start_number n -i test_%d.jpg -vcodec mpeg4 test.avi

where n is the start of the sequence of stills.

Note, this will work as long as the sequence is unbroken once it starts. If there are gaps and you want all of the stills included, then renumbering may be necessary to fill the gaps.

There are some other switches you might find useful.

I use the following one-liner to get a slower frame rate and to compress the images and have a smaller resulting video:

ffmpeg.exe -f image2 -framerate 25 -pattern_type sequence -start_number 1234 -r 3 -i Imgp%04d.jpg -s 720x480 test.avi

The -r 3 option sets the framerate of the resulting video to 3 frames per second so that I can see each still for a short period of time. The -s option rescales the pictures to the desired resolution to manage the size of the resulting video.

(In the Windows shell, replace -i Imgp%04d.jpg with -i "Imgp%%04d.jpg". Credit for this to Mike Fitzpatrick https://superuser.com/a/344178/153054)

Solution 2:

you can use this below code snippet:

 cat *.jpg | ffmpeg -f image2pipe -r 1 -vcodec mjpeg -i - -vcodec libx264 out.mp4

Solution 3:

From ffmpeg's docs:

Using a glob pattern

ffmpeg also supports bash-style globbing (* represents any number of any characters).

This is useful if your images are sequential but not necessarily in a numerically sequential order as in the previous examples.

ffmpeg -r 1 -pattern_type glob -i 'test_*.jpg' -c:v libx264 out.mp4

So, as long as your files are sorted, using the -pattern_type glob switch should work for you.

Solution 4:

You can find an example script in the ffmpeg documentation:

3.2 How do I encode single pictures into movies?

If you have large number of pictures to rename, you can use the following command to ease the burden. The command, using the bourne shell syntax, symbolically links all files in the current directory that match *jpg to the /tmp' directory in the sequence of img001.jpg', `img002.jpg' and so on.

x=1; for i in *jpg; do counter=$(printf %03d $x); ln "$i" /tmp/img"$counter".jpg; x=$(($x+1)); done

Then run:

ffmpeg -f image2 -i /tmp/img%03d.jpg /tmp/a.mpg

Solution 5:

As far as I know, you cannot start the sequence in random numbers (I don't remember if you should start it at 0 or 1), plus, it cannot have gaps, if it does, ffmpeg will assume the sequence is over and stop adding more images.

Also, as stated in the comments to my answer, remember you need to specify the width of your index. Like:

image%03d.jpg

And if you use a %03d index type, you need to pad your filenames with 0, like :

image001.jpg image002.jpg image003.jpg

etc.