0

I'm pretty new in python and trying to make a python script to put images together into a video(.mp4) using MoviePy.

However, I have multiple files and would like to be more efficient by sort of.... naming the folder and selecting all images within that folder than having to select all images individually.

Here's my Code:

from moviepy.editor import *
import os

clips = []
clip1 =  ImageClip('imagesfolder\images0.jpg').set_duration(4)
clip2 =  ImageClip('imagesfolder\images1.jpg').set_duration(4)
clip3 =  ImageClip('imagesfolder\images2.jpg').set_duration(4)
clip4 =  ImageClip('imagesfolder\images3.jpg').set_duration(4)
clip5 =  ImageClip('imagesfolder\images4.jpg').set_duration(4)
clip6 =  ImageClip('imagesfolder\images5.jpg').set_duration(4)
clip7 =  ImageClip('imagesfolder\images6.jpg').set_duration(4)
clip8 =  ImageClip('imagesfolder\images7.jpg').set_duration(4)
clip9 =  ImageClip('imagesfolder\images8.jpg').set_duration(4)
clip10 = ImageClip('imagesfolder\images9.jpg').set_duration(4)

clips.append(clip1)
clips.append(clip2)
clips.append(clip3)
clips.append(clip4)
clips.append(clip5)
clips.append(clip6)
clips.append(clip7)
clips.append(clip8)
clips.append(clip9)
clips.append(clip10)

video_clip = concatenate_videoclips(clips, method='compose')
video_clip.write_videofile("memes.mp4", fps=24, remove_temp=True, codec="libx264", 
audio_codec="aac")
Emossauce
  • 3
  • 3

1 Answers1

0

You can use a function called glob to find all files in a directly which match a pattern.

Eg

from glob import glob

clips = [ImageClip(clip).set_duration(4) for clip in glob("imagesfolder\*.gif")]

video_clip = concatenate_videoclips(clips, method="compose")
Mat
  • 1,345
  • 9
  • 15
  • Thanks, However now I keep getting an error saying: line 75, in concatenate_videoclips w = max(r[0] for r in sizes) ValueError: max() arg is an empty sequence – Emossauce Oct 11 '22 at 17:44
  • Would you mind adding more context please? I can't see what `sizes` is and how it's being set. – Mat Oct 11 '22 at 21:07
  • Sorry, your answer works perfect! Thank you very much. The error had to do with for some reason Visual Code opening up a copy file outside the folder from the images but now it works fine. – Emossauce Oct 12 '22 at 15:14