C读取MP3头部和尾部信息的代码
Mp3的文件头长10字节.主要保存ID3、ID3版本和副版本信息,以及Flag和帧长的信息。
Mp3尾部128字节大小的空间中包含了Header(尾部头,4字节),Title(歌曲名称,30字节),Artilst(艺术家30字节),Album(专辑,30字节),Year(发行年份,4字节),Comment(评论,30字节),Conre(?,1字节)
需要使用的函数 fseek() fread()
代码如下:
?
#include <stdio.h>FILE *fp;// Tag_header__Startstruct Tag_header{char Header[3]; // It must be 'ID3' char ver; // The version of ID3 char ReVer; // the subversion of ID3 char Flag; char Size[4]; };// Tag_header__End// Tag_Trailer__Startstruct Tag_Trailer{char Header[3];char Title[30];char Artist[30];char Album[30];char Year[4];char Comment[30];char Cenre[1];};// Tag_GetTrailer__Endstruct Tag_header Mp3Header;struct Tag_Trailer Mp3Trailer;main(){// 01.mp3是我测试时用的MP3的名称fp = fopen("01.mp3","r+");if(fp == NULL){ printf("No such file");}else{ Tag_GetHeader(); Tag_GetTrailer();}printf("\n%s\n",Mp3Trailer.Title);printf("%s\n",Mp3Trailer.Artist);printf("%s\n",Mp3Trailer.Album);printf("%s\n",Mp3Trailer.Year);printf("%s\n",Mp3Trailer.Comment);fclose(fp);}void Tag_GetTrailer(){// Get Headerfseek(fp,-128,SEEK_END);fread(Mp3Trailer.Header,3,1,fp);// Get Titlefseek(fp,-125,SEEK_END);fread(Mp3Trailer.Title,30,1,fp);// Get Artistfseek(fp,-95,SEEK_END);fread(Mp3Trailer.Artist,30,1,fp);// Get Albumfseek(fp,-65,SEEK_END);fread(Mp3Trailer.Album,30,1,fp);// Get Yearfseek(fp,-35,SEEK_END);fread(Mp3Trailer.Year,4,1,fp);// Get Commentfseek(fp,-31,SEEK_END);fread(Mp3Trailer.Comment,30,1,fp);// Get Cenrefseek(fp,-1,SEEK_END);fread(Mp3Trailer.Cenre,1,1,fp);}void Tag_GetHeader(){// Get ID3 Tagfseek(fp,0,SEEK_SET);fread(Mp3Header.Header,sizeof(Mp3Header.Header),1,fp); // Get ID3 Versionfseek(fp,3,SEEK_CUR);fread(Mp3Header.ver,sizeof(Mp3Header.ver),1,fp);// Get ID3 Revisionfseek(fp,4,SEEK_CUR);fread(Mp3Header.ReVer,sizeof(Mp3Header.ReVer),1,fp);// Get ID3 Flagfseek(fp,5,SEEK_CUR);fread(Mp3Header.Flag,sizeof(Mp3Header.Flag),1,fp);// Get ID3 Sizefseek(fp,6,SEEK_CUR);fread(Mp3Header.Size,sizeof(Mp3Header.Size),1,fp);}来源:http://www.cnblogs.com/aliasmic/archive/2008/09/20/1294915.html