1. install
2.mp3 turn wav Or other formats
3. More general conversion functions
4. Batch conversion of audio formats
Pydub It's based on ffmpeg Of Python Audio processing module , It encapsulates a lot of ffmpeg Bottom interface , So it is very convenient to use it to convert the file format of music songs , If you have read our previous articles :3 That's ok Python Code implementation of music editing You know how powerful it is .
Today I will introduce its music file format conversion function , Almost all music audio formats are supported , Very powerful .
1. installinstall Pydub Installation is required ffmpeg:
Mac ( Open the terminal (Terminal), use homebrew install ):
brew install ffmpeg --with-libvorbis --with-sdl2 --with-theora
Linux:
apt-get install ffmpeg libavcodec-extra
Windows:
1. Get into http://ffmpeg.org/download.html#build-windows, Click on windows The corresponding icon , Enter the download interface and click download Download button ,
2. Unzip download okay zip File to specified directory
3. Put the extracted files in the directory bin Catalog ( contain ffmpeg.exe ) Added to the path In the environment variables
Above ffmpeg After the installation is successful, you can open the command prompt (cmd), install pydub:
pip install pydub
2.mp3 turn wav Or other formats Will single mp3 Convert audio files to wav Audio format :
from pydub import AudioSegmentdef trans_mp3_to_wav(filepath): """ take mp3 File to wav Format Args: filepath (str): File path """ song = AudioSegment.from_mp3(filepath) filename = filepath.split(".")[0] song.export(f"{filename}.wav", format="wav")
The code font in the wechat official account is too big , And sometimes there is no highlight reminder , Here is a picture version of the code ( The same below ):
You can continue to encapsulate this function , Will single mp3 Convert the file to any other music audio format :
from pydub import AudioSegmentdef trans_mp3_to_any_audio(filepath, audio_type): """ take mp3 Convert the file to any audio file format Args: filepath (str): File path audio_type(str): File format """ song = AudioSegment.from_mp3(filepath) filename = filepath.split(".")[0] song.export(f"{filename}.{audio_type}", format=f"{audio_type}")
Picture version code :
Such as ogg Format :
trans_mp3_to_any_audio("Alone.mp3", "ogg")
As long as it is ffmpeg Supported music audio formats , It can be converted , Dozens of formats are supported , Let me briefly list some :
wavavimp4flvoggflacapemp2aiffvocau 3. More general conversion functionsIt was mp3 To any audio format , I want to write it in any audio format to any audio format :
from pydub import AudioSegmentdef trans_any_audio_types(filepath, input_audio_type, output_audio_type): """ Convert any audio file format to any audio file format Args: filepath (str): File path input_audio_type(str): Input audio file format output_audio_type(str): Output audio file format """ song = AudioSegment.from_file(filepath, input_audio_type) filename = filepath.split(".")[0] song.export(f"{filename}.{output_audio_type}", format=f"{output_audio_type}")
Picture version code :
For example, will ogg Music audio format is converted to flv Music audio format :
trans_any_audio_types("Alone.ogg", "ogg", "flv")
perhaps MP4 Format , All in all , Generally speaking, it can satisfy all the formats you need .
trans_any_audio_types("Alone.ogg", "ogg", "mp4")
4. Batch conversion of audio formats Now? , Try to put all the non - in a folder mp3 Audio format file into mp3 Audio format :
def trans_all_file(files_path, target="mp3"): """ Batch conversion of audio music formats Args: files_path (str): Folder path target (str, optional): Target music format . Defaults to "mp3". """ for filepath in os.listdir(files_path): # Path processing modpath = os.path.dirname(os.path.abspath(sys.argv[0])) datapath = os.path.join(modpath, files_path + filepath) # Split into file names and suffixes and load the file input_audio = os.path.splitext(datapath) song = AudioSegment.from_file(datapath, input_audio[-1].split(".")[-1]) # export song.export(f"{input_audio[0]}.{target}", format=target)
Picture version code :
Just enter the folder name , You can convert all the music files in this folder to mp3 Format :
trans_all_file("F:\\push\\20200607\\music\\")
This is about Python This is the end of the article on batch conversion of various music formats , More about Python Please search the previous articles of SDN or continue to browse the related articles below. I hope you will support SDN more in the future !