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

defunct ffmpeg processes left even after clip.close() #745

Closed
TejasBob opened this issue Mar 16, 2018 · 4 comments
Closed

defunct ffmpeg processes left even after clip.close() #745

TejasBob opened this issue Mar 16, 2018 · 4 comments
Labels
lib-misc Issues pertaining to misc. 3rd-party libraries. video Related to VideoClip and related classes, or handling of video in general.

Comments

@TejasBob
Copy link

TejasBob commented Mar 16, 2018

I tried following snippets in ipython terminal.

Snippet 1:

from moviepy.editor import *

clip = VideoFileClip(_video_path_).subclip(10,20).resize((200,100))

clip.write_videofile("result.mov", codec = "libx264", fps= 25)

clip.close()

After doing clip.close(), I checked

$ps -ef | grep ffmpeg
501 56157   343   0 12:55PM ttys000    0:00.00 grep ffmpeg
501 56134 56091   0 12:52PM ttys001    0:00.00 (ffmpeg)

There is one ffmpeg process left in memory.
I thought this happened because of loss of reference to ffmpeg process due to 2 outplace operations (subclip and resize), so I tried following script.

Snippet2:

from moviepy.editor import *
proc_list = []
clip = VideoFileClip(_video_path_)
proc_list.append(clip)

clip = clip.subclip(10,20)
proc_list.append(clip)

clip = clip.resize((200,100))
proc_list.append(clip)

clip.write_videofile("result.mov", codec = "libx264", fps= 25)

for ele in proc_list:
    ele.close()

Again I did,
ps -ef | grep ffmpeg

It gave similar output. There is one defunct process left in memory.

What is the right way of removing the ffmpeg instances?

Thanks!

@tburrows13
Copy link
Collaborator

This is an problem with the main branch, that doesn't have a fix yet. It is discussed in #596. There is a fix in #608 that you can try using.

Hope this helps, feel free to ask again if you need more assistance.

@TejasBob
Copy link
Author

TejasBob commented Mar 17, 2018

Thank you for references.

I noticed that, defunct ffmpeg processes are left only when the video being read has an audio associated with it. I managed to figure out that subclip() on the audio was causing the problem.

I added outplace decorator to subclip method in Clip.py and then it stopped leaving zombie processes behind.

Original Clip.py:

@convert_to_seconds(['t_start', 't_end'])
@apply_to_mask
@apply_to_audio
def subclip(self, t_start=0, t_end=None):
    ...

After adding outplace decorator

@convert_to_seconds(['t_start', 't_end'])
@apply_to_mask
@apply_to_audio
@outplace
def subclip(self, t_start=0, t_end=None):
    ...

I'm not sure if it is a legit fix or just co-incidence.

@TejasBob
Copy link
Author

TejasBob commented Mar 19, 2018

Update:

Adding the outplace operator isn't the solution.

After going through source code for couple of times, I noticed that the steps used to close ffmpeg video reader are same as steps used to close ffmpeg audio reader except one step which is proc.wait().

close_proc() method in readers.py ( which is handling closing of ffmpeg audio reader) doesn't have proc.wait(). I added it and ran the code. This time there were no defunct processes left behind.
See the code below for reference.

def close_proc(self):
    if hasattr(self, 'proc') and self.proc is not None:
        self.proc.terminate()
        for std in [ self.proc.stdout,
                     self.proc.stderr]:
            std.close()
        self.proc.wait() # added this line
        self.proc = None

I think it is a legit fix. I'm not sure if someone else has pointed this out earlier.

Thanks!

@keikoro keikoro added lib-misc Issues pertaining to misc. 3rd-party libraries. video Related to VideoClip and related classes, or handling of video in general. labels Dec 16, 2018
@tburrows13
Copy link
Collaborator

Fixed in #1185.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib-misc Issues pertaining to misc. 3rd-party libraries. video Related to VideoClip and related classes, or handling of video in general.
Projects
None yet
Development

No branches or pull requests

3 participants