Mastering Audio Editing in MoviePy: Extraction and Manipulation

Mastering Audio Editing in MoviePy: Extraction and Manipulation

In the previous textual content, we delved into the set up of MoviePy and utilized the VideoFileClip class to entry video information, performing easy edits like resizing, rotation, and section clipping.

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

Creating AudioClip

MoviePy gives two strategies to create audio clips: extracting audio from video information and producing audio clips primarily based on present audio information.

Extracting Audio from Video Files

Assuming we possess a video file named input_video.mp4, we are able to straight extract its audio with the next code:

from moviepy.editor import *

video = VideoFileClip("input_video.mp4")
audio = video.audio
audio.write_audiofile("output_audio.mp3")

Generating Audio Clips from Audio Files

By utilizing AudioFileClip, we are able to learn the audio file extracted from the video. The code seems as follows:

audio = AudioFileClip("output_audio.mp3")

We can embed this audio clip into one other video section.

Directly create a video utilizing TextClip and set the video length utilizing audio.length.

video = TextClip("Hello MoviePy").set_duration(audio.length).set_fps(1)

Let’s load the audio onto this video.

Here’s the code:

video.audio = audio
video.write_audio("text_video.mp4", coloration="orange", measurement=(100, 100))

When opening the video file utilizing QuickTime Player on a Mac, keep in mind so as to add the parameter audio_codec="aac" when writing:

video.write_audio("text_video.mp4", audio_codec="aac")

Upon completion, you may open the video and evaluate the results.

Audio Operations

This part discusses elementary operations on AudioClips, together with quantity management, audio section clipping, and eradicating audio from movies.

Volume Control

Adjusting the quantity of audio clips is completed straight via the volumex technique of AudioClip.

To lower the quantity by 50%, use the code beneath:

audio = audio.volumex(0.5)

Segment Clipping

Similar to trimming movies, subclip allows us to extract particular parts of audio clips.

For occasion, to extract the audio section from 5 to fifteen seconds, use the code:

audio = audio.subclip(5, 15)

Removing Audio from Videos

Here’s a helpful trick: to take away audio from a video, set the audio parameter to False when studying the video file with VideoFileClip.

video = VideoFileClip("input_video", audio=False)

Alternatively, you may make the most of the without_audio technique of VideoFileClip to take away audio from video segments.

video = video.without_audio()

Isn’t it easy?

Conclusion

This article primarily covers elementary MoviePy operations. For extra MoviePy tutorials, subscribe to our weblog. Thank you!

HI-FI News

by way of DEV Community https://dev.to

January 4, 2024 at 04:08PM