[파이썬] pydub 오디오의 사운드 이펙트 추가

PyDub is a powerful audio processing library for Python that makes it easy to manipulate and analyze audio files. One of its key features is the ability to add sound effects to audio files. In this blog post, we will explore how to use PyDub to add various sound effects to your audio files.

Installation

To get started, you first need to install PyDub. You can do this by running the following command:

pip install pydub

PyDub has some dependencies that will also be installed automatically.

Adding Sound Effects

Once you have PyDub installed, you can begin adding sound effects to your audio files. PyDub provides a wide range of built-in sound effects that you can apply. Some of the popular sound effects include:

Let’s take a look at how to apply some of these sound effects using PyDub.

Example: Fade In/Out Effect

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_mp3("input.mp3")

# Apply fade in effect (duration in milliseconds)
fade_in_audio = audio.fade_in(2000)

# Apply fade out effect (duration in milliseconds)
fade_out_audio = audio.fade_out(2000)

# Save the modified audio with fade in/out effects
fade_in_audio.export("output_fade_in.mp3", format="mp3")
fade_out_audio.export("output_fade_out.mp3", format="mp3")

In the above example, we load an audio file using AudioSegment.from_mp3() function. Then, we apply the fade in effect using fade_in() method with a duration of 2000 milliseconds. Similarly, we apply the fade out effect using fade_out() method. Finally, we export the modified audio with the fade in/out effects using export() method.

Example: Speed Up/Slow Down

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_wav("input.wav")

# Increase the speed by 20%
speed_up_audio = audio.speedup(playback_speed=1.2)

# Decrease the speed by 30%
slow_down_audio = audio.speedup(playback_speed=0.7)

# Save the modified audio with speed up/slow down effects
speed_up_audio.export("output_speed_up.wav", format="wav")
slow_down_audio.export("output_slow_down.wav", format="wav")

In the above example, we load an audio file using AudioSegment.from_wav() function. Then, we increase the speed of the audio by 20% using speedup() method. Similarly, we decrease the speed of the audio by 30%. Finally, we export the modified audio with the speed up/slow down effects using export() method.

Example: Pitch Shift

from pydub import AudioSegment

# Load the audio file
audio = AudioSegment.from_ogg("input.ogg")

# Shift the pitch by 3 semitones
pitch_shifted_audio = audio + 3

# Save the modified audio with pitch shift effect
pitch_shifted_audio.export("output_pitch_shifted.ogg", format="ogg")

In the above example, we load an audio file using AudioSegment.from_ogg() function. Then, we shift the pitch of the audio by 3 semitones using + operator. Finally, we export the modified audio with the pitch shift effect using export() method.

Conclusion

In this blog post, we have explored how to add sound effects to audio files using PyDub. By leveraging the power of PyDub, you can easily enhance your audio files with various sound effects like fade in/out, speed up/slow down, pitch shift, echo, reverb, and equalizer. You can now experiment with different sound effects and create unique audio experiences. Happy coding!