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