Skip to content

Commit

Permalink
Fix audio normalizing for mute clips (Zulko#1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrockschmidt authored and tburrows13 committed Jan 19, 2021
1 parent e95c58d commit 34e55ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion moviepy/audio/fx/audio_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 11 additions & 0 deletions tests/test_fx.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 34e55ee

Please sign in to comment.