Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing VideoFileClip #57

Closed
lazem opened this issue Aug 18, 2014 · 12 comments
Closed

Closing VideoFileClip #57

lazem opened this issue Aug 18, 2014 · 12 comments

Comments

@lazem
Copy link

lazem commented Aug 18, 2014

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.

@Zulko
Copy link
Owner

Zulko commented Aug 18, 2014

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 ?

@lazem
Copy link
Author

lazem commented Aug 18, 2014

Yes, that works for me.

Appreciating your help. Thanks.

@Zulko Zulko closed this as completed Aug 19, 2014
@Zulko
Copy link
Owner

Zulko commented Aug 19, 2014

Cool.

@reidoda
Copy link

reidoda commented Nov 12, 2014

Just wondering if this this update been implemented yet?

@Zulko
Copy link
Owner

Zulko commented Nov 13, 2014

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

@reidoda
Copy link

reidoda commented Nov 13, 2014

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 notifications@github.com 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
#57 (comment).

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

@angelaambroz
Copy link

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...)

@Zulko
Copy link
Owner

Zulko commented Mar 22, 2015

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.

@angelaambroz
Copy link

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

@angelaambroz
Copy link

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?

@waclock
Copy link

waclock commented Jan 28, 2016

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

@yingshaoxo
Copy link

yingshaoxo commented Dec 26, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants