Mp3文件结构全解析(一)

Mp3文件结构全解析(一)MP3 文件是由帧 frame 构成的 帧是 MP3 文件最小的组成单位

大家好,欢迎来到IT知识分享网。

Mp3文件结构全解析(一)

整体结构

MP3 文件大体分为四部分:TAG_V2(ID3V2),Frame, TAG_V1(ID3V1),其他说明信息

  • ID3V2 包含了作者,作曲,专辑等信息,长度不固定,扩展了ID3V1 的信息量。
  • Frame 一系列的帧,个数由文件大小和帧长决定,每个FRAME 的长度可能不固定,也可能固定,由位率bitrate 决定, 每个FRAME 又分为帧头和数据实体两部分,帧头记录了mp3 的位率,采样率,版本等信息,每个帧之间相互独立
  • ID3V1 包含了作者,作曲,专辑等信息,长度为128BYTE。
  • 其他说明信息(不重要)

ID3V2解析

ID3V2一共有四个版本,ID3V2.1/2.2/2.3/2.4,目前流行的播放软件一般只支持第三版即ID3V2.3,由于ID3V1记录在文件的末尾处,ID3V2就只能记录在文件的首部了,也是因为这个原因,对ID3V2的操作比ID3V1要慢,而且ID3V2的结构比ID3V1的结构复杂的多,但是ID3V2可以记录更多的信息,长度可变

ID3V2 主要有四个版本,现在主流的是ID3V2.3,ID3V3.4, 2.2,2.1与后面版本差异较大:2.2的Frame head id是三个字节,而2.3,2.4是四个字节.

ID3V2.3由一个标签头和若干个标签帧或者一个扩展标签头组成,至少要有一个标签帧,每一个标签帧记录一种信息,例如作曲、标题等

ID3V2.4 Overall tag structure:

 +-----------------------------+ | Header (10 bytes) | +-----------------------------+ | Extended Header | | (variable length, OPTIONAL) | +-----------------------------+ | Frames (variable length) | +-----------------------------+ | Padding | | (variable length, OPTIONAL) | +-----------------------------+ | Footer (10 bytes, OPTIONAL) | +-----------------------------+ 

标签头

位于文件开始处,长度为10字节,结构如下:

  • char Header[3]; /*必须为“ID3”否则认为标签不存在*/
  • char Ver; /*版本号ID3V2.3 就记录3*/
  • char Revision; /*副版本号此版本记录为0*/
  • char Flag; /*标志字节,只使用高三位,其它位为0 */
  • char Size[4]; /*标签大小*/

注:标签大小,不能确定具体包括哪些内容,解析歌曲文件后,发现没有哪些字节之和会等于该值,详见下面的实例分析

标志字节一般为0,定义如下(abc000000B)

  • a:表示是否使用Unsynchronisation
  • b:表示是否有扩展头部,一般没有,所以一般也不设置
  • c:表示是否为测试标签,99.99%的标签都不是测试标签,不设置

标签帧

每个标签帧都有10个字节的帧头(和标签头不是一个东西,虽然他们刚好都是10字节,标签头只有一个,每个标签帧都有一个帧头)和至少一个字节的内容构成,标签帧与标签头/其他标签帧无特殊字节分割,只能通过帧头信息来确定帧内容的大小。

帧头长度10字节,定义如下:

  • char ID[4]; /*标识帧,说明其内容,例如作者/标题等*/
  • char Size[4]; /*帧内容的大小,不包括帧头,不得小于1*/
  • char Flags[2]; /*标志帧,只定义了6 位*/

标识帧,常见的内容如下:

  • TIT2=标题
  • TPE1=作者
  • TALB=专集
  • TRCK=音轨格式:N/M 其中N为专集中的第N首,M为专集中共M首,N和M 为ASCII 码表示的数字
  • TYER=年代是用ASCII 码表示的数字
  • TCON=类型直接用字符串表示
  • COMM=备注格式:“eng\0备注内容”,其中eng 表示备注所使用的自然语言

ID3V2.4中,帧内容大小,计算公式如下:

Size=(Size[0]&0x7F)*0x+(Size[1]&0x7F)*0x4000+(Size[2]&0x7F)*0x80+(Size[3]&0x7F) 

很多文章是这样的:

Size = Size[0]*0x + Size[1]*0x10000+ Size[2]*0x100 +Size[3]; 
  • a – 标签保护标志,设置时认为此帧作废
  • b – 文件保护标志,设置时认为此帧作废
  • c – 只读标志,设置时认为此帧不能修改
  • x – 压缩标志,设置时一个字节存放两个BCD 码表示数字
  • y– 加密标志
  • z– 组标志,设置时说明此帧和其他的某帧是一组
    在这里插入图片描述

ID3V1解析

实例分析

歌曲信息

<<胡广生.mp3>>

结构解析

标签头

在这里插入图片描述

标签头:10字节,00H-09H

1 2 3 4 5 6 7 8 9 10
49 44 33 04 00 00 00 03 10 27
I(73) D(68) 3(51) 4(V2.4) 0(revision) 0(flag) 0x00 0x03 0x10 0x27
TIT2标签帧

在这里插入图片描述

地址 字节 说明
AH-DH 4 54 49 54 32 TIT2标签帧说明
EH-11H 4 00 00 00 09 帧内容大小: 9个字节
12H-13H 2 00 00 标志,均为0
14H- 1CH 9 01 FF FE E1 80 7F 5E 1F 75 帧内容
TALB标签帧

在这里插入图片描述

地址 字节 说明
1dH-20H 4 54 41 4C 42 TALB标签帧说明
21H-24H 4 00 00 00 09 帧内容大小: 9个字节
25H-26H 2 00 00 标志,均为0
27H- 2FH 9 01 FF FE E1 80 7F 5E 1F 75 帧内容
TPE1标签帧

在这里插入图片描述

地址 字节 说明
30H-33H 4 54 50 45 31 TPE1标签帧说明
34H-37H 4 00 00 00 09 帧内容大小: 9个字节
38H-39H 2 00 00 标志,均为0
27H- 2FH 9 01 FF FE FB 4E 20 7D 50 6C 帧内容
APIC标签帧

This frame contains a picture directly related to the audio file. Image format is the MIME type and subtype for the image. In the event that the MIME media type name is omitted, “image/” will be implied. The “image/png” or “image/jpeg” picture format should be used when interoperability is wanted. Description is a short description of the picture, represented as a terminated textstring. The description has a maximum length of 64 characters, but may be empty. There may be several pictures attached to one file, each in their individual “APIC” frame, but only one with the same content descriptor. There may only be one picture with the picture type declared as picture type $01 and $02 respectively. There is the possibility to put only a link to the image file by using the ‘MIME type’ “–>” and having a complete URL instead of picture data. The use of linked files should however be used sparingly since there is the risk of separation of files.

<Header for 'Attached picture', ID: "APIC"> Text encoding $xx MIME type <text string> $00 Picture type $xx Description <text string according to encoding> $00 (00) Picture data <binary data> 

Picture type:

$00 Other $01 32x32 pixels 'file icon' (PNG only) $02 Other file icon $03 Cover (front) $04 Cover (back) $05 Leaflet page $06 Media (e.g. lable side of CD) $07 Lead artist/lead performer/soloist $08 Artist/performer $09 Conductor $0A Band/Orchestra $0B Composer $0C Lyricist/text writer $0D Recording Location $0E During recording $0F During performance $10 Movie/video screen capture $11 A bright coloured fish $12 Illustration $13 Band/artist logotype $14 Publisher/Studio logotype 

在这里插入图片描述

地址 字节 说明
43H-46H 4 41 50 49 43 APIC标签帧说明
47H-4AH 4 00 01 43 65 帧内容大小: 83789个字节
38H-39H 2 00 00 标志,均为0
27H- 2FH 9 00 69 … 帧内容
帧内容大小:

Size = Size[0]*0x00000000 + Size[1]*0x10000+ Size[2]*0x100 +Size[3]

=0x010x10000+0x430x100+0x65

=0x10000+0x4300+0x65

=0x14365=82789

实现一个MP3解析器之标签解析

根据上面分析,先要读取标签头,先读取十个字节的标签头,定义一个结构体:

struct tagheader { char ID[3]; // The first 4 bytes should be ID3 char version[2]; // $03 00 char flags; // $abc00000 : a:unsynchronisation if set; b:extended header exist if set; c:experimental indicator if set char size[4]; // (total tag size - 10) excluding the tagheader; }; 

读取十个字节:

struct tagheader header; size_t sz; if (read(fd, &header, sizeof(header)) < 0) { perror("Read File: "); exit(1); } 

计算标签大小:

sz = (header.size[0] & 0x7F) * 0x + (header.size[1] & 0x7F) * 0x400 + (header.size[2] & 0x7F) * 0x80 + (header.size[3] & 0x7F); 

接下来遍历读取每一个标签帧,每个标签帧又有十个字节的标签帧头,同样定义一个结构体:

struct frameheader { char frameid[4]; // TIT2 MCDI TRCK ... char size[4]; char flags[2]; // %abc00000 %ijk00000 | a 0:frame should be preserved 1:frame should be discard }; 

接下来循环读取标签帧头:

struct frameheader header; int framesz = 0; int start = 0; while(start <= tagsize){ if (read(fd, &header, sizeof(header)) < 0) { perror("Read File: "); return -1; } start += sizeof(header); if(start > tagsize){ printf("has parsed all\n"); return 1; } framesz = header.size[0]*0x + header.size[1]*0x10000 + header.size[2]*0x100 + header.size[3]; start+=framesz; ... 

计算到标签body大小后,读取标签大小内容,并且转换成可读内容:

 ... char* input = malloc(framesz); if(read(fd, input, framesz)< 0){ printf("error"); return -1; } if(strncmp(header.frameid, "APIC", 4) == 0){//attach picture是图片内容,不可以转为文字 printf("The %s is:\t\t Attach Picture, size = %d\n", header.frameid, framesz); continue; } framesz -= 1; // framesz include the encode of the ID, so minus the encode byte; depend on the type of the tagid. if (framesz <= 0) continue; char* encode = (*input == 1)? "UTF-16" : "GB18030"; // The biggest problem is here, hard to know encode,so just guess size_t outsize = framesz * 2; char* result = malloc(outsize); bzero(result, outsize); char *temp = input+1; if (strncmp(header.frameid, "PRIV", 4) == 0) { // PRIV's handle is a bit of special, still don't understand encode = "ISO-8859-1"; temp = input; framesz +=1; } if ((doconv(temp, framesz, encode, result, outsize)) == -1) { perror("doconv: "); continue; } printf("The %s is:\t\t%s, size = %d\n", header.frameid, result, framesz); free(result); } 

附录

帧标识

  • AENC:Audioencryption
  • APIC:Attached picture
  • COMM:Comments
  • COMR:Commercial
  • ENCR:Encryptionmethod registration
  • EQUA:Equalization
  • ETCO:Event timingcodes
  • GEOB:Generalencapsulated object
  • GRID:Groupidentification registration
  • IPLS:Involvedpeople list
  • LINK:Linkedinformation
  • MCDI:Music CDidentifier
  • MLLT:MPEGlocationlookup table
  • OWNE:Ownership
  • PRIV:Private
  • PCNT:Playcounter
  • POPM:Popularimeter
  • POSS:Positionsynchronisation
  • RBUF:Recommendedbuffer size
  • RVAD:Relativevolume adjustment
  • RVRB:Reverb
  • SYLT:Synchronizedlyric/text
  • SYTC:Synchronizedtempo codes
  • TALB:Album/Movie/Showtitle
  • TBPM:BPM(beats perminute)
  • TCOM:Composer
  • TCON:Content type
  • TCOP:Copyrightmessage
  • TDAT:Date
  • TDLY:Playlistdelay
  • TENC:Encoded by
  • TEXT:Lyricist/Textwriter
  • TFLT:Filetype
  • TIME:Time
  • TIT1:Content groupdeion
  • TIT2:Title/songname/contentdeion
  • TIT3:Subtitle/Deionrefinement
  • TKEY:Initial key
  • TLAN:Language(s)
  • TLEN:Length
  • TMED:Media type
  • TOAL:Originalalbum/movie/show title
  • TOFN:Originalfilename
  • TOLY:Originallyricist(s)/text writer(s)
  • TOPE:Originalartist(s)/performer(s)
  • TORY:Originalrelease year
  • TOWN:Fileowner/licensee
  • TPE1:Leadperformer(s)/Soloist(s)
  • TPE2:Band/orchestra/accompaniment
  • TPE3:Conductor/performerrefinement
  • TPE4:Interpreted,remixed, or otherwise modified by
  • TPOS:Partof a set
  • TPUB:Publisher
  • TRCK:Tracknumber/Position in set
  • TRDA:Recordingdates
  • TRSN:Internetradio station name
  • TRSO:Internetradio station owner
  • TSIZ:Size
  • TSRC:ISRC(internationalstandard recording code)
  • TSSE:Software/Hardwareand settings used for encoding
  • TYER:Year
  • TXXX:Userdefinedtext information
  • UFID:Unique fileidentifier
  • USER:Terms of use
  • USLT:Unsychronizedlyric/text tranion
  • WCOM:Commercialinformation
  • WCOP:Copyright/Legalinformation
  • WOAF:Officialaudio file webpage
  • WOAR:Officialartist/performer webpage
  • WOAS:Officialaudio source webpage
  • WORS:Officialinternet radio station homepage
  • WPAY:Payment
  • WPUB:Publishersofficial webpage
  • WXXX:UserdefinedURL link

音乐类型

  • 0=“Blues”;
  • 1=“ClassicRock”;
  • 2=“Country”;
  • 3=“Dance”;
  • 4=“Disco”;
  • 5=“Funk”;
  • 6=“Grunge”;
  • 7=“Hip-Hop”;
  • 8=“Jazz”;
  • 9=“Metal”;
  • 10=“NewAge”;
  • 11=“Oldies”;
  • 12=“Other”;
  • 13=“Pop”;
  • 14=“R&B”;
  • 15=“Rap”;
  • 16=“Reggae”;
  • 17=“Rock”;
  • 18=“Techno”;
  • 19=“Industrial”;
  • 20=“Alternative”;
  • 21=“Ska”;
  • 22=“Deathl”;
  • 23=“Pranks”;
  • 24=“Soundtrack”;
  • 25=“Euro-Techno”;
  • 26=“Ambient”;
  • 27=“Trip-Hop”;
  • 28=“Vocal”;
  • 29=“Jazz+Funk”;
  • 30=“Fusion”;
  • 31=“Trance”;
  • 32=“Classical”;
  • 33=“Instrumental”;
  • 34=“Acid”;
  • 35=“House”;
  • 36=“Game”;
  • 37=“SoundClip”;
  • 38=“Gospel”;
  • 39=“Noise”;
  • 40=“AlternRock”;
  • 41=“Bass”;
  • 42=“Soul”;
  • 43=“Punk”;
  • 44=“Space”;
  • 45=“Meditative”;
  • 46=“InstrumentalPop”;
  • 47=“InstrumentalRock”;
  • 48=“Ethnic”;
  • 49=“Gothic”;
  • 50=“Darkwave”;
  • 51=“Techno-Industrial”;
  • 52=“Electronic”;
  • 53=“Pop-Folk”;
  • 54=“Eurodance”;
  • 55=“Dream”;
  • 56=“SouthernRock”;
  • 57=“Comedy”;
  • 58=“Cult”;
  • 59=“Gangsta”;
  • 60=“Top40”;
  • 61=“ChristianRap”;
  • 62=“Pop/Funk”;
  • 63=“Jungle”;
  • 64=“NativeAmerican”;
  • 65=“Cabaret”;
  • 66=“NewWave”;
  • 67=“Psychadelic”;
  • 68=“Rave”;
  • 69=“Showtunes”;
  • 70=“Trailer”;
  • 71=“Lo-Fi”;
  • 72=“Tribal”;
  • 73=“AcidPunk”;
  • 74=“AcidJazz”;
  • 75=“Polka”;
  • 76=“Retro”;
  • 77=“Musical”;
  • 78=“Rock&Roll”;
  • 79=“HardRock”;
  • 80=“Folk”;
  • 81=“Folk-Rock”;
  • 82=“NationalFolk”;
  • 83=“Swing”;
  • 84=“FastFusion”;
  • 85=“Bebob”;
  • 86=“Latin”;
  • 87=“Revival”;
  • 88=“Celtic”;
  • 89=“Bluegrass”;
  • 90=“Avantgarde”;
  • 91=“GothicRock”;
  • 92=“ProgessiveRock”;
  • 93=“PsychedelicRock”;
  • 94=“SymphonicRock”;
  • 95=“SlowRock”;
  • 96=“BigBand”;
  • 97=“Chorus”;
  • 98=“EasyListening”;
  • 99=“Acoustic”;
  • 100=“Humour”;
  • 101=“Speech”;
  • 102=“Chanson”;
  • 103=“Opera”;
  • 104=“ChamberMusic”;
  • 105=“Sonata”;
  • 106=“Symphony”;
  • 107=“BootyBass”;
  • 108=“Primus”;
  • 109=“PornGroove”;
  • 110=“Satire”;
  • 111=“SlowJam”;
  • 112=“Club”;
  • 113=“Tango”;
  • 114=“Samba”;
  • 115=“Folklore”;
  • 116=“Ballad”;
  • 117=“PowerBallad”;
  • 118=“RhythmicSoul”;
  • 119=“Freestyle”;
  • 120=“Duet”;
  • 121=“PunkRock”;
  • 122=“DrumSolo”;
  • 123=“Acapella”;
  • 124=“Euro-House”;
  • 125=“DanceHall”;
  • 126=“Goa”;
  • 127=“Drum&Bass”;
  • 128=“Club-House”;
  • 129=“Hardcore”;
  • 130=“Terror”;
  • 131=“Indie”;
  • 132=“BritPop”;
  • 133=“Negerpunk”;
  • 134=“PolskPunk”;
  • 135=“Beat”;
  • 136=“ChristianGangstaRap”;
  • 137=“Heavyl”;
  • 138=“Blackl”;
  • 139=“Crossover”;
  • 140=“ContemporaryChristian”;
  • 141=“ChristianRock”;
  • 142=“Merengue”;
  • 143=“Salsa”;
  • 144=“Trashl”;
  • 145=“Anime”;
  • 146=“JPop”;
  • 147=“Synthpop”;

参考

  • MPEG简介+如何计算CBR/VBR MP3的播放时间-1
  • MPEG简介 + 如何计算CBR/VBR MP3的播放时间 – 2
  • MPEG简介 + 如何计算CBR/VBR MP3的播放时间 – 3
  • MPEG简介 + 如何计算CBR/VBR MP3的播放时间 – 4
  • MPEG简介 + 如何计算CBR和VBR的MP3的播放时间 v1.5
  • Mp3格式总结
  • MP3文件结构解析(超详细)
  • MP3编码分析
  • (官网)ISO/IEC 11172-1:1993(en)Information technology — Coding of moving pictures and associated audio for digital storage media at up to about 1,5 Mbit/s — Part 1: Systems
  • (官网)ISO/IEC 11172-3:1993(en) Information technology — Coding of moving pictures and associated audio for digital storage media at up to about 1,5 Mbit/s — Part 3: Audio
  • (ietf)The audio/mpeg Media Type
  • (wikipedia)MP3
  • (fileformat)MP3
  • (官网)ID3 tag version 2.4.0 – Main Structure
  • (官网)ID3 tag version 2.3.0
  • (官网)ID3 tag version 2.2.0
  • github ParseMp3tag

下篇 Mp3文件结构全解析(二)接着分析MP3文件的音频内容解析
示例代码地址::qingkouwei/mp3parser.git
*如果对你有帮助的话点个赞吧!!!

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/120480.html

(0)
上一篇 2025-10-29 15:00
下一篇 2025-10-29 15:15

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信