Make a video in sequential order from frames

Viewed 459

I have saved images in the sequential format- 0.jpg, 1.jpg, ---,99.jpg, 100.jpg. I am making video from these frames by using ffmpeg for conversion:

cmd1 = 'ffmpeg -framerate 25 -pattern_type glob -i "/home/ubuntu/17/1_1/*.jpg" /home/ubuntu/17_a.avi'

But ffmpeg is not reading the images in sequential manner. How to make a video that take frames in a sequential order.

2 Answers

But ffmpeg is not reading the images in sequential manner. How to make a video that take frames in a sequential order.

globs generally provide lexicographical order, i.e. 0 to 9 are treated as letters thus, if you sort numbers from 1 to 100 (inclusive) it goes like 1, 10, 100, 11, 12...

I do not know if ffmpeg's glob has option to use natsort, but if you can rename your files it should be easy to pad with zeros using python and thus making lexicographical ordering same as natsort. Please create copy of catalog with your jpg files and then run there following python code

import glob,os
for jpg in glob.glob("*.jpg"):
    os.rename(jpg,jpg.zfill(7))

which should rename

0.jpg, 1.jpg, ---,99.jpg, 100.jpg

to

000.jpg, 001.jpg, ---,099.jpg, 100.jpg

then run your ffmpeg command and check if it worked correctly. .zfill does pad with zeros to specified width, I used 7 as last name 100.jpg has 7 characters. If it would be 1000.jpg then at least 8 would be required and so on.

The default behaviour for ffmpeg is to read images in continuous numeric sequence. But since you have specified glob pattern, it uses that. Drop pattern_type and specify the correct pattern in the filename field.

ffmpeg -framerate 25 -i "/home/ubuntu/17/1_1/%d.jpg" /home/ubuntu/17_a.avi'

Related