Detailed python for MP3 format judgment

Source: Internet
Author: User
Tags id3
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 not be fixed or fixed, is determined by the bit rate bitrate, each frame is divided into two parts: frame head and Data entity, the frame Head records the MP3 bitrate, version, and 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

# coding:utf-8 ' @author: bigfengfeng@time:16/12/21 pm 6:10@license:apache licence@description: ' Import os# Mp3filepath is the Def ismp3format (Mp3filepath) in the MP3 format: #读取文件内字符串 f = open (Mp3filepath, "R"); Filestr = F.read (); F.close (); Head3str = Filestr[:3]; #判断开头是不是ID3 if head3str = = "ID3": return True; #判断结尾有没有TAG last32str = filestr[-32:]; If last32str[:3] = = "TAG": return True; #判断第一帧是不是FFF开头, turn into a number # Fixme should loop through each frame head, so as to 100% judge is not mp3 ASCII = ord (filestr[:1]); if ASCII = = 255:return True; return false; #遍历folderPath看看是不是都是mp3格式的, #是就true, is not False, and returns a list of MP3, not MP3 listdef ismp3foldertraverse ( FolderPath): Mp3list = []; Notmp3list = []; Isallmpformat = True;    For Dirpath, Dirnames, filenames in Os.walk (folderpath): for filename in filenames:path = dirpath + os.sep + filename;   ISMP3 = Ismp3format (path);  #判断是不是mp3结尾的 and is the MP3 format if isMp3 = = False and Str.endswith (Path, ". mp3") = = True: # print ("--warning:file" + path +    "is not MP3 format!--");   Notmp3list.append (path); Isallmpformat = False; Else:mp3List.append (path); Return Isallmpformat, mp3list, notmp3list;if __name__ = = ' __main__ ': Ismp3format ("S_com_click1.mp3"); ISALLMP3, mp3list, notmp3list = Ismp3foldertraverse ("sound"); Print isAllMp3; Print mp3list; Print notmp3list;

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

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.