A tutorial on mp3 format judgment with Python

Source: Internet
Author: User
Tags file size id3

The project uses the mp3 format for sound effects playback. When encountering an mp3 file in the program, the sound cannot be played alive. Finally, it was found that it was a wav format file, but ended with mp3. If you want to judge the mp3 format of the resource, how do you judge it? It is definitely not reliable to use the .mp3 suffix. You have to judge from the encoding format. The method is as follows:

1.mp3 encoding

MP3 files are a streaming file format, so there is no file header. Formats such as AVI and WAV have file headers. It is very easy to judge that they all start with RIFF. As long as the RIFF string comparison is performed, it can be found out whether it is AVI or WAV. MP3 can only analyze the encoding format. Here about the mp3 encoding rules, please refer to this article for details

The MP3 file is roughly divided into three parts: TAG_V2 (ID3V2), audio data, TAG_V1 (ID3V1)

a). ID3V2 at the beginning of the file, beginning with ID3, contains author, composition, album and other information.

b). A series of audio data frames, in the middle of the file, the number is determined by the file size and frame length; each frame starts with FFF, the length may be fixed or fixed, determined by the bitrate Each frame is divided into a frame header and a data entity. The frame header records the mp3 bit rate, sampling rate, version and other information, and each frame is independent of each other.

c). ID3V1's position at the end of the file, beginning with TAG, contains author, composition, album and other information. The length is 128Byte. It is not required.

ID3V2

Contains information such as author, composition, album, etc., the length is not fixed, which expands the information volume of ID3V1.
Frame

.

.

.

Frame

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

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

Each frame is divided into a frame header and a data entity.

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

ID3V1

Contains author, composition, album and other information, and is 128 BYTE in length.

 In other words, according to the TAG_V2 (ID3V2), audio data, and TAG_V1 (ID3V1) header information in the three structures, you can determine whether it is an mp3 encoded file.

2.python code

# coding: utf-8
 
'' '
@author: BigFengFeng
@time: 12/21/16 6:10 PM
@license: Apache Licence
@description:
 
'' '
 
importos
 
# mp3filePath is in mp3 format
defisMp3Format (mp3filePath):
 #Read string in file
 f = open (mp3filePath, "r");
 fileStr = f.read ();
 f.close ();
 head3Str = fileStr [: 3];
 
 #Determine if the beginning is ID3
 ifhead3Str == "ID3":
  returnTrue;
 
 #Decide if there is a TAG at the end
 last32Str = fileStr [-32:];
 iflast32Str [: 3] == "TAG":
  returnTrue;
 
 #Determine if the first frame starts with FFF, and turn it into a number
 # fixme should loop through each frame header so that it can be 100% judged whether it is mp3
 ascii = ord (fileStr [: 1]);
 ifascii == 255:
  returnTrue;
 
 returnFalse;
 
 
# Let's traverse folderPath to see if they are all in mp3 format,
# 是 就 true, not just false, and return a list of mp3, not a list of MP3
defisMp3FolderTraverse (folderPath):
 mp3List = [];
 notMp3List = [];
 isAllMpFormat = True;
 fordirpath, dirnames, filenamesinos.walk (folderPath):
  forfilenameinfilenames:
   path = dirpath + os.sep + filename;
   isMp3 = isMp3Format (path);
   #Judge if it is mp3 ending and it is mp3 format
   ifisMp3 == Falseandstr.endswith (path, ". mp3") == True:
    # print ("-warning: file" + path + "is not mp3 format!-");
    notMp3List.append (path);
    isAllMpFormat = False;
   else:
    mp3List.append (path);
 returnisAllMpFormat, mp3List, notMp3List;
 
 
if__name __ == '__ main__':
 isMp3Format ("s_com_click1.mp3");
 isAllMp3, mp3List, notMp3List = isMp3FolderTraverse ("sound");
 printisAllMp3;
 printmp3List;
 printnotMp3List;

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.