A powerful tool for Python audio processing

421d53559164687ee4470ef91a1d4566.png

Lost little book boy

Needed after reading

3

minute

Speed ​​reading only takes 1 minute

When it comes to audio processing and editing, PyDub is a very powerful and easy-to-use open source library. It provides many functions such as audio cutting, merging, mixing, speed changing, pitch changing, etc. This article will take you step by step to understand the basic principles and usage of PyDub.

Installing PyDub is very simple. You just need to open a terminal or command prompt and run the following command

pip install pydub

This will automatically download and install the PyDub library and its dependencies.

Once the installation is complete, you can start using PyDub. Let's look at some basic usage.

First, you need to import the PyDub library

from pydub import AudioSegment

The core concept of PyDub is the AudioSegment class, which represents an audio segment. You can load audio files into AudioSegment objects and then perform various operations on them.

Let's start with a simple example. Suppose you have a music file song.mp3, and you want to cut the first 30 seconds of it and save it as a new file intro.mp3. You can use the following code to achieve

from pydub import AudioSegment


# 加载音频文件
song = AudioSegment.from_file("song.mp3")


# 剪切前30秒
intro = song[:30000]


# 保存为新文件
intro.export("intro.mp3", format="mp3")

In this example, we first loaded the song.mp3 file using the from_file method and stored it in the song object. We then use the slicing operator [:30000] to select the first 30 seconds of the audio clip and store it in the intro object. Finally, we use the export method to save the intro object as an intro.mp3 file.

In addition to clipping, PyDub provides many other features. For example, you can use the + operator to merge two audio clips together

combined = intro + song

You can also use the overlay method to overlay one audio clip onto another.

combined = song.overlay(intro)

PyDub allows you to perform various complex operations on audio, such as fade in and fade out effects, volume adjustment, audio special effects, etc. Finally, let’s look at a sample code for the fade-in and fade-out effect.

from pydub import AudioSegment


# 加载音频文件
song = AudioSegment.from_file("song.mp3")


# 淡入淡出效果
fade_in = song.fade_in(2000)  # 淡入2000毫秒,即最开始2秒声音小
fade_out = song.fade_out(2000)  # 淡出2000毫秒,即最后2秒声音小


# 保存文件
fade_in.export("fade_in.mp3", format="mp3")


# 保存文件
fade_out.export("fade_out.mp3", format="mp3")

In this example, we use the fade_in method and the fade_out method to add fade-in and fade-out effects to the audio respectively. Then, we use the export function to save it as a local audio file.

PyDub also provides many other methods for processing audio, such as mixing, speed changing, pitch changing, etc. You can learn more details by consulting PyDub's official documentation at: https://github.com/jiaaro/pydub ( https://github.com/jiaaro/pydub ).

d9ba72b8cf1eaf0a882139d5aa3d4b9b.jpeg

9a1ee8a4e65bfcce1afc7efaaa718c88.gif

Guess you like

Origin blog.csdn.net/djstavaV/article/details/133503686