2 * FAPG 0.0 released under GPL
\r
3 * http://royale.zerezo.com/fapg/
\r
10 #include <sys/types.h>
\r
17 int format=0; /* 0 = m3u ; 1 = pls ; 2 = html */
\r
18 unsigned char *prefix="";
\r
23 unsigned char buffer[MAX];
\r
27 unsigned char artist[1024];
\r
28 unsigned char title[1024];
\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
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
39 void parse_options(int argc,char **argv)
\r
41 static struct option long_options[]=
\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
53 while ((c=getopt_long_only(argc,argv,"",long_options,&option_index))!=-1)
\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
67 if (fopen(optarg,"w")==NULL) { fprintf(stderr,"Error >> unable to open output file : %s\n",optarg); exit(2); }
\r
70 prefix=malloc(strlen(optarg)+1);
\r
71 strcpy(prefix,optarg);
\r
79 void parse_mp3(unsigned char *file)
\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
92 if (debug) fprintf(stderr,"Debug >> parsing mp3 : %s\n",file);
\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
100 if (buffer[0]=='I' && buffer[1]=='D' && buffer[2]=='3')
\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
110 size=(*c<<24)+(*(c+1)<<16)+(*(c+2)<<8)+(*(c+3));
\r
111 if (size>lus) size=lus;
\r
113 if (version==2) while (c<buffer+size-5)
\r
115 int size=(*(c+3)<<16)+(*(c+4)<<8)+(*(c+5));
\r
117 if (strncmp(c,"TT2",3)==0)
\r
119 strncpy(title,c+7,size-1);
\r
120 title[size-1]='\0';
\r
122 if (strncmp(c,"TP1",3)==0)
\r
124 strncpy(artist,c+7,size-1);
\r
125 artist[size-1]='\0';
\r
129 if (version==3 || version==4) while (c<buffer+size-7)
\r
131 int size=(*(c+4)<<24)+(*(c+5)<<16)+(*(c+6)<<8)+(*(c+7));
\r
133 if (strncmp(c,"TIT2",4)==0)
\r
135 strncpy(title,c+11,size-1);
\r
136 title[size-1]='\0';
\r
138 if (strncmp(c,"TPE1",4)==0)
\r
140 strncpy(artist,c+11,size-1);
\r
141 artist[size-1]='\0';
\r
147 while (c<buffer+lus-10)
\r
149 if (*c==0xFF && (*(c+1)&0xF0)==0xF0)
\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
164 fseek(fic,0,SEEK_END);
\r
165 time=(ftell(fic)+buffer-c)/125/bitrate;
\r
175 if (strlen(artist)==0 && strlen(title)==0)
\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
181 strncpy(title,buffer+3,30);
\r
184 while (c>title && *c==' ') *(c--)='\0';
\r
185 strncpy(artist,buffer+33,30);
\r
188 while (c>artist && *c==' ') *(c--)='\0';
\r
195 void parse_ogg(unsigned char *file)
\r
203 if (debug) fprintf(stderr,"Debug >> parsing ogg : %s\n",file);
\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
210 if (buffer[0]!='O' && buffer[1]!='g' && buffer[2]!='g')
\r
212 fprintf(stderr,"Warning >> not a Ogg header : %s\n",file);
\r
217 sample_rate=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
\r
219 while (c<buffer+lus-10)
\r
222 if (strncmp(c,"TITLE=",6)==0)
\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
229 if (strncmp(c,"ARTIST=",7)==0)
\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
239 fseek(fic,-MAX,SEEK_END);
\r
240 lus=fread(buffer,1,MAX,fic);
\r
242 while (strncmp(c,"OggS",4)!=0 && c>buffer) c--;
\r
246 samples=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
\r
247 time=samples/sample_rate;
\r
253 void parse_directory(unsigned char *path)
\r
257 unsigned char newpath[PATH_MAX];
\r
260 void print_faketitle()
\r
262 c=newpath+strlen(newpath);
\r
263 while (c>newpath && *c!='/') c--;
\r
264 while (c<newpath+strlen(newpath)-5)
\r
267 if (*c=='_') putchar(' '); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
\r
274 for (c=newpath+skip;*c!='\0';c++) if (*c=='/') putchar(separator); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
\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
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
284 parse_directory(newpath);
\r
286 if (dp->d_type & DT_REG)
\r
288 unsigned char ext[5];
\r
290 for (i=0;i<5;i++) ext[i]=tolower(dp->d_name[strlen(dp->d_name)-4+i]);
\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
298 if (time!=-2) /* is it an audio file ? */
\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
315 printf("File%d=",counter);
\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
322 if (time!=-1) printf("Length%d=%d\n",counter,time);
\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
336 int main(int argc,char **argv)
\r
338 parse_options(argc,argv);
\r
339 if (optind==argc) usage();
\r
343 printf("#EXTM3U\n");
\r
346 printf("[playlist]\n");
\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
352 for (;optind<argc;optind++)
\r
354 if (strlen(prefix)!=0) skip=strlen(argv[optind]);
\r
355 parse_directory(argv[optind]);
\r
360 printf("NumberOfEntries=%d\nVersion=2\n",counter);
\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