Skip to content

Video editing with Python

Fix audio normalizing for mute clips (#1401)

Jaclyn Brockschmidtf5944d436f93
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dacc86da..b99628c6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ Lots of method and parameter names have been changed. This will be explained bet
- Fixed `rotate` sometimes failing with `ValueError: axes don't match array` [\#1335](https://github.com/Zulko/moviepy/pull/1335)
- Fixed positioning error generating frames in `CompositeVideoClip` [\#1420](https://github.com/Zulko/moviepy/pull/1420)
- Changed deprecated `tostring` method by `tobytes` in `video.io.gif_writers::write_gif` [\#1429](https://github.com/Zulko/moviepy/pull/1429)
+- Fixed calling `audio_normalize` on a clip with no sound causing `ZeroDivisionError` [\#1401](https://github.com/Zulko/moviepy/pull/1401)
## [v2.0.0.dev2](https://github.com/zulko/moviepy/tree/v2.0.0.dev2) (2020-10-05)
diff --git a/moviepy/audio/fx/audio_normalize.py b/moviepy/audio/fx/audio_normalize.py
index ca63d5e0..8d088134 100644
--- a/moviepy/audio/fx/audio_normalize.py
+++ b/moviepy/audio/fx/audio_normalize.py
@@ -19,4 +19,9 @@ def audio_normalize(clip):
"""
max_volume = clip.max_volume()
- return volumex(clip, 1 / max_volume)
+ if max_volume == 0:
+ # Nothing to normalize.
+ # Avoids a divide by zero error.
+ return clip.copy()
+ else:
+ return volumex(clip, 1 / max_volume)
diff --git a/tests/test_fx.py b/tests/test_fx.py
index 80cf4bec..96ee6e67 100644
--- a/tests/test_fx.py
+++ b/tests/test_fx.py
@@ -1,7 +1,9 @@
import os
+import numpy as np
import pytest
+from moviepy.audio.AudioClip import AudioClip
from moviepy.audio.fx.audio_normalize import audio_normalize
from moviepy.audio.io.AudioFileClip import AudioFileClip
from moviepy.utils import close_all_clips
@@ -456,5 +458,14 @@ def test_normalize():
close_all_clips(locals())
+def test_normalize_muted():
+ z_array = np.array([0.0])
+ make_frame = lambda t: z_array
+ clip = AudioClip(make_frame, duration=1, fps=44100)
+ clip = audio_normalize(clip)
+ assert np.array_equal(clip.to_soundarray(), z_array)
+ close_all_clips(locals())
+
+
if __name__ == "__main__":
pytest.main()