Notes: audio format conversion ByPython

Recently, I bought a new headset. The store attached some non-destructive music resources of director Zhou. After receiving the goods, I couldn't wait to download and audition. I found that these resources were in wav format. After importing the player, the song title, author and album were all unknown. At that time, I wondered whether there was a problem with the store's resources. After checking the Internet, we know that wav format has no label information. Incidentally, we know that the mainstream lossless music formats include WAV, flac, ape, etc. wav is the original coding format. Because it is relatively old, without considering the addition of labels, almost any player will support playback, while flac and ape are compressed formats, which need the player to decode accordingly, However, the latter two formats are relatively new and can be labeled.

Therefore, it is planned to convert the wav to flac encoding of the file and label it. There are many conversion tools on the Internet, but I am a person who has studied python. I also use tool software for small coding conversion. First, the software is not flexible enough. Second, it can not represent the identity of my science man, so I decided to use Python for batch coding conversion and labeling.

I installed Visual Studio 2022 Community Edition, VScode and Anaconda3 on my computer and decided to debug Visual Studio 2022 first. OK, just create a project and give me an error:

can't find. NETFramework,Version=v4. Referenced assembly for 0. To fix this problem, install the developer kit (SDK / target package) for this framework version or redirect the application. Can be in https://aka.ms/msbuild/developerpacks Download from NET Framework Developer Kit

After searching the Internet, I didn't find an effective solution, but the debugging discovery doesn't affect the appearance of the program. Ha ha, it doesn't matter. However, in actual use, I found that the python interactive window of vs2022 is more card (maybe my thin book is not very good), and I don't know how to use the package management of vs2022 (I feel that the vs series is more complex), but I know Anaconda is specially used for package management, so I decided to use cmd+Anaconda3 for interactive learning and debugging, Basically find out how to write the code, and then write the program with vs2022. (it's the habit of a wild self scholar. I don't know how a professional born or formal programmer gets it)

Next, we'd better search the tutorial on the Internet to see how netizens convert audio with python. After reading several answers, we found that we all use the combination of pydub+ffmpeg (where ffmpeg is the dependent package of pydub). Let's try it first and open cmd

conda create -n AudioConversion python=3.8  
activate AudioConversion

First create a python 3 8 environment, named AudioConversion, and loaded.

pip install ipython
pip install ffmpeg
pip install pydub
ipython
import ffmpeg
import pydub

Download the required packages and enter ipython (the newly created basic environment does not have ipython. I prefer ipython with automatic completion and other functions to basic python), and then load the two packages. When loading pydub, you will be prompted:

Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work

Choose sexual neglect.

song=pydub.AudioSegment.from_wav("file.wav")
song.export("song.flac",format="flac")

According to the tutorial, first read the wav file as AudioSegment object, and then output it as flac file. The result is an error. I wonder if it doesn't support flac. Replace it with mp3 and report the same error. Considering the previous tips, I think it should be the problem of ffmpeg package.

pip uninstall ffmpeg
pip uninstall pydub
conda install ffmpeg
conda install pydub

I found that it was still not possible, so I checked the Internet and found that ffmpeg seemed to be prone to problems. I had to install another ffmpeg Python package.

conda install ffmpeg-python
import ffmpeg
impert pydub
song=pydub.AudioSegment.from_wav("file.wav")
song.export("song.flac",format="flac")

import os
os.getcwd()

Continue the process just now, load the os package, find the command running directory, open the directory, and find a new song FLAC file, click open, the display is occupied, so I exit the command window and open it again, the song can be played normally, and the format conversion is successful. Next is labeling.

>help(pydub.AudioSegment.export)
Help on function export in module pydub.audio_segment:

export(self, out_f=None, format='mp3', codec=None, bitrate=None, parameters=None, tags=None, id3v2_version='4', cover=None)
Export an AudioSegment to a file with given options

out_f (string):
    Path to destination audio file. Also accepts os.PathLike objects on
    python >= 3.6

format (string)
    Format for destination audio file.
    ('mp3', 'wav', 'raw', 'ogg' or other ffmpeg/avconv supported files)

codec (string)
    Codec used to encode the destination file.

bitrate (string)
    Bitrate used when encoding destination file. (64, 92, 128, 256, 312k...)
    Each codec accepts different bitrate arguments so take a look at the
    ffmpeg documentation for details (bitrate usually shown as -b, -ba or
    -a:b).

parameters (list of strings)
    Aditional ffmpeg/avconv parameters

tags (dict)
    Set metadata information to destination files
    usually used as tags. ({title='Song Title', artist='Song Artist'})

id3v2_version (string)
    Set ID3v2 version for tags. (default: '4')

cover (file)
    Set cover for audio file from image file. (png or jpg)

The help command checks the help document and finds that the tag information is given by the tags dictionary parameter of the export method of the AudioSegment object. Now I know how to convert the format and add label information. Next, I need a label information source (for the time being, I only know the song name - given by the file name, and the singer is Jay Chou, but I don't know other information such as lyrics, composition and album information). Baidu looks at the relevant documents of Jay Chou's songs, and it doesn't seem to find the appropriate information. However, Baidu Encyclopedia records the information of songs very well. It seems that I have to write a crawler to get the song label information.

Keywords: Python

Added by spiyun on Wed, 02 Mar 2022 17:51:49 +0200