eaa8ac3c0c32bc74eff7a6f864d419b4e52be73f
[FAPG] / fapg.c
1 /*\r
2  * FAPG 0.0 released under GPL\r
3  * http://royale.zerezo.com/fapg/\r
4  */\r
5 \r
6 #include <stdio.h>\r
7 #include <stdlib.h>\r
8 #include <getopt.h>\r
9 #include <dirent.h>\r
10 #include <sys/types.h>\r
11 #include <string.h>\r
12 #include <limits.h>\r
13 \r
14 #define MAX 10240\r
15 \r
16 int debug=0;\r
17 int format=0; /* 0 = m3u ; 1 = pls ; 2 = html */\r
18 unsigned char *prefix="";\r
19 int recursive=0;\r
20 int separator='/';\r
21 int skip=0;\r
22 int windows=0;\r
23 unsigned char buffer[MAX];\r
24 \r
25 int counter=0;\r
26 \r
27 unsigned char artist[1024];\r
28 unsigned char title[1024];\r
29 int time;\r
30 \r
31 unsigned char unix2dos[256]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,70,35,36,37,38,39,40,41,82,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,84,59,36,61,65,71,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,36,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,36,125,126,127,199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,248,163,216,215,131,225,237,243,250,241,209,170,186,191,174,172,189,188,161,171,187,166,166,166,166,166,193,194,192,169,166,166,43,43,162,165,43,43,45,45,43,45,43,227,195,43,43,45,45,166,45,43,164,240,208,202,203,200,105,205,206,207,43,43,166,220,166,204,175,211,223,212,210,245,213,181,254,222,218,219,217,253,221,175,180,173,177,61,190,182,167,247,184,176,168,183,185,179,178,166,160};\r
32 \r
33 void usage()\r
34 {\r
35   fprintf(stderr,"Usage >> fapg [-backslash] [-debug] [-format=m3u|pls|html] [-output=/path/to/file.m3u] [-prefix=/the/prefix] [-recursive] [-windows] /path/to/mp3/dir1 [/path/to/mp3/dir2 ...]\n");\r
36   exit(1);\r
37 }\r
38 \r
39 void parse_options(int argc,char **argv)\r
40 {\r
41   static struct option long_options[]=\r
42   {\r
43     {"backslash",no_argument,&separator,'\\'},\r
44     {"debug",no_argument,&debug,1},\r
45     {"format",required_argument,0,'f'},\r
46     {"output",required_argument,0,'o'},\r
47     {"prefix",required_argument,0,'p'},\r
48     {"recursive",no_argument,&recursive,1},\r
49     {"windows",no_argument,&windows,1}\r
50   };\r
51   int c;\r
52   int option_index=0;\r
53   while ((c=getopt_long_only(argc,argv,"",long_options,&option_index))!=-1)\r
54   {\r
55     switch(c)\r
56     {\r
57       case 0:\r
58         break;\r
59       case 'f':\r
60         if (strcmp(optarg,"m3u")==0)  format=0; else\r
61         if (strcmp(optarg,"pls")==0)  format=1; else\r
62         if (strcmp(optarg,"html")==0) format=2; else\r
63         usage();\r
64         break;\r
65       case 'o':\r
66         close(1);\r
67         if (fopen(optarg,"w")==NULL) { fprintf(stderr,"Error >> unable to open output file : %s\n",optarg); exit(2); }\r
68         break;\r
69       case 'p':\r
70         prefix=malloc(strlen(optarg)+1);\r
71         strcpy(prefix,optarg);\r
72         break;\r
73       default:\r
74         usage();\r
75     }\r
76   }\r
77 }\r
78 \r
79 void parse_mp3(unsigned char *file)\r
80 {\r
81   int bitrates[2][3][15]=\r
82     {{{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448},\r
83       {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},\r
84       {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}},\r
85      {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},\r
86       {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},\r
87       {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}}};\r
88   FILE *fic;\r
89   unsigned char *c;\r
90   int lus;\r
91 \r
92   if (debug) fprintf(stderr,"Debug >> parsing mp3 : %s\n",file);\r
93 \r
94   /* read header */\r
95   if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }\r
96   lus=fread(buffer,1,MAX,fic);\r
97   c=buffer;  \r
98 \r
99   /* try ID3v2 */\r
100   if (buffer[0]=='I' && buffer[1]=='D' && buffer[2]=='3')\r
101   {\r
102     int size;\r
103     int version;\r
104     version=*(buffer+3);\r
105     if (version<2 || version>4)\r
106       fprintf(stderr,"Warning >> ID3 v2.%d not implemented ! trying anyway : %s\n",version,file);\r
107     if (*(buffer+5)!=0)\r
108       fprintf(stderr,"Warning >> specials headers not implemented (%d) ! trying anyway : %s\n",*(buffer+5),file);\r
109     c=buffer+6;\r
110     size=(*c<<24)+(*(c+1)<<16)+(*(c+2)<<8)+(*(c+3));\r
111     if (size>lus) size=lus;\r
112     c+=4;\r
113     if (version==2) while (c<buffer+size-5)\r
114     {\r
115       int size=(*(c+3)<<16)+(*(c+4)<<8)+(*(c+5));\r
116       if (*c==0) break;\r
117       if (strncmp(c,"TT2",3)==0)\r
118       {\r
119         strncpy(title,c+7,size-1);\r
120         title[size-1]='\0';\r
121       }\r
122       if (strncmp(c,"TP1",3)==0)\r
123       {\r
124         strncpy(artist,c+7,size-1);\r
125         artist[size-1]='\0';\r
126       }\r
127       c+=size+6;\r
128     }\r
129     if (version==3 || version==4) while (c<buffer+size-7)\r
130     {\r
131       int size=(*(c+4)<<24)+(*(c+5)<<16)+(*(c+6)<<8)+(*(c+7));\r
132       if (*c==0) break;\r
133       if (strncmp(c,"TIT2",4)==0)\r
134       {\r
135         strncpy(title,c+11,size-1);\r
136         title[size-1]='\0';\r
137       }\r
138       if (strncmp(c,"TPE1",4)==0)\r
139       {\r
140         strncpy(artist,c+11,size-1);\r
141         artist[size-1]='\0';\r
142       }\r
143       c+=size+10;\r
144     }\r
145   }\r
146   \r
147   while (c<buffer+lus-10)\r
148   {\r
149     if (*c==0xFF && (*(c+1)&0xF0)==0xF0)\r
150     {\r
151       int version;\r
152       int lay;\r
153       int bitrate_index;\r
154       int bitrate;\r
155       version=2-(*(c+1)>>3&1);\r
156       lay=4-(*(c+1)>>1&3);\r
157       bitrate_index=*(c+2)>>4&0xF;\r
158       if (version>=1 && version<=2 && lay-1>=0 && lay-1<=2 && bitrate_index>=0 && bitrate_index<=14)\r
159         bitrate=bitrates[version-1][lay-1][bitrate_index];\r
160       else\r
161         bitrate=0;\r
162       if (bitrate!=0)\r
163       {\r
164         fseek(fic,0,SEEK_END);\r
165         time=(ftell(fic)+buffer-c)/125/bitrate;\r
166       }\r
167       else\r
168         time=0;\r
169       break;\r
170     }\r
171     c++;\r
172   }\r
173 \r
174   /* try ID3v1 */\r
175   if (strlen(artist)==0 && strlen(title)==0)\r
176   {\r
177     fseek(fic,-128,SEEK_END);\r
178     lus=fread(buffer,1,128,fic);\r
179     if (lus==128 && buffer[0]=='T' && buffer[1]=='A' && buffer[2]=='G')\r
180     {\r
181       strncpy(title,buffer+3,30);\r
182       title[30]='\0';\r
183       c=title+29;\r
184       while (c>title && *c==' ') *(c--)='\0';\r
185       strncpy(artist,buffer+33,30);\r
186       artist[30]='\0';\r
187       c=artist+29;\r
188       while (c>artist && *c==' ') *(c--)='\0';\r
189     }\r
190   }\r
191 \r
192   fclose(fic);\r
193 }  \r
194 \r
195 void parse_ogg(unsigned char *file)\r
196 {\r
197   FILE *fic;\r
198   unsigned char *c;\r
199   int lus;\r
200   int sample_rate;\r
201   int samples;\r
202 \r
203   if (debug) fprintf(stderr,"Debug >> parsing ogg : %s\n",file);\r
204 \r
205   /* read header */\r
206   if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }\r
207   lus=fread(buffer,1,MAX,fic);\r
208 \r
209   /* try Ogg */\r
210   if (buffer[0]!='O' && buffer[1]!='g' && buffer[2]!='g')\r
211   {\r
212     fprintf(stderr,"Warning >> not a Ogg header : %s\n",file);\r
213     return;\r
214   }\r
215   \r
216   c=buffer+0x28;\r
217   sample_rate=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);\r
218 \r
219   while (c<buffer+lus-10)\r
220   {\r
221     int size;\r
222     if (strncmp(c,"TITLE=",6)==0)\r
223     {\r
224       size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);\r
225       strncpy(title,c+6,size-6);\r
226       title[size-6]='\0';\r
227       c+=size;\r
228     }\r
229     if (strncmp(c,"ARTIST=",7)==0)\r
230     {\r
231       size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);\r
232       strncpy(artist,c+7,size-7);\r
233       artist[size-7]='\0';\r
234       c+=size;\r
235     }\r
236     c++;\r
237   }\r
238   \r
239   fseek(fic,-MAX,SEEK_END);\r
240   lus=fread(buffer,1,MAX,fic);\r
241   c=buffer+lus-1;\r
242   while (strncmp(c,"OggS",4)!=0 && c>buffer) c--;\r
243   if (c!=buffer)\r
244   {\r
245     c+=6;\r
246     samples=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);\r
247     time=samples/sample_rate;\r
248   }\r
249   \r
250   fclose(fic);\r
251 }\r
252 \r
253 void parse_directory(unsigned char *path)\r
254 {\r
255   DIR *dirp;\r
256   struct dirent *dp;\r
257   unsigned char newpath[PATH_MAX];\r
258   unsigned char *c;\r
259 \r
260   void print_faketitle()\r
261   {\r
262     c=newpath+strlen(newpath);\r
263     while (c>newpath && *c!='/') c--;\r
264     while (c<newpath+strlen(newpath)-5)\r
265     {\r
266       c++;\r
267       if (*c=='_') putchar(' '); else if (windows) putchar(unix2dos[*c]); else putchar(*c);\r
268     }\r
269   }\r
270   \r
271   void print_path()\r
272   {\r
273     printf(prefix);\r
274     for (c=newpath+skip;*c!='\0';c++) if (*c=='/') putchar(separator); else if (windows) putchar(unix2dos[*c]); else putchar(*c);\r
275   }\r
276 \r
277   if (debug) fprintf(stderr,"Debug >> parsing directory : %s\n",path);\r
278   if ((dirp=opendir(path))==NULL) { fprintf(stderr,"Warning >> can't open directory : %s\n",path); return; }\r
279   while ((dp=readdir(dirp))!=NULL)\r
280   {\r
281     sprintf(newpath,"%s/%s",path,dp->d_name);\r
282     if (recursive && dp->d_type & DT_DIR && strcmp(dp->d_name,".")!=0 && strcmp(dp->d_name,"..")!=0)\r
283     {\r
284       parse_directory(newpath);\r
285     }\r
286     if (dp->d_type & DT_REG)\r
287     {\r
288       unsigned char ext[5];\r
289       int i;\r
290       for (i=0;i<5;i++) ext[i]=tolower(dp->d_name[strlen(dp->d_name)-4+i]);\r
291       artist[0]='\0';\r
292       title[0]='\0';\r
293       time=-2;\r
294       if (strcmp(".mp3",ext)==0) { time=-1; parse_mp3(newpath); }\r
295       if (strcmp(".ogg",ext)==0) { time=-1; parse_ogg(newpath); }\r
296       if (strcmp(".wav",ext)==0) { time=-1; /* parse_wav(newpath); */ }\r
297       \r
298       if (time!=-2) /* is it an audio file ? */\r
299       {\r
300         counter++;\r
301         switch (format)\r
302         {\r
303           case 0:\r
304             if (time!=-1)\r
305             {\r
306               printf("#EXTINF:%d,",time);\r
307               if (strlen(artist)==0 && strlen(title)==0) print_faketitle();\r
308               else printf("%s - %s",artist,title);\r
309               putchar('\n');\r
310             }\r
311             print_path();\r
312             putchar('\n');\r
313             break;\r
314           case 1:\r
315             printf("File%d=",counter);\r
316             print_path();\r
317             putchar('\n');\r
318             printf("Title%d=",counter);\r
319             if (strlen(artist)==0 && strlen(title)==0) print_faketitle();\r
320             else printf("%s - %s",artist,title);\r
321             putchar('\n');\r
322             if (time!=-1) printf("Length%d=%d\n",counter,time);\r
323             break;\r
324           case 2:\r
325             printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>",counter,artist,title);\r
326             if (time==-1) printf("?"); else printf("%d",time);\r
327             printf("</td></tr>\n");\r
328             break;\r
329         }\r
330       }\r
331     }\r
332   }\r
333   closedir(dirp);\r
334 }\r
335 \r
336 int main(int argc,char **argv)\r
337 {\r
338   parse_options(argc,argv);\r
339   if (optind==argc) usage();\r
340   switch (format)\r
341   {\r
342     case 0:\r
343       printf("#EXTM3U\n");\r
344       break;\r
345     case 1:\r
346       printf("[playlist]\n");\r
347       break;\r
348     case 2:\r
349       printf("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n\n<html>\n\n<head>\n<title>Playlist generated by FAPG 0.0</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n<link rel=\"stylesheet\" type=\"text/css\" href=\"fapg.css\" />\n</head>\n\n<body>\n\n<h1>Playlist</h1>\n\n<table>\n<tr><th>Entry</th><th>Artist</th><th>Title</th><th>Length</th></tr>\n");\r
350       break;\r
351   }\r
352   for (;optind<argc;optind++)\r
353   {\r
354     if (strlen(prefix)!=0) skip=strlen(argv[optind]);\r
355     parse_directory(argv[optind]);\r
356   }\r
357   switch(format)\r
358   {\r
359     case 1:\r
360       printf("NumberOfEntries=%d\nVersion=2\n",counter);\r
361       break;\r
362     case 2:\r
363       printf("</table>\n\n<p>Playlist generated by <a href=\"http://royale.zerezo.com/fapg/\">FAPG 0.0</a></p>\n\n</body>\n\n</html>");\r
364       break;\r
365   }\r
366   exit(0);\r
367 }\r