Pymedia is a C/C ++/Python multimedia module that can encode, decode, and play files in MP3, Ogg, Avi, and other multimedia formats, A simple Python interface is provided based on FFMPEG.
1. Download and install pymedia
The official pymedia module has not been updated for a long time and does not support the latest Python 2.7 and Python 3.0. It is quite difficult to use it. Here is a website, it contains various unofficial Python modules, including pymedia that supports Python 2.7.
Unofficial Python Library maintenance
After the page is opened, my red umbrella reports that there are malicious files. I don't know what's going on, but deleting them directly should have no effect. After downloading and installing it, everything works.
Ii. Read MP3 files
You can use a common Python function to read MP3 files,CodeAs follows:
Data = open (u ' Meiyun-an appointment on Spring Festival ' , ' RB ' )
S = f. Read (1, 10000)
Pay special attention to the second line of code. It reads the first 10000 bytes of the MP3 file, about 10 KB. Why not read the first 1000 bytes, or are all read (about 5 MB?
I searched the internet and figured out what was going on. here we need to first understand the MP3 file structure.
3. MP3 file structure
MP3 files are divided into three parts: tag_v2 (ID3v2), frame, tag_v1 (id3v1 ).
Audio data is stored at a frame and at a frame. The data length of a frame is 4 bytes, and frame is the part that stores audio data frames.
The file header and tail store some other information data, such as the album name, artist, and age. The data cannot be decoded as audio.
For pymedia,At leastYou only need to read the first audio data in the file. It is okay to read a few more frames, but the first frame must be read. OtherwiseProgramIt cannot be run.
As for where the first frame of data starts, This is not certain. Therefore, the first read usually reads a little more to ensure that the first frame can be read.
Is there a way to confirm whether the next frame has been read? Continue to look down
Iv. parse audio data frames
After reading the first 10000 bytes of data, we can parse the audio data frame and play back the data. The Code is as follows:
1 Import Pymedia. muxer as muxer
2 Dm = muxer. demuxer ( ' MP3 ' )
3 Frames = DM. parse (data)
4 Print Len (frames)
The first line of code is imported into the muxer module. muxer Google translates the code into a "synthesizer ".
The second line of code creates an MP3 demuxer object DM
The third line of code is the focus. It parses the first few frames from the first 10000 bytes of data we read and stores these data frames in the frames array.
The fourth line of code tests the length of the frames array to know how many frames of audio data are contained in the 10000 bytes.
For this song, there are two frames. If you read all the data above, 1103 is displayed, that is, this file contains 1103 frames of audio data.
5. Set Decoder
Please note that decoding and parsing are not the same thing. Check the Code:
1 Import Pymedia. Audio. acodec as acodec
2 Dec = acodec. Decoder (DM. Streams [0])
The first line of code is imported into the decoder module.
The second line of code generates the decoder object and passes in a parameter DM. Streams [0]
Here, we will explain that when we parse the data, the DM object will automatically generate DM based on the data content. the streams array actually contains an element (DM. streams [0]. Its content is as follows:
{'Index': 0, 'block _ align ': 0, 'type': 1, 'frame _ rate_base': 1, 'height': 0, 'channels ': 0, 'width': 0, 'length':-2077252342, 'sample _ rate': 0, 'frame _ rate': 25, 'bitrate': 0, 'id': 86016}
To put it bluntly, it is the file information and encoding information of the MP3 file. Here, this parameter is parsed from the file data. In fact, we can set it ourselves, as shown below:
1 Params = { ' ID ' : Acodec. getcodecid ( ' MP3 ' ), ' Bitrate ' 128000, ' Sample_rate ' 44100, ' EXT ' : ' MP3 ' , ' Channels ' : 2}
2 Dec = acodec. Decoder (Params) 6. decode the first frame of audio data and create an audio output object.
With the decoder, We can first decode the first frame of audio data and create an audio output object. The Code is as follows:
1 # 4. decode the first frame of audio data and create an output object.
2 Frame = frames [0]
3 # Audio Data is in the second element of the frame array.
4 R = dec. Decode (frame [1])
5 Print " Sample_rate: % s, channels: % s " % (R. sample_rate, R. channels)
6 Import Pymedia. Audio. Sound as sound
7 SND = sound. Output (R. sample_rate, R. channels, sound. afmt_s16_le)
The first line of code: as we have said before, the frames array stores the first few frames of audio data. Frames [0] is the first frame of audio data.
Line 2 code: strictly speaking, frames [0] is also an array containing five elements. The second element frames [0] [1] is the real audio data, this is just a design of pymedia and has nothing to do with the data structure of MP3 audio frames.
Line 6 and line 7 create an audio output object
7. Play, read, and decode... loop down
Now we have the audio output object, and the decoded data of the first frame can be directly played.
# 6. Play
If R: SND. Play (R. Data)
Then continue reading, decoding, and playing data. The subsequent steps are simple. pymedia will automatically find the data frame.
# 7. Continue reading, decoding, and playing
While True:
Data = f. Read (1, 512)
If Len (data)> 0:
R = dec. Decode (data)
If R: SND. Play (R. Data)
Else :
Break
After reading the data of the last frame, the program must be delayed for a while before exiting. Otherwise, the last frame of data will not be played and the program will end.
1 Import Time
2 While SND. isplaying (): Time. Sleep (. 5) complete code # 1. the binary method reads the first 10000 bytes to ensure that the first frame of audio data can be read.
F = open (u ' Meiyun-an appointment on Spring Festival ' , ' RB ' )
Data = f. Read (1, 10000)
#2. Create a synthesizer object to parse the initial audio data of several frames.
ImportPymedia. muxer as muxer
Dm = muxer. demuxer ('MP3')
Frames = DM. parse (data)
PrintLen (frames)
# 3. create a decoder object
Import pymedia. audio. acodec as acodec
dec = acodec. decoder (DM. streams [0])
# same as the following
# Params = {'id ': acodec. getcodecid ('mp3'), 'bitrate': 128000, 'sample _ rate': 44100, 'text': 'mp3', 'channel ': 2}
# dec = acodec. decoder (Params)
#4. decode the first frame of audio data
Frame = frames [0]
#Audio Data is in the second element of the frame array.
R = dec. Decode (frame [1])
Print "Sample_rate: % s, channels: % s"% (R. sample_rate, R. channels)
#Note: This step can directly decode r = dec. Decode (data) without reading the first frame of audio data.
#However, there will be noise at the beginning. If the network stream is pure audio data and does not contain tag information, there will be no noise.
#5. Create an audio output object
ImportPymedia. Audio. Sound as sound
SND = sound. Output (R. sample_rate, R. channels, sound. afmt_s16_le)
#6. Play
IfR: SND. Play (R. Data)
#7. Continue reading, decoding, and playing
WhileTrue:
Data = f. Read (1, 512)
IfLen (data)> 0:
R = dec. Decode (data)
IfR: SND. Play (R. Data)
Else:
Break
#8. delay until playback is complete
ImportTime
WhileSND. isplaying (): Time. Sleep (. 5)