[파이썬] pydub 오디오 파일 배치 처리

Whether you are working on a music application or a podcast, efficiently processing audio files is crucial. Pydub is a powerful library that allows you to manipulate and process audio files in Python. In this blog post, we will explore how to use pydub for batch processing audio files.

Installation

To get started with pydub, you need to install it using pip:

pip install pydub

Additionally, pydub relies on the FFmpeg tool for working with different audio formats. So, make sure you have FFmpeg installed on your system and added to the system’s PATH variable.

Loading Audio Files

Before we begin processing audio files, we need to load them first. Pydub supports various audio formats such as WAV, MP3, FLAC, etc. Here’s an example of loading an audio file:

from pydub import AudioSegment

audio = AudioSegment.from_file("audiofile.mp3")

Applying Effects

Pydub provides a wide range of audio effects that you can apply to the loaded audio file. Let’s explore a few commonly used effects:

1. Changing Volume

To adjust the volume of the audio file, you can use the fade method. Here’s an example of increasing the volume by 3 decibels:

louder_audio = audio.fade(increase_dB=3)
louder_audio.export("louder_audio.mp3", format="mp3")

2. Adding Silence

If you want to insert a pause or silence in the audio, you can use the + operator to concatenate audio segments. Here’s an example of adding 2 seconds of silence at the beginning of the audio:

silence = AudioSegment.silent(duration=2000)
audio_with_silence = silence + audio
audio_with_silence.export("audio_with_silence.mp3", format="mp3")

3. Changing Speed

To speed up or slow down the audio playback, you can use the speedup or slowdown methods. Here’s an example of increasing the speed of the audio by 1.5 times:

fast_audio = audio.speedup(playback_speed=1.5)
fast_audio.export("fast_audio.mp3", format="mp3")

Batch Processing

Now that we know how to apply effects to a single audio file, let’s explore how to process multiple audio files in a batch.

import os

# Get all audio file names in the directory
audio_files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.mp3')]

# Process each audio file
for file in audio_files:
    audio = AudioSegment.from_file(file)
    
    # Apply your desired effects here
    modified_audio = audio.fade(increase_dB=3)
    
    # Export the modified audio file
    modified_audio.export("modified_" + file, format="mp3")

In the above code snippet, we first retrieve all the audio file names in the current directory. Then, we iterate over each file, apply the desired effects, and export the modified audio file with a “modified_” prefix.

Conclusion

In this blog post, we have explored how to use pydub for batch processing audio files. We covered loading audio files, applying effects, and processing multiple files at once. Pydub provides a straightforward and convenient interface for manipulating audio files in Python, making it a valuable tool for audio processing tasks.

Give pydub a try and take your audio processing projects to the next level!