version 0.22
[FAPG] / fapg.c
1 /*
2  * FAPG version 0.22
3  *
4  * FAPG means Fast Audio Playlist Generator.
5  * It is a tool to generate list of audio files (Wav, MP3, Ogg, etc)
6  * in various formats (M3U, PLS, HTML, etc).
7  * It is very usefull if you have a large amount of audio files
8  * and you want to quickly and frequently build a playlist.
9  *
10  * Copyright (C) 2003-2004  Antoine Jacquet <royale@zerezo.com>
11  * http://royale.zerezo.com/fapg/
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <getopt.h>
31 #include <dirent.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <unistd.h>
37 #include <ctype.h>
38
39 #define MP3_BASE 1024
40 #define OGG_BASE 1024*10
41 #define MAX 1024*200 /* 200ko for ID3 with JPEG images in it */
42
43 int debug=0;
44 int format=0; /* 0 = m3u ; 1 = pls ; 2 = html */
45 unsigned char *prefix="";
46 int recursive=0;
47 int separator='/';
48 int skip=0;
49 int windows=0;
50 unsigned char *eol="\n";
51 unsigned char buffer[MAX];
52
53 int counter=0;
54
55 unsigned char artist[1024];
56 unsigned char title[1024];
57 int duration;
58
59 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};
60
61 void usage()
62 {
63   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");
64   exit(1);
65 }
66
67 void parse_options(int argc,char **argv)
68 {
69   static char const short_options[]="bdf:o:p:rw";
70   static struct option long_options[]=
71   {
72     {"backslash",no_argument,NULL,'b'},
73     {"debug",no_argument,NULL,'d'},
74     {"format",required_argument,NULL,'f'},
75     {"output",required_argument,NULL,'o'},
76     {"prefix",required_argument,NULL,'p'},
77     {"recursive",no_argument,NULL,'r'},
78     {"windows",no_argument,NULL,'w'}
79   };
80   int c;
81   int option_index=0;
82   while ((c=getopt_long(argc,argv,short_options,long_options,&option_index))!=-1)
83   {
84     switch(c)
85     {
86       case 'b':
87         separator='\\';
88         break;
89       case 'd':
90         debug=1;
91         break;
92       case 'f':
93         if (strcmp(optarg,"m3u")==0)  format=0; else
94         if (strcmp(optarg,"pls")==0)  format=1; else
95         if (strcmp(optarg,"html")==0) format=2; else
96         usage();
97         break;
98       case 'o':
99         close(1);
100         if (fopen(optarg,"w")==NULL) { fprintf(stderr,"Error >> unable to open output file : %s\n",optarg); exit(2); }
101         break;
102       case 'p':
103         prefix=malloc(strlen(optarg)+1);
104         strcpy(prefix,optarg);
105         break;
106       case 'r':
107         recursive=1;
108         break;
109       case 'w':
110         windows=1;
111         eol="\r\n";
112         break;
113       default:
114         usage();
115     }
116   }
117 }
118
119 void parse_mp3(unsigned char *file)
120 {
121   int bitrates[2][3][15]=
122     {{{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448},
123       {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
124       {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}},
125      {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
126       {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
127       {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}}};
128   FILE *fic;
129   unsigned char *c;
130   int lus;
131
132   if (debug) fprintf(stderr,"Debug >> parsing mp3 : %s\n",file);
133
134   /* read header */
135   if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }
136   lus=fread(buffer,1,MP3_BASE,fic);
137   c=buffer;  
138
139   /* try ID3v2 */
140   if (buffer[0]=='I' && buffer[1]=='D' && buffer[2]=='3')
141   {
142     int size;
143     int version;
144     version=*(buffer+3);
145     if (version<2 || version>4)
146       fprintf(stderr,"Warning >> ID3 v2.%d not implemented ! trying anyway : %s\n",version,file);
147     if (*(buffer+5)!=0)
148       fprintf(stderr,"Warning >> specials headers not implemented (%d) ! trying anyway : %s\n",*(buffer+5),file);
149     c=buffer+6;
150     size=(*c<<21)+(*(c+1)<<14)+(*(c+2)<<7)+(*(c+3));
151     /* read more header */
152     if (size+lus>MAX)
153     {
154       lus+=fread(buffer+lus,1,MAX-lus,fic);
155       fprintf(stderr,"Warning >> ID3 header is huge (%d bytes) ! trying anyway : %s\n",size,file);
156     }
157     else
158       lus+=fread(buffer+lus,1,size,fic);
159     if (size>lus) size=lus;
160     c+=4;
161     if (version==2) while (c<buffer+size)
162     {
163       int size=(*(c+3)<<16)+(*(c+4)<<8)+(*(c+5));
164       if (*c==0) break;
165       if (strncmp(c,"TT2",3)==0)
166       {
167         strncpy(title,c+7,size-1);
168         title[size-1]='\0';
169       }
170       if (strncmp(c,"TP1",3)==0)
171       {
172         strncpy(artist,c+7,size-1);
173         artist[size-1]='\0';
174       }
175       c+=size+6;
176     }
177     if (version==3 || version==4) while (c<buffer+size)
178     {
179       int size=(*(c+4)<<24)+(*(c+5)<<16)+(*(c+6)<<8)+(*(c+7));
180       if (*c==0) break;
181       if (strncmp(c,"TIT2",4)==0)
182       {
183         strncpy(title,c+11,size-1);
184         title[size-1]='\0';
185       }
186       if (strncmp(c,"TPE1",4)==0)
187       {
188         strncpy(artist,c+11,size-1);
189         artist[size-1]='\0';
190       }
191       c+=size+10;
192     }
193   }
194   
195   while (c<buffer+lus-10)
196   {
197     if (*c==0xFF && (*(c+1)&0xF0)==0xF0)
198     {
199       int version;
200       int lay;
201       int bitrate_index;
202       int bitrate;
203       version=2-(*(c+1)>>3&1);
204       lay=4-(*(c+1)>>1&3);
205       bitrate_index=*(c+2)>>4&0xF;
206       if (version>=1 && version<=2 && lay-1>=0 && lay-1<=2 && bitrate_index>=0 && bitrate_index<=14)
207         bitrate=bitrates[version-1][lay-1][bitrate_index];
208       else
209         bitrate=0;
210       if (bitrate!=0)
211       {
212         fseek(fic,0,SEEK_END);
213         duration=(ftell(fic)+buffer-c)/125/bitrate;
214       }
215       else
216         duration=0;
217       break;
218     }
219     c++;
220   }
221
222   /* try ID3v1 */
223   if (strlen(artist)==0 && strlen(title)==0)
224   {
225     fseek(fic,-128,SEEK_END);
226     lus=fread(buffer,1,128,fic);
227     if (lus==128 && buffer[0]=='T' && buffer[1]=='A' && buffer[2]=='G')
228     {
229       strncpy(title,buffer+3,30);
230       title[30]='\0';
231       c=title+29;
232       while (c>title && *c==' ') *(c--)='\0';
233       strncpy(artist,buffer+33,30);
234       artist[30]='\0';
235       c=artist+29;
236       while (c>artist && *c==' ') *(c--)='\0';
237     }
238   }
239
240   fclose(fic);
241 }  
242
243 void parse_ogg(unsigned char *file)
244 {
245   FILE *fic;
246   unsigned char *c;
247   int lus;
248   int sample_rate;
249   int samples;
250
251   if (debug) fprintf(stderr,"Debug >> parsing ogg : %s\n",file);
252
253   /* read header */
254   if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }
255   lus=fread(buffer,1,OGG_BASE,fic);
256
257   /* try Ogg */
258   if (buffer[0]!='O' && buffer[1]!='g' && buffer[2]!='g')
259   {
260     fprintf(stderr,"Warning >> not a Ogg header : %s\n",file);
261     return;
262   }
263   
264   c=buffer+0x28;
265   sample_rate=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
266
267   while (c<buffer+lus-10)
268   {
269     int size;
270     if (strncasecmp(c,"TITLE=",6)==0)
271     {
272       size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);
273       strncpy(title,c+6,size-6);
274       title[size-6]='\0';
275       c+=size;
276     }
277     if (strncasecmp(c,"ARTIST=",7)==0)
278     {
279       size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);
280       strncpy(artist,c+7,size-7);
281       artist[size-7]='\0';
282       c+=size;
283     }
284     c++;
285   }
286   
287   fseek(fic,-OGG_BASE,SEEK_END);
288   lus=fread(buffer,1,OGG_BASE,fic);
289   c=buffer+lus-1;
290   while (strncmp(c,"OggS",4)!=0 && c>buffer) c--;
291   if (c!=buffer)
292   {
293     c+=6;
294     samples=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
295     duration=samples/sample_rate;
296   }
297   
298   fclose(fic);
299 }
300
301 void parse_directory(unsigned char *path)
302 {
303   int i,n;
304   struct dirent **namelist;
305   unsigned char newpath[PATH_MAX];
306   unsigned char *c;
307   struct stat infos;
308
309   void print_faketitle()
310   {
311     c=newpath+strlen(newpath);
312     while (c>newpath && *c!='/') c--;
313     while (c<newpath+strlen(newpath)-5)
314     {
315       c++;
316       if (*c=='_') putchar(' '); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
317     }
318   }
319   
320   void print_path()
321   {
322     printf(prefix);
323     for (c=newpath+skip;*c!='\0';c++) if (*c=='/') putchar(separator); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
324   }
325
326   if (debug) fprintf(stderr,"Debug >> parsing directory : %s\n",path);
327   if ((n=scandir(path,&namelist,0,alphasort))<0) { fprintf(stderr,"Warning >> can't open directory : %s\n",path); return; }
328   for (i=0;i<n;i++)
329   {
330     sprintf(newpath,"%s/%s",path,namelist[i]->d_name);
331     if (stat(newpath,&infos)!=0) { fprintf(stderr,"Warning >> can't stat file : %s\n",newpath); continue; }
332     if (recursive && S_ISDIR(infos.st_mode) && strcmp(namelist[i]->d_name,".")!=0 && strcmp(namelist[i]->d_name,"..")!=0) parse_directory(newpath);
333     if (S_ISREG(infos.st_mode))
334     {
335       unsigned char ext[5];
336       int j;
337       for (j=0;j<5;j++) ext[j]=tolower(namelist[i]->d_name[strlen(namelist[i]->d_name)-4+j]);
338       artist[0]='\0';
339       title[0]='\0';
340       duration=-2;
341       if (strcmp(".mp3",ext)==0) { duration=-1; parse_mp3(newpath); }
342       if (strcmp(".ogg",ext)==0) { duration=-1; parse_ogg(newpath); }
343       if (strcmp(".wav",ext)==0) { duration=-1; /* parse_wav(newpath); */ }
344       
345       if (duration!=-2) /* is it an audio file ? */
346       {
347         counter++;
348         switch (format)
349         {
350           case 0:
351             if (duration!=-1)
352             {
353               printf("#EXTINF:%d,",duration);
354               if (strlen(artist)==0 && strlen(title)==0) print_faketitle();
355               else printf("%s - %s",artist,title);
356               printf("%s",eol);
357             }
358             print_path();
359             printf("%s",eol);
360             break;
361           case 1:
362             printf("File%d=",counter);
363             print_path();
364             printf("%s",eol);
365             printf("Title%d=",counter);
366             if (strlen(artist)==0 && strlen(title)==0) print_faketitle();
367             else printf("%s - %s",artist,title);
368             printf("%s",eol);
369             if (duration!=-1) printf("Length%d=%d%s",counter,duration,eol);
370             break;
371           case 2:
372             printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>",counter,artist,title);
373             if (duration==-1) printf("?"); else printf("%d:%s%d",duration/60,duration%60<10?"0":"",duration%60);
374             printf("</td></tr>%s",eol);
375             break;
376         }
377       }
378     }
379     free(namelist[i]);
380   }
381   free(namelist);
382 }
383
384 int main(int argc,char **argv)
385 {
386   parse_options(argc,argv);
387   if (optind==argc) usage();
388   switch (format)
389   {
390     case 0:
391       printf("#EXTM3U%s",eol);
392       break;
393     case 1:
394       printf("[playlist]%s,eol");
395       break;
396     case 2:
397       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.22</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);
398       break;
399   }
400   for (;optind<argc;optind++)
401   {
402     if (strlen(prefix)!=0) skip=strlen(argv[optind]);
403     parse_directory(argv[optind]);
404   }
405   switch(format)
406   {
407     case 1:
408       printf("NumberOfEntries=%d%sVersion=2%s",counter,eol,eol);
409       break;
410     case 2:
411       printf("</table>%s%s<p>Playlist generated by <a href=\"http://royale.zerezo.com/fapg/\">FAPG 0.22</a></p>%s%s</body>%s%s</html>",eol,eol,eol,eol,eol,eol);
412       break;
413   }
414   exit(0);
415 }