Python for MP3 format judgment

Source: Internet
Author: User
Tags id3

Python for MP3 format judgment

The project uses the MP3 format for sound playback, encountered a MP3 file in the program or die out sound, and finally found that it is WAV format file, but the end of MP3. To determine the MP3 format of the resources, then how to judge, with the. mp3 suffix is definitely not reliable, you have to judge from the encoding format, the method is as follows:

1.mp3 encoding

The MP3 file is a streaming media file format, so there is no file header. Like AVI, WAV this has the file header format, very good judgment, they are riff beginning, as long as the riff string comparison, you can find out whether it is AVI, WAV, and MP3 can only analyze the encoding format. Here is probably said MP3 coding rules, a detailed reference to this article

The MP3 file is broadly divided into three parts:tag_v2 (ID3v2), audio data, TAG_V1 (ID3V1)

a). ID3v2 at the beginning of the file, beginning with the ID3 , contains the author, composition, album and other information, length is not fixed, expanded the amount of ID3V1, not required

b). A series of audio data frames, in the middle position of the file, the number is determined by the size of the file and the length of the frame, each frame starts with FFF, may be fixed, or is determined by the bit rate bitrate; each frame is divided into frame head and data entity two parts The frame head records information such as MP3, sample rate, version, and so on, each frame is independent of each other.

c). ID3v1 at the end of the file, starting with tag , including the author, composition, album and other information, length 128Byte, not required .

ID3v2

Contains the author, composition, album and other information, the length is not fixed, expanding the amount of id3v1.

Frame

.

.

.

Frame

A series of frames, the number of which is determined by file size and frame length

The length of each frame may not be fixed, or it may be fixed, determined by the bit rate bitrate

Each frame is divided into frames header and data entity two parts

The frame header records the MP3, sample rate, version and other information, each frame is independent of each other.

ID3v1

Contains the author, composer, album and other information, the length of 128BYTE.

In other words, according to Tag_v2 (ID3v2), audio data, TAG_V1 (ID3V1) three structure of the beginning of the information, you can determine whether MP3 encoded files.

2.python Code
1 #Coding:utf-82 3 " "4 @author: Bigfengfeng5 @time: 16/12/21 pm 6:106 @license: Apache Licence7 @description:8 9 " "Ten  One ImportOS A  - #whether the Mp3filepath is in the MP3 format - defIsmp3format (mp3filepath): the     #reading a string inside a file -f = open (Mp3filepath,"R"); -Filestr =F.read (); - f.close (); +Head3str = Filestr[:3]; -  +     #Judging whether the beginning is ID3 A     ifHead3str = ="ID3": at         returnTrue; -  -     #The end of the judgment has no tag -Last32str = filestr[-32:]; -     ifLast32str[:3] = ="TAG": -         returnTrue; in  -     #determine if the first frame is a fff start and turn into a number to     #Fixme should iterate through each frame head so that it can be 100% judged if it is mp3 +ASCII = Ord (filestr[:1]); -     ifASCII = = 255: the         returnTrue; *  $     returnFalse;Panax Notoginseng  -  the #Traverse FolderPath to see if it's all in MP3 format, + #is true, not just false, and returns the list of MP3, not the MP3 list A defIsmp3foldertraverse (folderpath): theMp3list = []; +Notmp3list = []; -Isallmpformat =True; $      forDirpath, Dirnames, filenamesinchOs.walk (folderpath): $          forFileNameinchFilenames: -Path = Dirpath + Os.sep +filename; -ISMP3 =Ismp3format (path); the             #Judging is not mp3 ending and is in MP3 format -             ifISMP3 = = False andStr.endswith (Path,". mp3") ==True:Wuyi                 #print ("--warning:file" + path + "is not MP3 format!--"); the notmp3list.append (path); -Isallmpformat =False; Wu             Else: - mp3list.append (path); About     returnIsallmpformat, Mp3list, notmp3list; $  -  - if __name__=='__main__': -Ismp3format ("S_com_click1.mp3"); +ISALLMP3, mp3list, notmp3list = Ismp3foldertraverse (" Sound"); the     PrintisAllMp3; -     Printmp3list; $     PrintNotmp3list;

Python for MP3 format judgment

Related Article

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.