读mp3文件ID3V2信息,文字编码转换…
来源:互联网 发布:java socket通信原理 编辑:程序博客网 时间:2024/06/05 11:51
提取mp3文件ID3V2信息,通过curl命令发送请求,发布微博。ID3V2信息为UTF-16编码,需要在发送curl命令之前先转码成UTF-8
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
typedef struct id3_v2_head
{
charHeader[3]; //必须为"ID3"否则认为标签不存在
unsignedchar Ver; //版本号ID3V2.3就记录3
unsignedcharRevision; //副版本号此版本记录为0
unsignedchar Flag; //存放标志的字节,这个版本只定义了三位,稍后详细解说
unsignedchar Size[4]; //标签大小,包括标签头的10个字节和所有的标签帧的大小
}id3_v2_head_t;
typedef struct id3_v2_frame
{
charFrameID[4]; //用四个字符标识一个帧,说明其内容,稍后有常用的标识对照表
unsignedchar Size[4]; //帧内容的大小,不包括帧头,不得小于1
unsignedchar Flags[2]; //存放标志,只定义了6位,稍后详细解说
}id3_v2_frame_t;
int get_id3_v2_info(char *string);
int publish_weibo(const char* hdr, const int info_idx[3][2]);
int convert_UTF16toUTF8(const char *input, int *insize, char*output, int *outsize);
int main(int argc,char **argv)
{
char*filename;
if(argc!=2){
return -1;
}
filename =argv[1];
get_id3_v2_info(filename);
return0;
}
int get_id3_v2_info(char *string)
{
intinfo_idx[3][2] = {0};
char*filename=NULL;
int ret =0;
id3_v2_head_t * id3_v2_head=NULL;
id3_v2_frame_t* id3_v2_frame=NULL;
intfd;
char *hdr =NULL;
long intid3_v2_head_length=0;
long intid3_v2_frame_length=0;
long inttotal_read=0;
struct statsb;
char *info =NULL;
filename=string;
fd =open(filename, O_RDONLY);
if(fd==-1)
{
return -1;
}
fstat(fd,&sb);
hdr=(char*)mmap(NULL,sb.st_size,PROT_READ,MAP_PRIVATE,fd,0);
if(hdr==MAP_FAILED)
{
return 0;
close(fd);
}
id3_v2_head=(id3_v2_head_t *)malloc(sizeof(id3_v2_head_t));
if(id3_v2_head==NULL)
{
munmap(hdr,sb.st_size);
close(fd);
return -1;
}
memcpy(id3_v2_head,hdr,sizeof(id3_v2_head_t));
if(strncmp(id3_v2_head->Header, "ID3", 3))
{
printf("have no id3 v2 info\n");
munmap(hdr,sb.st_size);
close(fd);
return -1;
}
printf("have id3 v2.%d.%dinfo\n",id3_v2_head->Ver,id3_v2_head->Revision);
id3_v2_head_length =(id3_v2_head->Size[0]&0x7F)*0x200000+(id3_v2_head->Size[1]&0x7F)*0x400+(id3_v2_head->Size[2]&0x7F)*0x80+(id3_v2_head->Size[3]&0x7F);
printf("headlength:%ld\n",id3_v2_head_length);
total_read+= sizeof(id3_v2_head_t);
id3_v2_frame = (id3_v2_frame_t*)malloc(sizeof(id3_v2_frame_t));
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
typedef struct id3_v2_head
{
}id3_v2_head_t;
typedef struct id3_v2_frame
{
}id3_v2_frame_t;
int get_id3_v2_info(char *string);
int publish_weibo(const char* hdr, const int info_idx[3][2]);
int convert_UTF16toUTF8(const char *input, int *insize, char*output, int *outsize);
int main(int argc,char **argv)
{
}
int get_id3_v2_info(char *string)
{