Skip to content

Video editing with Python

Fix the error message and modify/add tests

zalgo8fd8d87fd3af
diff --git a/moviepy/video/io/ffmpeg_writer.py b/moviepy/video/io/ffmpeg_writer.py
index b602364c..694d580c 100644
--- a/moviepy/video/io/ffmpeg_writer.py
+++ b/moviepy/video/io/ffmpeg_writer.py
@@ -98,6 +98,7 @@ class FFMPEG_VideoWriter:
self.logfile = logfile
self.filename = filename
self.codec = codec
+ self.audio_codec = audio_codec
self.ext = self.filename.split(".")[-1]
if not pixel_format: # pragma: no cover
pixel_format = "rgba" if with_mask else "rgb24"
@@ -164,13 +165,13 @@ class FFMPEG_VideoWriter:
f"writing file {self.filename}:\n\n {ffmpeg_error}"
)
- if "Unknown encoder" in ffmpeg_error:
+ if "Unknown encoder" in ffmpeg_error or "Unknown decoder" in ffmpeg_error:
error += (
"\n\nThe video export failed because FFMPEG didn't find the "
- f"specified codec for video encoding {self.codec}. "
+ f"specified codec for video or audio. "
"Please install this codec or change the codec when calling "
"write_videofile.\nFor instance:\n"
- " >>> clip.write_videofile('myvid.webm', codec='libvpx')"
+ " >>> clip.write_videofile('myvid.webm', audio='myaudio.mp3', codec='libvpx', audio_codec='aac')"
)
elif "incorrect codec parameters ?" in ffmpeg_error:
diff --git a/tests/test_VideoClip.py b/tests/test_VideoClip.py
index 4e2d1abb..ddc2f838 100644
--- a/tests/test_VideoClip.py
+++ b/tests/test_VideoClip.py
@@ -72,7 +72,7 @@ def test_write_frame_errors(util, video):
clip.write_videofile(location, codec="nonexistent-codec")
assert (
"The video export failed because FFMPEG didn't find the specified"
- " codec for video encoding nonexistent-codec" in str(e.value)
+ " codec for video or audio" in str(e.value)
), e.value
autogenerated_location = "unlogged-writeTEMP_MPY_wvf_snd.mp3"
@@ -90,7 +90,7 @@ def test_write_frame_errors_with_redirected_logs(util, video):
clip.write_videofile(location, codec="nonexistent-codec", write_logfile=True)
assert (
"The video export failed because FFMPEG didn't find the specified"
- " codec for video encoding nonexistent-codec" in str(e.value)
+ " codec for video or audio" in str(e.value)
)
autogenerated_location_mp3 = "logged-writeTEMP_MPY_wvf_snd.mp3"
@@ -112,6 +112,22 @@ def test_write_videofiles_with_temp_audiofile_path(util):
assert any(file.startswith("temp_audiofile_path") for file in contents_of_temp_dir)
+def test_write_videofiles_audio_codec_error(util, video):
+ """Checks error cases return helpful messages."""
+ clip = video()
+ location = os.path.join(util.TMP_DIR, "unlogged-write.mp4")
+ with pytest.raises(IOError) as e:
+ clip.write_videofile(location, audio="media/crunching.mp3", audio_codec="nonexistent-codec")
+ assert (
+ "The video export failed because FFMPEG didn't find the specified"
+ " codec for video or audio" in str(e.value)
+ ), e.value
+
+ autogenerated_location = "unlogged-writeTEMP_MPY_wvf_snd.mp3"
+ if os.path.exists(autogenerated_location):
+ os.remove(autogenerated_location)
+
+
@pytest.mark.parametrize("mask_color", (0, 0.5, 0.8, 1))
@pytest.mark.parametrize(
"with_mask",