Python audio processing library pydub tutorial
- 2020-06-03 07:09:23
- OfStack
preface
pydub is a library in Python for users to process audio files. This paper mainly introduces the content related to the use of Python audio processing library pydub, and shares it for your reference and study. Here is a detailed introduction:
Installation:
1. Installation of pip tool:
sudo apt-get install python-pip
2. pydub installation:
sudo pip install pydub
3. pydub depends on ffmpeg, so ffmpeg needs to be installed. Since es23EN14.04 official source has been removed, ppa source is installed:
sudo apt-add-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get install ffmpeg
Use:
The AudioSegment method is able to open an audio file into the AudioSegment example and process the audio using various methods, calling before use
from pydub import AudioSegment
Open audio:
sound1 = AudioSegment.from_file("/path/to/sound.wav", format="wav") // The default mp3 format
sound2 = AudioSegment.from_file("/path/to/another_sound.mp3", format="mp3") Is equivalent to sound1
= AudioSegment.from_mp3("/path/to/sound.mp3")
Volume handling:
louder = sound1 + 6 //sound1 Sounds, 6dB
quieter = sound1 - 6 //sound1 Voice down 6dB
combined = sound1 + sound2 //sound1 and sound2 The superposition
duration_in_milliseconds = len(sound1) // To obtain sound The length of the
beginning = sound1[:5000] // To obtain sound1 The former 5 Second audio data
end = sound1[-5000:] // To obtain sound1 After the 5 Second audio data
Note:
1. For the calculation of multiple audio, the number of channels, frames, sampling rate and the number of bits between multiple audio files should be 1. Otherwise, low-quality audio will be converted to high-quality audio, mono will be converted to stereo, and low frame number will be converted to high frame number.
2, AudioSegment native support wav and raw, if other files need to install ffmpeg. raw also needs sample_width, frame_rate, channels3.
Generated file:
export()
Method to convert 1 AudioSegment object to 1 file.
sound = AudioSegment.from_file("/path/to/sound.wav", format="wav")
file_handle = sound.export("/path/to/output.mp3", format="mp3") // Simple output
file_handle = sound.export("/path/to/output.mp3",
format="mp3",
bitrate="192k",
tags={"album": "The Bends", "artist": "Radiohead"}) // Complex output
AudioSegment. empty () :
AudioSegment.empty()
Used to generate 1 AudioSegment object of length 0, 1 is generally used for multiple audio merges.
sounds = [
AudioSegment.from_wav("sound1.wav"),
AudioSegment.from_wav("sound2.wav"),
AudioSegment.from_wav("sound3.wav"),
]
playlist = AudioSegment.empty()
for sound in sounds:
playlist += sound
AudioSegment. silent () :
ten_second_silence = AudioSegment.silent(duration=10000) // produce 1 The duration of is 10s The silent AudioSegment object
Get parameters:
In addition, the parameters of the audio can be obtained through AudioSegment, while the original parameters can also be modified.
Specific see: https: / / github com jiaaro/pydub blob/master/API markdown
conclusion