ffmpeg -pattern_type glob Not Loading Files in Correct Order

ffmpegnumberingwildcards

I have a dilemma..
I've had a script for a while now that downloads pictures from a webcam every few minutes.
The naming convention just does new_image((len(files(dir))+1) + '.jpg') and that's all fine.. until today..

I've had a Python script that loads the images based on creation date and renders them and i take the OpenGL data and dump it into a movie, which isn't hard and it's all ok.. Except now i have a few thousand images and it's quite ineffective to go on about it this way (even tho it's cool because i can build my own GUI and overlay etc).. anyway, i'm using ffmpeg to combine the images into a slideshow, like so:

ffmpeg -f image2 -r 25 -pattern_type glob -i '*.jpg' -qscale 3 -s 1920x1080 -c:v wmv1 video.wmv

The ffmpeg works fine, except that -pattern_type glob takes the images in naming order which doesn't work because the way the files get fed is similar if not the same as ls, which looks like:

user@host:/storage/photos$ ls
0.jpg     1362.jpg  1724.jpg  2086.jpg  2448.jpg  280.jpg   3171.jpg  3533.jpg  3896.jpg  4257.jpg  4619.jpg  4981.jpg  5342.jpg  5704.jpg  6066.jpg  650.jpg
1000.jpg  1363.jpg  1725.jpg  2087.jpg  2449.jpg  2810.jpg  3172.jpg  3534.jpg  3897.jpg  4258.jpg  461.jpg   4982.jpg  5343.jpg  5705.jpg  6067.jpg  651.jpg

0, 1000, 1, 2000, 2… this is the logic of ls so the image sequence (timelapse) will be all f-ed up..

Any ideas on how to use ffmpeg to load the images in a more sorted manner?

Best Answer

ffmpeg concat same file types

You could use a command like this to concatenate the list of files any way you want:

ffmpeg -f concat -i <( for f in *.wav; do echo "file '$(pwd)/$f'"; done ) \
         output.wav

The above can only work if the files you're concatenating are all the same codecs. So they'd all have to be .wav or .mpg for example.

NOTE: You need to have ffmpeg v1.1 or higher to use the concat demuxer, you can read more about the above example and also how to concatenate different codecs using this technique on the ffmpeg website.

ffmpeg using printf formatters

Ffmpeg can also take input using the printf formatters such as %d. This matches digits starting at 0 and going up from there in order. If the numbers were structured like this, 000 - 099, you could use this formatter, %03d, which means a series of 3 digits, zero padded.

So you could do something like this:

ffmpeg -r 25 -i %d.png -qscale 3 -s 1920x1080 -c:v wmv1 video.wmv

The above didn't quite work for me, ffmpeg was complaining about the option -c:v. I simply omitted that option and this version of the command worked as expected.

ffmpeg -r 25 -i %d.png -qscale 3 -s 1920x1080 video.wmv
Related Question