From abde0d70d12831d6f5d04ff10074614133984468 Mon Sep 17 00:00:00 2001 From: Jaclyn Brockschmidt Date: Mon, 4 Jan 2021 21:19:44 -0800 Subject: [PATCH 1/5] Fix audio normalizing for mute clips --- moviepy/audio/fx/audio_normalize.py | 5 ++++- tests/test_fx.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/moviepy/audio/fx/audio_normalize.py b/moviepy/audio/fx/audio_normalize.py index ca63d5e01..91805c218 100644 --- a/moviepy/audio/fx/audio_normalize.py +++ b/moviepy/audio/fx/audio_normalize.py @@ -19,4 +19,7 @@ def audio_normalize(clip): """ max_volume = clip.max_volume() - return volumex(clip, 1 / max_volume) + if max_volume == 0: + return clip.copy() + else: + return volumex(clip, 1 / max_volume) diff --git a/tests/test_fx.py b/tests/test_fx.py index 44edebd28..23e3e5f8c 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,12 @@ def test_normalize(): close_all_clips(locals()) +def test_normalize_muted(): + make_frame = lambda t: np.array([0.0]) + clip = AudioClip(make_frame, duration=1, fps=44100) + clip = audio_normalize(clip) + close_all_clips(locals()) + + if __name__ == "__main__": pytest.main() From 20aeb1ef33a0ee7c32aa8718f026ec5c588d72a1 Mon Sep 17 00:00:00 2001 From: Jaclyn Brockschmidt Date: Mon, 11 Jan 2021 17:57:11 -0800 Subject: [PATCH 2/5] Add clarification for audio normalizing Adds clarifying comments to the code and sentence the docstring for the audio_normalize effect. --- moviepy/audio/fx/audio_normalize.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/moviepy/audio/fx/audio_normalize.py b/moviepy/audio/fx/audio_normalize.py index 91805c218..e7f6bf73b 100644 --- a/moviepy/audio/fx/audio_normalize.py +++ b/moviepy/audio/fx/audio_normalize.py @@ -9,6 +9,7 @@ def audio_normalize(clip): Return an audio (or video) clip whose audio volume is normalized so that the maximum volume is at 0db, the maximum achievable volume. + Audio that is muted is left unchanged. Examples ======== @@ -20,6 +21,8 @@ def audio_normalize(clip): max_volume = clip.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) From 8656577923c0b162f7febb82f57484fa3c355085 Mon Sep 17 00:00:00 2001 From: Jaclyn Brockschmidt Date: Wed, 13 Jan 2021 14:04:17 -0800 Subject: [PATCH 3/5] Check that sound array is actually muted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Álvaro Mondéjar --- tests/test_fx.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_fx.py b/tests/test_fx.py index 23e3e5f8c..0263c55d8 100644 --- a/tests/test_fx.py +++ b/tests/test_fx.py @@ -459,9 +459,11 @@ def test_normalize(): def test_normalize_muted(): - make_frame = lambda t: np.array([0.0]) + 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()) From aaf630009596b7fbcd29e07f593928df3733e662 Mon Sep 17 00:00:00 2001 From: Jaclyn Brockschmidt Date: Wed, 13 Jan 2021 14:05:20 -0800 Subject: [PATCH 4/5] Remove unnecessary docstring clarification --- moviepy/audio/fx/audio_normalize.py | 1 - 1 file changed, 1 deletion(-) diff --git a/moviepy/audio/fx/audio_normalize.py b/moviepy/audio/fx/audio_normalize.py index e7f6bf73b..8d088134b 100644 --- a/moviepy/audio/fx/audio_normalize.py +++ b/moviepy/audio/fx/audio_normalize.py @@ -9,7 +9,6 @@ def audio_normalize(clip): Return an audio (or video) clip whose audio volume is normalized so that the maximum volume is at 0db, the maximum achievable volume. - Audio that is muted is left unchanged. Examples ======== From cd1177acc7a41f3e935372f06ab6bfd6a657d78c Mon Sep 17 00:00:00 2001 From: Tom Burrows Date: Wed, 13 Jan 2021 22:20:24 +0000 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1bb09acd..dcdb12886 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ Lots of method and parameter names have been changed. This will be explained bet ### Fixed - Fixed BitmapClip with fps != 1 not returning the correct frames or crashing [#1333] - Fixed `rotate` sometimes failing with `ValueError: axes don't match array` [#1335] +- 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)