Moviepy: Closing VideoFileClip

Created on 18 Aug 2014  Â·  12Comments  Â·  Source: Zulko/moviepy

Hi,

I'm using moviepy in an artwork, making concatenated videos selected from a list of 10k video files. I'm running this process in an infinite loop (making a video and displaying it), but I can't find a way to close the video files I used, and after a while it halts on the "python - IOError: [Errno 24] Too many open files" error. It is on the constructor of VideoFileClip.

Is there a way to release the files, cause I can't find any in the object methods?
Many thanks.

Most helpful comment

Hey,

It depends. If you are using the function concatenate on a list of clips, all these clips must be open, there is no workaround for the moment (if that is your issue I can propose a solution).

If you just want to get rid of a few VideoFileClips at the end of each loop, here is how you proceed to close the files and avoid memory leaks:

from moviepy.editor import *

while True:
    clip = VideoFileClip(some_name)
    # do whatever with the clip...
    del clip.reader
    del clip

In the next versions of MoviePy just del clip will suffice. Does that solve your problem ?

All 12 comments

Hey,

It depends. If you are using the function concatenate on a list of clips, all these clips must be open, there is no workaround for the moment (if that is your issue I can propose a solution).

If you just want to get rid of a few VideoFileClips at the end of each loop, here is how you proceed to close the files and avoid memory leaks:

from moviepy.editor import *

while True:
    clip = VideoFileClip(some_name)
    # do whatever with the clip...
    del clip.reader
    del clip

In the next versions of MoviePy just del clip will suffice. Does that solve your problem ?

Yes, that works for me.

Appreciating your help. Thanks.

Cool.

Just wondering if this this update been implemented yet?

I think so, why, have you met a similar problem with the recent versions of MoviePy ?

Thanks for the response. No I haven't. I was just wondering if I needed to
include the extra line in my code.

On Thu, Nov 13, 2014 at 12:41 AM, Zulko [email protected] wrote:

I think so, why, have you met a similar problem with the recent versions
of MoviePy ?

—
Reply to this email directly or view it on GitHub
https://github.com/Zulko/moviepy/issues/57#issuecomment-62844541.

Reid Oda
Ph.D. Candidate
Princeton University
858-349-2037
http://www.cs.princeton.edu/~roda

Hi - I'm getting the same error as the original poster: I'm aiming to concatenate 360+ clips together. Currently, it's breaking down at 66 clips. What would you recommend as a good workaround? (My first instinct is to bunch them into arrays of 50-ish clips, concatenate, .write_videofile them, and then use that .mp4 as the base for the next array, and so on...)

Yeah, that's the main limitation of MoviePy for now, that's fixable but it's not trivial and may take time, sorry. For now you will need to go with your trick consisting in making intermediary clips and then concatenating these intermediary clips.

When you make your intermediary clips, make sure to use a large bitrate="20000k" and audio_bitrate="2000k" to avoid quality loss.

Cool, I'll do that. And no problem - MoviePy is such a fun library! Thanks for your work on it.

Hi Zulko - I just tried implementing the iteration hack, but it's still throwing an error on too many open files. Here's my code:

            for i in range(2+step,all_days,step):
                print "Iteration: ", i
                for item in sorted(os.listdir(day_files), key=gettimestamp)[i-step:i]:
                    if item==".DS_Store" or item=="DAY_December31.mp4":
                        pass    
                    else:
                        print "Now doing: " + item
                        day_clip = mpy.VideoFileClip(day_files+item).set_duration(1)
                        caption = datetime.datetime.strptime(item[4:-4], "%B%d")
                        caption = datetime.datetime.strftime(caption, "%B %d")
                        txt_clip = mpy.TextClip(caption,fontsize=50,color="white")
                        txt_clip = txt_clip.set_pos(("center","bottom")).set_duration(1)
                        clip_item = mpy.CompositeVideoClip([day_clip, txt_clip])
                        del day_clip
                        del txt_clip
                        clip_array.append(clip_item)
                        del clip_item
                chunk_video = mpy.concatenate_videoclips(clip_array, method='compose')
                chunk_video.write_videofile(day_files + "../2015edited/clip" + `i` + ".mp4",fps=24)
                chunk_clip = mpy.VideoFileClip(day_files + "../2015edited/clip" + `i` + ".mp4")
                master_array.append(chunk_clip)
                del clip_array[:]

            year_video = mpy.concatenate_videoclips(master_array, method='compose')
            year_video.write_videofile(day_files + "../2015edited/2015 in review.mp4",fps=24)

Not sure how to kill those subprocesses beyond using del on them. Any pointers?

Same problem as @angelaambroz , were you able to solve it?

Now I understood

You should never give too many videos files(.mp4) to moviepy at once.


But you can give it a parent video

Then split it to thousands of subclips in memory

Actually, in this time, they are not real clips (i mean .mp4 file)


At this moment, your memory only stored one video, the parent video, and the information about subclips (where it starts, where it ends)


In this way, you feed the clip list to concatenate function, it won't cause memory overflow anymore


Here is the demo codes:

parent_clip = VideoFileClip("./parent_video.mp4")
clip_list = []
for part in time_parts:
    time_start = part[0]
    time_end = part[1]
    clip_list.append(
        parent_clip.subclip(time_start, time_end)
    )
concat_clip = concatenate_videoclips(clip_list)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

PyB1l picture PyB1l  Â·  3Comments

keikoro picture keikoro  Â·  4Comments

buddhashrestha picture buddhashrestha  Â·  3Comments

xjcl picture xjcl  Â·  3Comments

Deng-deng-deng-deng picture Deng-deng-deng-deng  Â·  3Comments