Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a video from a series of images with varying image durations?

I'd like to programmatically create a video file that is composed of a series of images. However, I'd also like to be able to specify a duration for each image. I often see ffmpeg examples suggested for similar tasks, but they always assume the same duration for each image. Is there an efficient way to accomplish this? (An inefficient solution might be setting the frame rate to something high and repeatedly copying each image until it matches the intended duration)

I will be dynamically generating each of the images as well, so if there is way to encode the image data into video frames without writing each image to disk, that's even better. This, however, is not a requirement.

Edit: To be clear, I don't necessarily need to use ffmpeg. Other free command-line tools are fine, as are video-processing libraries. I'm just looking for a good solution.

like image 557
user1354557 Avatar asked Dec 20 '12 09:12

user1354557


3 Answers

I was able to solve the exact same problem with the following commands. vframes is set to the number of seconds * fps In the example the first video has 100 frames (100 frame / 25 fps = 4 seconds) and second one has 200 frames (8 seconds)

ffmpeg -f image2 -loop 1 -vframes 100 -r 25 -i a.jpg -vcodec mpeg4 a.avi
ffmpeg -f image2 -loop 1 -vframes 200 -r 25 -i b.jpg -vcodec mpeg4 b.avi
mencoder -ovc copy -o out.mp4 a.mp4 b.mp4

The mencoder part is just like the one of d33pika

like image 128
Marco Martinelli Avatar answered Nov 02 '22 22:11

Marco Martinelli


You can use the concat demuxer to manually order images and to provide a specific duration for each image.

ffmpeg -f concat -i input.txt -vsync vfr -pix_fmt yuv420p output.mp4

Your input.txt should look like this.

file '/path/to/dog.png'
duration 5
file '/path/to/cat.png'
duration 1
file '/path/to/rat.png'
duration 3
file '/path/to/tapeworm.png'
duration 2
file '/path/to/tapeworm.png'

You can write this txt file dynamically according to your needs and excute the command.

For more info refer to https://trac.ffmpeg.org/wiki/Slideshow

like image 24
Gourav Saini Avatar answered Nov 02 '22 20:11

Gourav Saini


It seems like there is no way to have different durations for different images using ffmpeg. I would create separate videos for each of the images and then concat them using mencoder like this:

ffmpeg -f image2 -vframes 30 -i a.jpg -vcodec libx264 -r 1 a.mp4
ffmpeg -f image2 -vframmes 10 -i bjpg -vcodec libx264 -r 1 b.mp4
mencoder -ovc copy -o out.mp4 a.mp4 b.mp4

mencoder for the concat operation needs all the output videos to have same resolution,framerate and codec.

Here a.mp4 has 30 frames of duration 30 seconds and b.mp4 has 10 frames of 10 seconds.

like image 2
d33pika Avatar answered Nov 02 '22 21:11

d33pika