4bfc290b7da65ffd7b50b82a649ce0011df3778b
[my-code/mp3db.git] / mp3read.c
1 /* read mp3 header & tag, author: hackbard */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <errno.h>
11
12 #define ID_TAG_SIZE 128
13 #define MAX_BUF_SIZE 32
14 #define MAX_TITLE 30
15 #define MAX_ARTIST 30
16 #define MAX_ALBUM 30
17 #define MAX_YEAR 4
18 #define MAX_COMMENT 30
19 #define MAX_GENRE 1
20
21 #define MAX_FILENAME 32
22
23
24 /*
25 info:
26 http://www.dv.co.yu/mpgscript/mpeghdr.htm
27 */
28
29 int main (int argc,char **argv)
30 {
31  int file_fd;
32  int file_size;
33  unsigned char buf[MAX_BUF_SIZE];
34  char filename[MAX_FILENAME];
35
36  strcpy(filename,argv[1]);
37  file_size=atoi(argv[2]);
38
39  if((file_fd=open(filename,O_RDONLY))<=0) {
40   puts("open failed");
41   return -23;
42  }
43
44  if((lseek(file_fd,file_size-ID_TAG_SIZE,SEEK_SET))<0) {
45   puts("cannot seek to id tag");
46   return -23;
47  }
48
49  /* verify TAG now */
50  if((read(file_fd,&buf,3))<3) {
51   puts("read failed");
52   return -23;
53  }
54  if(strncmp(buf,"TAG",3)) {
55   puts("TAG not found");
56   return -23;
57  }
58
59  read(file_fd,&buf,MAX_TITLE);
60  printf("title: %s<br>\n",buf);
61
62  read(file_fd,&buf,MAX_ARTIST);
63  printf("artist: %s<br>\n",buf);
64
65  read(file_fd,&buf,MAX_ALBUM);
66  printf("album: %s<br>\n",buf);
67
68  // read(file_fd,&buf,MAX_YEAR);
69  // printf("year: %s<br>\n",buf);
70
71  // read(file_fd,&buf,MAX_COMMENT);
72  // printf("comment: %s<br>\n",buf);
73
74  // read(file_fd,&buf,MAX_GENRE);
75  // printf("genre: %d<br>\n",(int)*buf);
76
77  close(file_fd);
78  
79  return 23;
80 }