mp3信息读取

作者在 2010-12-23 22:05:14 发布以下内容
有点想编写一个播放MP3的歌曲,当然网上很多播放器,但是那些开源的代码太多看起来就头大了,我只想要简单的播放功能而且是dos界面下的,不过那貌似很麻烦,要自己编写解码的程序,还没有头绪(当然那些调用上层库函数的很多,我不想用)现在从网上枪来了一点东西,希望做个铺垫......

MP3标签1(放在文件尾部128字节)

offset     type     len       name

--------------------------------------------

0               char     3           "TAG "

3               char     30         title

33             char     30         artist

63             char     30         album

93             char     4           year

97             char     30         comments

127           byte     1           genre

--------------------------------------------

样我们只需把指针移动到“文件大小-128字节”位置处并定义一个结构体就可以操作了.....

struct   mp3File

{

      char   TAG[3];

      char   title[30];

      char   artlist[30];

      char   album[30];

      char   year[4];

      char   comments[30];

      unsigned   char   genre[1];

};

/*若是多首歌曲的话,可以定义如上的结构体*/

/*标准MP3的音乐,在文件的最后128个字节就来存放MP3的信息的,只要打开读出来就行了,

用C语言来操作非简单。
*/

#include <stdio.h>

#include <string.h>

int main(int argc, char* argv[])

{

  FILE *fp;

  char pbuf[128];

  char buf[64];

  fp=fopen("D:\\musics\\边做边爱.mp3","r");

  if(fp==NULL)

  {

   printf("Failed to open mp3.\n");

   exit(-1);

  }

  memset(pbuf,0,sizeof(pbuf));

  fseek(fp,-128,SEEK_END);

  fread(pbuf,1,128,fp);

  if(!((pbuf[0] == 'T'|| pbuf[0] == 't')&&(pbuf[1] == 'A'|| pbuf[1] == 'a')&&(pbuf[2] == 'G'|| pbuf[0] == 'g')))

  {

  printf("Not a standard MP3 format.\n");

  return 1;

  }

  memset(buf,0,64);

  memcpy(buf,pbuf,3);

  buf[3]='\0';

  printf("标识:%s\n",buf);

  memset(buf,0,64);

  memcpy(buf,pbuf+3,30);

  buf[30]='\0';

  printf("歌名:%s\n",buf);

  memset(buf,0,64);

  memcpy(buf,pbuf+33,30);

  buf[63]='\0';

  printf("作者:%s\n",buf);

  memset(buf,0,64);

  memcpy(buf,pbuf+63,30);

  buf[93]='\0';

  printf("唱片名:%s\n",buf);

  memset(buf,0,64);

  memcpy(buf,pbuf+93,4);

  buf[97]='\0';

  printf("年份:%s\n",buf);

  memset(buf,0,64);

  memcpy(buf,pbuf+97,28);

  buf[125]='\0';

  printf("本歌注释:%s\n",buf);

  fclose(fp);

  

  return 0;

}
默认分类 | 阅读 1111 次
文章评论,共1条
变幻小子
2011-03-20 19:49
1
游客请输入验证码
浏览48271次
文章分类