Python judges the mp3 format and pythonmp3 format

Source: Internet
Author: User
Tags id3

Python judges the mp3 format and pythonmp3 format
Determine mp3 format using python

The project uses the mp3 format for sound playback. When an mp3 file does not play sound in the program, it is found to be a wav file, but ended with an mp3 file. To import a resource in the MP3 format, you must determine the encoding format by using the suffix. The method is as follows:

1.mp3 Encoding

MP3 is a streaming media file format, so there is no file header. The format with file headers such as AVI and WAV is well judged. They all start with RIFF. If you compare the RIFF strings, you can check whether they are AVI and WAV, mp3 can only analyze the encoding format. Here we will talk about the mp3 encoding rules. For details, refer to this article.

MP3 files are divided into three parts:TAG_V2 (ID3V2), audio data, TAG_V1 (ID3V1)

A ).ID3V2 starts with ID3 in the position where the file starts. It contains the author, composing, album, and other information. The length is not fixed. It extends the ID3V1 information and is optional.

B ).Frames of a series of audio data are in the middle of a file. The number of frames is determined by the file size and frame length. Each frame starts with FFF and may be unfixed or fixed, the bitrate is determined by the bit rate. Each frame is divided into two parts: the frame header and the data entity. The frame header records information such as the bit rate, sampling rate, and version of mp3, and each frame is independent of each other.

C ).The position of ID3V1 at the end of the file. It starts with a TAG and contains information such as the author, composing, and album. The length is 128 bytes, not required.

ID3V2

Contains the author, composing, album, and other information. The length is not fixed, and the information of ID3V1 is extended.

Frame

.

.

.

Frame

The number of frames is determined by the file size and frame length.

The length of each FRAME may be unfixed or fixed, determined by bitrate

Each FRAME is divided into two parts: the FRAME header and the data entity.

The frame header records information such as the bit rate, sampling rate, and version of mp3. Each frame is independent of each other.

ID3V1

Contains the author, composing, and album information. The length is 128 bytes.

That is to say, accordingTAG_V2 (ID3V2), audio data, TAG_V1 (ID3V1)The start information in the three structures can be used to determine whether an mp3-encoded file is used.

2. python code
1 # coding: UTF-8 2 3 ''' 4 @ author: BigFengFeng 5 @ time: 16/12/21 pm 6 @ license: Apache Licence 7 @ description: 8 9 ''' 10 11 import os12 13 # Whether mp3filePath is a 14 def isMp3Format (mp3filePath) in mp3 format: 15 # Read the character string in the file 16 f = open (mp3filePath, "r"); 17 fileStr = f. read (); 18 f. close (); 19 head3Str = fileStr [: 3]; 20 21 # determine whether the start is ID322 if head3Str = "ID3": 23 return True; 24 25 # determine whether there is a TAG26 last32Str = fileStr [-32: ]; 27 if last32Str [: 3] = "TAG": 28 return True; 29 30 # determine whether the first frame starts with FFF, convert to the number 31 # fixme should traverse each frame header cyclically so that 100% can be used to determine whether it is mp332 ascii = ord (fileStr [: 1]); 33 if ascii = 255: 34 return True; 35 36 return False; 37 38 39 # traverse folderPath to see if all formats are mp3. 40 # true is true, false is not, and the mp3 list is returned, list41 def isMp3FolderTraverse (folderPath) Not MP3: 42 mp3List = []; 43 notMp3List = []; 44 isAllMpFormat = True; 45 for dirpath, dirnames, file Names in OS. walk (folderPath): 46 for filename in filenames: 47 path = dirpath + OS. sep + filename; 48 isMp3 = isMp3Format (path); 49 # determine if it is an mp3-ended 50 if isMp3 = False and str. endswith (path ,". mp3 ") = True: 51 # print (" -- warning: file "+ path +" is not mp3 format! -- "); 52 notMp3List. append (path); 53 isAllMpFormat = False; 54 else: 55 mp3List. append (path); 56 return isAllMpFormat, mp3List, notMp3List; 57 58 59 if _ name _ = '_ main _': 60 isMp3Format ("s_com_click1.mp3 "); 62 isAllMp3, mp3List, notMp3List = isMp3FolderTraverse ("sound"); 63 print isAllMp3; 64 print mp3List; 65 print notMp3List;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.