Skip to content

Commit

Permalink
Add audio normalization function
Browse files Browse the repository at this point in the history
Issue: Zulko#32
  • Loading branch information
dspinellis committed Jul 4, 2017
1 parent 6ba12d0 commit 582ec7f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
6 changes: 4 additions & 2 deletions docs/ref/audiofx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ You can either import a single function like this: ::
Or import everything: ::
import moviepy.audio.fx.all as afx
newaudio = (audioclip.afx( vfx.volumex, 0.5)
newaudio = (audioclip.afx( vfx.normalize)
.afx( vfx.volumex, 0.5)
.afx( vfx.audio_fadein, 1.0)
.afx( vfx.audio_fadeout, 1.0))

Expand All @@ -41,4 +42,5 @@ the module ``audio.fx`` is loaded as ``afx`` and you can use ``afx.volumex``, et
audio_fadein
audio_fadeout
audio_loop
volumex
audio_normalize
volumex
6 changes: 6 additions & 0 deletions docs/ref/audiofx/moviepy.audio.fx.all.audio_normalize.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
moviepy.audio.fx.all.audio_normalize
==================================

.. currentmodule:: moviepy.audio.fx.all

.. autofunction:: audio_normalize
9 changes: 9 additions & 0 deletions moviepy/audio/fx/audio_normalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from moviepy.decorators import audio_video_fx

@audio_video_fx
def audio_normalize(clip):
""" Return an audio (or video) clip whose volume is normalized
to 0db."""

mv = clip.max_volume()
return clip.volumex(1 / mv)
4 changes: 3 additions & 1 deletion moviepy/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
for method in [
"afx.audio_fadein",
"afx.audio_fadeout",
"afx.audio_normalize",
"afx.volumex",
"transfx.crossfadein",
"transfx.crossfadeout",
Expand All @@ -75,6 +76,7 @@
for method in ["afx.audio_fadein",
"afx.audio_fadeout",
"afx.audio_loop",
"afx.audio_normalize",
"afx.volumex"
]:

Expand Down Expand Up @@ -111,4 +113,4 @@ def preview(self, *args, **kwargs):
""" NOT AVAILABLE : clip.preview requires Pygame installed."""
raise ImportError("clip.preview requires Pygame installed")

AudioClip.preview = preview
AudioClip.preview = preview
7 changes: 7 additions & 0 deletions tests/test_fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from moviepy.video.fx.crop import crop
from moviepy.video.fx.fadein import fadein
from moviepy.video.fx.fadeout import fadeout
from moviepy.audio.fx.audio_normalize import audio_normalize
from moviepy.audio.io.AudioFileClip import AudioFileClip
from moviepy.video.io.VideoFileClip import VideoFileClip

import download_media
Expand Down Expand Up @@ -67,6 +69,11 @@ def test_fadeout():
clip1 = fadeout(clip, 1)
clip1.write_videofile(os.path.join(TMP_DIR,"fadeout1.webm"))

def test_normalize():
clip = AudioFileClip('media/crunching.mp3')
clip = audio_normalize(clip)
assert clip.max_volume() == 1


if __name__ == '__main__':
pytest.main()

0 comments on commit 582ec7f

Please sign in to comment.