解析本地音乐文件

一首音乐的后128字节保留了信息,前3个字节用于识别是否是音乐,接着31个字节存储歌曲名,再接31个字节是歌手名,再接31个字节是唱片名,然后5个字节是年份,29个字节是注释,最后3个字节作为保留位。

所以用C语言代码如下

#include<iostream>
using namespace std;
typedef struct _MP3INFO //MP3信息的结构
{
	char Identify[3]; //TAG三个字母
					  //这里可以用来鉴别是不是文件信息内容
	char Title[31];   //歌曲名,30个字节
	char Artist[31];  //歌手名,30个字节
	char Album[31];   //所属唱片,30个字节
	char Year[5];	  //年,4个字节
	char Comment[29]; //注释,28个字节
	unsigned char reserved;  //保留位,	1个字节
	unsigned char reserved2; //保留位,1个字节
	unsigned char reserved3; //保留位,1个字节
} MP3INFO;
bool GetInfo(char *filePath)
{
	// QTextCodec::setCodecForLocale(QTextCodec::codecForName("gbk"));
	FILE * fp;
	unsigned char mp3tag[128] = { 0 };
	MP3INFO mp3info;
	fp = fopen(filePath, "rb");
	if (NULL == fp)
	{
		cout << "打开文件失败" << endl;
		return false;
	}
	fseek(fp, -128, SEEK_END);
	fread(&mp3tag, 1, 128, fp);
	if (!((mp3tag[0] == 'T' || mp3tag[0] == 't')
		&& (mp3tag[1] == 'A' || mp3tag[1] == 'a')
		&& (mp3tag[2] == 'G' || mp3tag[0] == 'g')))
	{
		fclose(fp);
		cout<< "解析当前文件失败"<<endl;
		return false;
	}
	//获取Mp3信息
	memcpy((void *)mp3info.Identify, mp3tag, 3); //获得tag
	memcpy((void *)mp3info.Title, mp3tag + 3, 30); //获得歌名
	memcpy((void *)mp3info.Artist, mp3tag + 33, 30); //获得作者
	memcpy((void *)mp3info.Album, mp3tag + 63, 30); //获得唱片名
	memcpy((void *)mp3info.Year, mp3tag + 93, 4); //获得年
	memcpy((void *)mp3info.Comment, mp3tag + 97, 28); //获得注释
	memcpy((void *)&mp3info.reserved, mp3tag + 125, 1); //获得保留
	memcpy((void *)&mp3info.reserved2, mp3tag + 126, 1);
	memcpy((void *)&mp3info.reserved3, mp3tag + 127, 1);
	fclose(fp);
	
	cout << "Title:" << mp3info.Title << endl;
	cout << "Artist:" << mp3info.Artist << endl;
	cout << "Album:" << mp3info.Album << endl;
	cout << "Year:" << mp3info.Year << endl;
	cout << "Comment:" << mp3info.Comment << endl;
	return true;
}
int main()
{
	GetInfo("D:\\2.mp3");
	system("pause");
}


猜你喜欢

转载自blog.csdn.net/hlw0522/article/details/79479289
今日推荐