-1

I am trying to create an mp4 by concatenating some videoclips using moviepy. However, depending on number of clips, I get the error: too many open files. In the code below two lists are being used where jpegs contains all embedded pictures and their corresponding mp3 in mp3s list having more than 100 clips.

image_main = []
d = 0

for i in range(0,len(jpegs)):

   audio = AudioFileClip(mp3s[i])

   image = (ImageClip(jpegs[i])
        .set_duration(audio.duration)
        .set_audio(audio))

   d  = d + (image.duration / 60.0)
   print(jpegs[i])
   print('TOTAL DURATION: ', d)

   image_main.append(image)
   #del audio.reader
   #del audio 

As seen in the code snippet above, I tried to add deleting audio (lines commented out) just after being appended. However, this caused another issue in the code below as a result of which I could not create video file.

concat_clip = concatenate_videoclips(image_main, method="compose", transition=transition)
clip_resized = concat_clip.resize(width=1920)
clip_resized.write_videofile("REDDIT/stupid_TRIAL.mp4", fps=24, threads = 4)

Now I have solved the issue by just using a few videos instead of many.

How can I solve this?

mlee_jordan
  • 772
  • 4
  • 18
  • 50

1 Answers1

1

I think that the best way to handle this is to break it down into smaller steps. After you have imported lots of audio/image files, concatenate them and write them to a temp video file. Then close each of the audio files with audio.close() (this will automatically close audio.reader.

Repeat this process until you have done all of the files, then read the temp video files in and concatenate those ones together.

Tom Burrows
  • 2,225
  • 2
  • 29
  • 46