Skip to content

Video editing with Python

FFmpeg Writer: handle redirected logs when checking for errors (#890)

Nick Boultbee98c73cd12252
diff --git a/moviepy/video/io/ffmpeg_writer.py b/moviepy/video/io/ffmpeg_writer.py
index e82f53f8..ff464a83 100644
--- a/moviepy/video/io/ffmpeg_writer.py
+++ b/moviepy/video/io/ffmpeg_writer.py
@@ -81,7 +81,7 @@ class FFMPEG_VideoWriter:
if logfile is None:
logfile = sp.PIPE
-
+ self.logfile = logfile
self.filename = filename
self.codec = codec
self.ext = self.filename.split(".")[-1]
@@ -137,7 +137,12 @@ class FFMPEG_VideoWriter:
try:
self.proc.stdin.write(img_array.tobytes())
except IOError as err:
+ logs = self.logfile.name
_, ffmpeg_error = self.proc.communicate()
+ if not ffmpeg_error:
+ with open(logs, "rb") as f:
+ ffmpeg_error = f.read()
+
error = str(err) + (
"\n\nMoviePy error: FFMPEG encountered "
"the following error while writing file %s:"
diff --git a/tests/test_VideoClip.py b/tests/test_VideoClip.py
index c9e4d931..a9b56830 100644
--- a/tests/test_VideoClip.py
+++ b/tests/test_VideoClip.py
@@ -27,6 +27,17 @@ def test_check_codec():
close_all_clips(locals())
+def test_errors_with_redirected_logs():
+ """Checks error cases return helpful messages even when logs redirected
+ See https://github.com/Zulko/moviepy/issues/877"""
+ clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
+ location = os.path.join(TMP_DIR, "logged-write.mp4")
+ with pytest.raises(IOError) as e:
+ clip.write_videofile(location, codec="nonexistent-codec", write_logfile=True)
+ assert "Unknown encoder 'nonexistent-codec'" in str(e.value)
+ close_all_clips(locals())
+
+
def test_save_frame():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
location = os.path.join(TMP_DIR, "save_frame.png")