This is the second day of my participation in Gwen Challenge
When dealing with audio formats, you need to download various audio processing software (there are fees for professional software). Those of us who know Python know that Python can do everything (ha ha ha, just kidding). Audio format lossless conversion in Python
Pydub supports conversion between audio formats. The demo looks like this (only 3 formats are shown here, and various common audio formats are described below) :
01 Install the PyDub library
Install the Python library -Pydub with the following command
pip install pydub
Copy the code
Once installed, introduce PyDub into Python
from pydub import AudioSegment
Copy the code
02 This section describes common audio formats
Here are some common audio formats, each with its own characteristics. Let’s start converting between audio formats programmatically.
1. Mp3 -> WAV, OGG, flag, etc
from pydub import AudioSegment
# transform function method
def trans_mp3_to_wav(filepath,hz) :
song = AudioSegment.from_mp3(filepath)
song.export("Python researcher."+str(hz), format=str(hz))
Parameter 1: audio path, parameter 2: converted format
trans_mp3_to_wav("Light music.mp3"."wav")
trans_mp3_to_wav("Light music.mp3"."ogg")
trans_mp3_to_wav("Light music.mp3"."flac")
Copy the code
Transformation results:
Convert light music. Mp3 to WAV, OGG, FLAC, and name it Python Researcher. After the conversion are lossless high tone quality, without any distortion.
Wav, OGG, FLAC ->mp3
song = AudioSegment.from_wav("Python researcher. Wav.")
song.export("Python researcher _wav-mp3.mp3".format="mp3")
song = AudioSegment.from_ogg("Python researcher. Ogg")
song.export("Python researcher _ogg-mp3.mp3".format="mp3")
AudioSegment.from_file("Python researcher. Flac")
song.export("Python researcher _flac-mp3.mp3".format="mp3")
Copy the code
After testing, WAV, OGG and FLAC can also be successfully converted to MP3, proving that direct conversion between different audio formats is possible.
03 summary
Here is only a demonstration of MP3, FLAC, WAV, OGG these four formats, the rest of the other formats we can start to try, here is not repetitive repetitive.