Mastering Audio Editing in MoviePy: Extraction and Manipulation

poloxue - Jan 4 - - Dev Community

In the preceding text, we delved into the installation of MoviePy and utilized the VideoFileClip class to access video files, performing simple edits like resizing, rotation, and segment clipping.

This article's focus lies on understanding MoviePy's audio capabilities.

Creating AudioClip

MoviePy offers two methods to create audio clips: extracting audio from video files and generating audio clips based on existing audio files.

Extracting Audio from Video Files

Assuming we possess a video file named input_video.mp4, we can directly extract its audio with the following code:

from moviepy.editor import *

video = VideoFileClip("input_video.mp4")
audio = video.audio
audio.write_audiofile("output_audio.mp3")
Enter fullscreen mode Exit fullscreen mode

Generating Audio Clips from Audio Files

By using AudioFileClip, we can read the audio file extracted from the video. The code appears as follows:

audio = AudioFileClip("output_audio.mp3")
Enter fullscreen mode Exit fullscreen mode

We can embed this audio clip into another video segment.

Directly create a video using TextClip and set the video duration using audio.duration.

video = TextClip("Hello MoviePy", color="orange", size=(100, 100))
video = video.set_duration(audio.duration).set_fps(1)
Enter fullscreen mode Exit fullscreen mode

Let's load the audio onto this video.

Here's the code:

video.audio = audio
video.write_audio("text_video.mp4")
Enter fullscreen mode Exit fullscreen mode

When opening the video file using QuickTime Player on a Mac, remember to add the parameter audio_codec="aac" when writing:

video.write_audio("text_video.mp4", audio_codec="aac")
Enter fullscreen mode Exit fullscreen mode

Upon completion, you can open the video and review the effects.

Audio Operations

This section discusses fundamental operations on AudioClips, including volume control, audio segment clipping, and removing audio from videos.

Volume Control

Adjusting the volume of audio clips is accomplished directly through the volumex method of AudioClip.

To decrease the volume by 50%, use the code below:

audio = audio.volumex(0.5)
Enter fullscreen mode Exit fullscreen mode

Segment Clipping

Similar to trimming videos, subclip enables us to extract specific portions of audio clips.

For instance, to extract the audio segment from 5 to 15 seconds, use the code:

audio = audio.subclip(5, 15)
Enter fullscreen mode Exit fullscreen mode

Removing Audio from Videos

Here's a handy trick: to remove audio from a video, set the audio parameter to False when reading the video file with VideoFileClip.

video = VideoFileClip("input_video", audio=False)
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can utilize the without_audio method of VideoFileClip to remove audio from video segments.

video = video.without_audio()
Enter fullscreen mode Exit fullscreen mode

Isn't it simple?

Conclusion

This article primarily covers fundamental MoviePy operations. For more MoviePy tutorials, subscribe to our blog. Thank you!

. . . . .