ffmpeg-python icon indicating copy to clipboard operation
ffmpeg-python copied to clipboard

How to input different duration when creating a video from images?

Open ytbeom opened this issue 3 years ago • 1 comments

Hi guys, I'm trying to make one video from multiple images using python-ffmpeg. I read document of python-ffmpeg and many posts, but I didn't get the information I wanted.

I want to create a video from multiple images (e.g., 1.png, 2.png, ..., 10.png). Also, I want to set different duration on each image in ms. (e.g., 1.png -> 00:00:00.000 ~ 00:00:00.500, 2.png -> 00:00:00.500 ~ 00:00:01:230, etc) I've read a lot of posts, but every article is just an example of using options such as %d to input multiple images at once. I was testing the code as below. Because I want to set a different duration for each image, I made ffmpeg.input for each image and tried to set t parameter (in this code, t = 1 for just test).

import ffmpeg

captions = []
for i in range(1, 10):
    caption = ffmpeg.input('caption/{0}.png'.format(i), t=1, r=1)
    captions.append(caption)

joined = ffmpeg.concat(*captions)
ffmpeg.output(joined, 'captions.mov', vcodec='png').run()

However, the code produced a 3s video with 1.png, 2.png, and 3.png played 1s each, not 9s video. Also, I changed the value of the r parameter and the value in t parameter (e.g., 100ms), but no video of the desired length was generated. Is there a way to make a video that shows images with different duration (in ms)?

Thanks.

ytbeom avatar Jul 05 '22 20:07 ytbeom

I write down my solution for people who have similar problems with me. I created an input text file with duration for each image, and I used this file as an ffmpeg input. The input.txt file is configured as below. (I referred to this link)

file '1.png'
duration 2102ms
file '2.png'
duration 1835ms
file '3.png'
duration 1635ms
file '4.png'
duration 3603ms
file '4.png'
duration 0ms

And I called os.system method as below.

import os

os.system("ffmpeg -f concat -safe 0 -i input.txt -vf \"settb=AVTB,fps=30\" \
 -vcodec png -r 30 test_with_zero_blank.mov -y")

ytbeom avatar Jul 17 '22 14:07 ytbeom