可以做代销的网站都有哪些,品牌视觉设计包含什么,郑州发布评论,电脑软件培训班计算机培训班Mp3文件结构全解析(一)
MP3 文件是由帧(frame)构成的#xff0c;帧是MP3 文件最小的组成单位。MP3的全称应为MPEG1 Layer-3 音频 文件#xff0c;MPEG(Moving Picture Experts Group) 在汉语中译为活动图像专家组#xff0c;特指活动影音压缩标准#xff0c;MPEG 音频文件…Mp3文件结构全解析(一)
MP3 文件是由帧(frame)构成的帧是MP3 文件最小的组成单位。MP3的全称应为MPEG1 Layer-3 音频 文件MPEG(Moving Picture Experts Group) 在汉语中译为活动图像专家组特指活动影音压缩标准MPEG 音频文件是MPEG1 标准中的声音部分也叫MPEG 音频层它根据压缩质量和编码复杂程度划分为三层即Layer-1、Layer2、Layer3 且分别对应MP1、MP2、MP3 这三种声音文件并根据不同的用途使用不同层次的编码。MPEG 音频编码的层次越高编码器越复杂压缩率也越高MP1 和MP2 的压缩率分别为41 和61-81而MP3 的压缩率则高达101-121也就是说一分钟CD 音质的音乐未经压缩需要10MB的存储空间而经过MP3 压缩编码后只有1MB 左右。不过MP3 对音频信号采用的是有损压缩方式为了降低声音失真度MP3采取了“感官编码技术”即编码时先对音频文件进行频谱分析然后用过滤器滤掉噪音电平接着通过量化的方式将剩下的每一位打散排列最后形成具有较高压缩比的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表示是否使用Unsynchronisationb表示是否有扩展头部一般没有所以一般也不设置c表示是否为测试标签99.99%的标签都不是测试标签不设置
标签大小共四个字节每个字节只使用低7位最高位不使用恒为0计算时将最高位去掉得到28bit的数据计算公式如下 Size(Size[0]0x7F)*0x200000(Size[1]0x7F)*0x4000(Size[2]0x7F)*0x80(Size[3]0x7F) 注意:很多文章这里面写的是Size[1]0x7F)*0x4000,左移14位是乘以0x4000,很多文章抄来抄去,源头出问题了,所有的都会出问题
以《胡广生.mp3》为例使用UltraEdit工具打开如下读者可自己对照上述结构本章结束会给出详细的结构分析
标签帧
每个标签帧都有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)*0x200000(Size[1]0x7F)*0x4000(Size[2]0x7F)*0x80(Size[3]0x7F)很多文章是这样的:
Size Size[0]*0x100000000 Size[1]*0x10000 Size[2]*0x100 Size[3];其实是有问题的. 标志帧使用每个字节的高三位其他位均为0(abc00000B xyz00000B)
a – 标签保护标志设置时认为此帧作废b – 文件保护标志设置时认为此帧作废c – 只读标志设置时认为此帧不能修改x – 压缩标志设置时一个字节存放两个BCD 码表示数字y-- 加密标志z-- 组标志设置时说明此帧和其他的某帧是一组
ID3V1解析
ID3 V1.0标准并不周全存放的信息少无法存放歌词无法录入专辑封面、图片等。V2.0是一个相当完备的标准但给编写软件带来困难虽然赞成此格式的人很多在软件中真正实现的却极少。绝大多数MP3仍使用ID3 V1.0标准。此标准是将MP3文件尾的最后128个字节用来存放ID3信息
实例分析
歌曲信息
胡广生.mp3
结构解析
标签头 标签头10字节00H-09H
1234567891049443304000000031027I(73)D(68)3(51)4(V2.4)0(revision)0(flag)0x000x030x100x27
(Size[0]0x7F)*0x200000(Size[1]0x7F)*0x400(Size[2]0x7F)*0x80(Size[3]0x7F) (0x000x7F)*0x200000 (0x030x7f)*0x4000 (0x100x7f)*0x80 (0x270x7f) 0x00 0xc000 0x800 0x27 0xc824 51239个字节
(0x000x7F)21 (0x030x7f)14 (0x100x7f)*7 (0x270x7f) 0x0 0xC000 0x800 0x27
TIT2标签帧 地址字节值说明AH-DH454 49 54 32TIT2标签帧说明EH-11H400 00 00 09帧内容大小: 9个字节12H-13H200 00标志,均为014H- 1CH901 FF FE E1 80 7F 5E 1F 75帧内容
TALB标签帧 地址字节值说明1dH-20H454 41 4C 42TALB标签帧说明21H-24H400 00 00 09帧内容大小: 9个字节25H-26H200 00标志,均为027H- 2FH901 FF FE E1 80 7F 5E 1F 75帧内容
TPE1标签帧 地址字节值说明30H-33H454 50 45 31TPE1标签帧说明34H-37H400 00 00 09帧内容大小: 9个字节38H-39H200 00标志,均为027H- 2FH901 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 dataPicture 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-46H441 50 49 43APIC标签帧说明47H-4AH400 01 43 65帧内容大小: 83789个字节38H-39H200 00标志,均为027H- 2FH900 69 …帧内容帧内容大小
Size Size[0]*0x00000000 Size[1]*0x10000 Size[2]*0x100 Size[3]
0x010x100000x430x1000x65
0x100000x43000x65
0x1436582789
82789 / 16 5174 0x1436 82789 % 16 6
0x1436 0x2 0x1438 3 6 9
实现一个MP3解析器之标签解析
根据上面分析,先要读取标签头,先读取十个字节的标签头,定义一个结构体:
struct tagheader {char ID[3]; // The first 4 bytes should be ID3char version[2]; // $03 00char flags; // $abc00000 : a:unsynchronisation if set; b:extended header exist if set; c:experimental indicator if setchar 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) * 0x200000 (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]*0x100000000 header.size[1]*0x10000 header.size[2]*0x100 header.size[3];startframesz;...计算到标签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 guesssize_t outsize framesz * 2;char* result malloc(outsize);bzero(result, outsize);char *temp input1;if (strncmp(header.frameid, PRIV, 4) 0) { // PRIVs handle is a bit of special, still dont 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);}到此为止,已经可以把所有的标签内容读取成功并打印了.详细代码参考: https://github.com/qingkouwei/mp3parser
Mp3格式定义参考官网: http://id3.org/id3v2.4.0-structure
附录
帧标识
AENCAudioencryptionAPICAttached pictureCOMMCommentsCOMRCommercialENCREncryptionmethod registrationEQUAEqualizationETCOEvent timingcodesGEOBGeneralencapsulated objectGRIDGroupidentification registrationIPLSInvolvedpeople listLINKLinkedinformationMCDIMusic CDidentifierMLLTMPEGlocationlookup tableOWNEOwnershipPRIVPrivatePCNTPlaycounterPOPMPopularimeterPOSSPositionsynchronisationRBUFRecommendedbuffer sizeRVADRelativevolume adjustmentRVRBReverbSYLTSynchronizedlyric/textSYTCSynchronizedtempo codesTALBAlbum/Movie/ShowtitleTBPMBPM(beats perminute)TCOMComposerTCONContent typeTCOPCopyrightmessageTDATDateTDLYPlaylistdelayTENCEncoded byTEXTLyricist/TextwriterTFLTFiletypeTIMETimeTIT1Content groupdeionTIT2Title/songname/contentdeionTIT3Subtitle/DeionrefinementTKEYInitial keyTLANLanguage(s)TLENLengthTMEDMedia typeTOALOriginalalbum/movie/show titleTOFNOriginalfilenameTOLYOriginallyricist(s)/text writer(s)TOPEOriginalartist(s)/performer(s)TORYOriginalrelease yearTOWNFileowner/licenseeTPE1Leadperformer(s)/Soloist(s)TPE2Band/orchestra/accompanimentTPE3Conductor/performerrefinementTPE4Interpreted,remixed, or otherwise modified byTPOSPartof a setTPUBPublisherTRCKTracknumber/Position in setTRDARecordingdatesTRSNInternetradio station nameTRSOInternetradio station ownerTSIZSizeTSRCISRC(internationalstandard recording code)TSSESoftware/Hardwareand settings used for encodingTYERYearTXXXUserdefinedtext informationUFIDUnique fileidentifierUSERTerms of useUSLTUnsychronizedlyric/text tranionWCOMCommercialinformationWCOPCopyright/LegalinformationWOAFOfficialaudio file webpageWOAROfficialartist/performer webpageWOASOfficialaudio source webpageWORSOfficialinternet radio station homepageWPAYPaymentWPUBPublishersofficial webpageWXXXUserdefinedURL 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“RB”;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“JazzFunk”;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“RockRoll”;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“DrumBass”;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的播放时间-1MPEG简介 如何计算CBR/VBR MP3的播放时间 - 2MPEG简介 如何计算CBR/VBR MP3的播放时间 - 3MPEG简介 如何计算CBR/VBR MP3的播放时间 - 4MPEG简介 如何计算CBR和VBR的MP3的播放时间 v1.5Mp3格式总结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.0github ParseMp3tag
下篇 Mp3文件结构全解析(二)接着分析MP3文件的音频内容解析 示例代码地址:gitgithub.com:qingkouwei/mp3parser.git *如果对你有帮助的话点个赞吧!!!