/*file name "music.c"
用于读取MP3格式歌曲信息.
仅支持ID3V1格式的MP3文件.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#define MAX 128
/*歌曲信息结构
结构中Header[3] 更改为 header[4],但只赋值3个.最后一个为0
*/
struct tagID3V1{
char Header[4]; /*TAG*/
char Title[30]; /*标题*/
char Artist[30]; /*作者*/
char Album[30]; /*专辑*/
char Year[4]; /*年代*/
char Comment[28]; /*备注*/
char reserve; /*保留*/
char track; /*音轨*/
char Genre; /*类型*/
};
int main(int arg, char* argv[])
{
printf("The Music is:%s/n", argv[1]);
struct tagID3V1 ID3V1;
int fd;
off_t off;
// char buf[MAX];
if((fd = open(argv[1], O_RDWR)) == -1)
{
printf("%s Can/'t open!/n", argv[1]);
exit(1);
}
off = lseek(fd, -128, SEEK_END);
if(read(fd, ID3V1.Header, 3) == -1)
{
printf("%s Can/'t read the music header/n", argv[1]);
exit(1);
}
if(read(fd, ID3V1.Title, 30) == -1)
{
printf("%s can/'t read the music title/n", argv[1]);
exit(1);
}
if(read(fd, ID3V1.Artist, 30) == -1)
{
printf("%s can/'t read the music Artist/n", argv[1]);
exit(1);
}
if(read(fd, ID3V1.Album, 30) == -1)
{
printf("%s can/'t read the music Album/n", argv[1]);
exit(1);
}
if(read(fd, ID3V1.Year, 4) == -1)
{
printf("%s can/'t read the music Year/n", argv[1]);
exit(1);
}
if(read(fd, ID3V1.Comment, 28) == -1)
{
printf("%s can/'t read the music Comment/n", argv[1]);
exit(1);
}
// printf("The music Header is:%s/n", ID3V1.Header);
printf("歌曲名称:%s/n", ID3V1.Title);
printf("作者:%s/n", ID3V1.Artist);
printf("专辑:%s/n", ID3V1.Album);
printf("年代:%s/n", ID3V1.Year);
printf("备注:%s/n", ID3V1.Comment);
close(fd);
return 0;
}
music.c ID3V1格式读取信息
最新推荐文章于 2023-12-04 17:06:50 发布