Python is used to determine the mp3 format and pythonmp3 format.

Source: Internet
Author: User
Tags id3

Python is used to determine the mp3 format and pythonmp3 format.

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) the position where ID3V2 starts with ID3, which contains the author, composing, album, and other information. The length is not fixed, and the information of ID3V1 is extended, which is not required.

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, according to the start information in the TAG_V2 (ID3V2), audio data, and TAG_V1 (ID3V1) structure, you can determine whether the file is mp3 encoded.

2. python code

# Coding: UTF-8 ''' @ author: BigFengFeng @ time: 16/12/21 @ license: Apache Licence @ description: '''import OS # mp3filePath: # Read the file string f = open (mp3filePath, "r"); fileStr = f. read (); f. close (); head3Str = fileStr [: 3]; # determine whether the start is ID3 if head3Str = "ID3": return True; # determine whether there is a TAG last32Str = fileStr [-32:] at the end; if last32Str [: 3] = "TAG": return True; # determine whether the first frame starts with FFF and converts it to a number # Fixme should traverse each frame header cyclically so that 100% can judge whether it is mp3 ascii = ord (fileStr [: 1]); if ascii = 255: return True; return False; # traverse the folderPath to see if it is in mp3 format. # If it is true or false, the system returns an mp3 list instead of an 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 = isMp3Form At (path); # determine if it is an mp3-formatted 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 all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.