Skip to content

Commit

Permalink
FFmpeg Writer: handle redirected logs when checking for errors (#890)
Browse files Browse the repository at this point in the history
* FFmpeg Writer: handle redirected logs when checking for errors

 * Previously we were looking for `'foo' in None` resulting in a TypeError (#877)
 * Now, if an `IOError` occurs when running FFmpeg to write video clips, **but** there is no output returned, assume we are redirected, and read the logs in from wherever they were actually placed.
  • Loading branch information
declension committed Mar 30, 2020
1 parent 674257e commit 98c73cd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion moviepy/video/io/ffmpeg_writer.py
Expand Up @@ -81,7 +81,7 @@ def __init__(

if logfile is None:
logfile = sp.PIPE

self.logfile = logfile
self.filename = filename
self.codec = codec
self.ext = self.filename.split(".")[-1]
Expand Down Expand Up @@ -137,7 +137,12 @@ def write_frame(self, img_array):
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:"
Expand Down
11 changes: 11 additions & 0 deletions tests/test_VideoClip.py
Expand Up @@ -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")
Expand Down

0 comments on commit 98c73cd

Please sign in to comment.