Broken Pipe Error On Moviepy When Receiving a SIGINT OR SIGTERM

Viewed 368

I'm trying to write a video with moviepy on a temp dir.

Here's a sample code:

video_file_name = '{}/{}'.format(tempfile.mkdtemp(), 'somefile.mp4')
clip.write_videofile(video_file_name, audio = False, threads =4,logger = None)

This works perfectly fine. However In my app context I want to be able to catch SIGTERM or SIGINT signals and wait until this write finishes before actually killing the app.

I do it this way:

signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)

def exit_gracefully():
    # Some logic to wait for video processing to finish, for now we'll wait 1 hour to give enough time.
    time.sleep(3600)

The problem is that when I do this, moviepy raises an exception like this:

/lib/python3.7/site-packages/moviepy/video/io/ffmpeg_writer.py", line 136, in write_frame
    self.proc.stdin.write(img_array.tobytes())
BrokenPipeError: [Errno 32] Broken pipe

During handling of the above exception, another exception occurred:

      File "<decorator-gen-55>", line 2, in write_videofile
      File "lib/python3.7/site-packages/moviepy/decorators.py", line 54, in requires_duration
        return f(clip, *a, **k)
      File "<decorator-gen-54>", line 2, in write_videofile
      File "lib/python3.7/site-packages/moviepy/decorators.py", line 135, in use_clip_fps_by_default
        return f(clip, *new_a, **new_kw)
      File "<decorator-gen-53>", line 2, in write_videofile
      File "lib/python3.7/site-packages/moviepy/decorators.py", line 22, in convert_masks_to_RGB
        return f(clip, *a, **k)
      File "lib/python3.7/site-packages/moviepy/video/VideoClip.py", line 307, in write_videofile
        logger=logger)
      File "lib/python3.7/site-packages/moviepy/video/io/ffmpeg_writer.py", line 228, in ffmpeg_write_video
        writer.write_frame(frame)
      File "lib/python3.7/site-packages/moviepy/video/io/ffmpeg_writer.py", line 180, in write_frame
        raise IOError(error)
    OSError: [Errno 32] Broken pipe
    
    MoviePy error: FFMPEG encountered the following error while writing file /tmp/tmpt0o8ae6e/1633997015.176414_re_saved.mp4:
    
     b''

Is there a way I can go around this error and allow my write file to finish? Another context is that this write_videofile() function is executed on a separate thread, I'm not sure if that's useful but it might help to give more insights.

Any ideas on how I can solve this?

Thanks!

0 Answers
Related