0

I want to concatenate 4 clips of the same videoclip, Fimage, for a certain duration of time. The print tests and logic tests say that this should work, but it generates a 7 second clip with half of it bugging out. My suspicion is that the concatenate_videoclips is messing up in the for loop as print statements are working inside the for loop, so is there another way I can concatenate it?

This should be really straight forward, but it bugs out on me. In theory it should generate a 23 second video clip of the same image, but instead it generates a 7 second image with it bugging at 4 second mark.

from moviepy.editor import *

# Create an ImageClip object
Fimage = ImageClip("Z:/Programming Stuff/Images/Testing/Char3.png")
Fimage = Fimage.set_duration(5)
Fimage.fps = 30

durations = [5,10,5,3] #Durations of each clip

clips = [Fimage, Fimage, Fimage, Fimage] #The same clip 4 times 

Final = clips[0].set_duration(durations[0]) 
#IT SEEMS THAT WHENEVER I PUT FOR LOOP CONCATENATE, IT DOESNT WORK
for i in range(1, len(clips)):
    clip = clips[i].set_duration(durations[i])
    Final = concatenate_videoclips([Final, clip])
#The print statements oddly work fine so the loop isn't the problem, I suspect it's the concatenate logic.
Final.write_videofile("wiggles1.mp4")

Electro
  • 17
  • 8
  • Most likely not your problem, but `Fimage.fps = 30` should rather be `Fimage = Fimage.set_fps(30)`. – chiefenne Feb 02 '23 at 09:44
  • Is there a difference? Both seem to work just fine, if one is more efficient or some other reason, then please elaborate. – Electro Feb 03 '23 at 06:17
  • I don't know if there is a difference, but by looking at the methods of the ImageClip class I only found the set_fps method. Maybe I overlooked something. – chiefenne Feb 08 '23 at 07:35

1 Answers1

0

My suspicions were correct. concatenate_videoclips does not like being in a for loop. It is not in the documentation, but from testing, it completes a single iteration and then freaks out for the second iteration and breaks, thus causing a 7 second clip with 5 seconds as the first clip and 2 seconds as the attempted second iteration. The correct way to do this is to alter each element's duration and concatenate outside of the loop. For all those who will attempt to do this, remember, concatenate just really hates being in loops and must be outside of it.

Final = clips[0].set_duration(durations[0]) #IT SEEMS THAT WHENEVER I PUT FOR LOOP CONCATENATE, IT DOESNT WORK
for i in range(1, len(clips)):
    clips[i] = clips[i].set_duration(durations[i])
Final = concatenate_videoclips(clips) #Never put this in a the loop
Electro
  • 17
  • 8