Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix audio normalizing for mute clips #1401

Merged
merged 6 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
mondeja marked this conversation as resolved.
Show resolved Hide resolved
# 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()