-/*\r
- * FAPG 0.1 released under GPL\r
- * http://royale.zerezo.com/fapg/\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <getopt.h>\r
-#include <dirent.h>\r
-#include <sys/stat.h>\r
-#include <sys/types.h>\r
-#include <string.h>\r
-#include <limits.h>\r
-#include <unistd.h>\r
-#include <ctype.h>\r
-\r
-#define MAX 10240\r
-\r
-int debug=0;\r
-int format=0; /* 0 = m3u ; 1 = pls ; 2 = html */\r
-unsigned char *prefix="";\r
-int recursive=0;\r
-int separator='/';\r
-int skip=0;\r
-int windows=0;\r
-unsigned char buffer[MAX];\r
-\r
-int counter=0;\r
-\r
-unsigned char artist[1024];\r
-unsigned char title[1024];\r
-int duration;\r
-\r
-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
-\r
-void usage()\r
-{\r
- 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");\r
- exit(1);\r
-}\r
-\r
-void parse_options(int argc,char **argv)\r
-{\r
- static char const short_options[]="bdf:o:p:rw";\r
- static struct option long_options[]=\r
- {\r
- {"backslash",no_argument,NULL,'b'},\r
- {"debug",no_argument,NULL,'d'},\r
- {"format",required_argument,NULL,'f'},\r
- {"output",required_argument,NULL,'o'},\r
- {"prefix",required_argument,NULL,'p'},\r
- {"recursive",no_argument,NULL,'r'},\r
- {"windows",no_argument,NULL,'w'}\r
- };\r
- int c;\r
- int option_index=0;\r
- while ((c=getopt_long(argc,argv,short_options,long_options,&option_index))!=-1)\r
- {\r
- switch(c)\r
- {\r
- case 'b':\r
- separator='\\';\r
- break;\r
- case 'd':\r
- debug=1;\r
- break;\r
- case 'f':\r
- if (strcmp(optarg,"m3u")==0) format=0; else\r
- if (strcmp(optarg,"pls")==0) format=1; else\r
- if (strcmp(optarg,"html")==0) format=2; else\r
- usage();\r
- break;\r
- case 'o':\r
- close(1);\r
- if (fopen(optarg,"w")==NULL) { fprintf(stderr,"Error >> unable to open output file : %s\n",optarg); exit(2); }\r
- break;\r
- case 'p':\r
- prefix=malloc(strlen(optarg)+1);\r
- strcpy(prefix,optarg);\r
- break;\r
- case 'r':\r
- recursive=1;\r
- break;\r
- case 'w':\r
- windows=1;\r
- break;\r
- default:\r
- usage();\r
- }\r
- }\r
-}\r
-\r
-void parse_mp3(unsigned char *file)\r
-{\r
- int bitrates[2][3][15]=\r
- {{{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448},\r
- {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},\r
- {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}},\r
- {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},\r
- {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},\r
- {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}}};\r
- FILE *fic;\r
- unsigned char *c;\r
- int lus;\r
-\r
- if (debug) fprintf(stderr,"Debug >> parsing mp3 : %s\n",file);\r
-\r
- /* read header */\r
- if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }\r
- lus=fread(buffer,1,MAX,fic);\r
- c=buffer; \r
-\r
- /* try ID3v2 */\r
- if (buffer[0]=='I' && buffer[1]=='D' && buffer[2]=='3')\r
- {\r
- int size;\r
- int version;\r
- version=*(buffer+3);\r
- if (version<2 || version>4)\r
- fprintf(stderr,"Warning >> ID3 v2.%d not implemented ! trying anyway : %s\n",version,file);\r
- if (*(buffer+5)!=0)\r
- fprintf(stderr,"Warning >> specials headers not implemented (%d) ! trying anyway : %s\n",*(buffer+5),file);\r
- c=buffer+6;\r
- size=(*c<<24)+(*(c+1)<<16)+(*(c+2)<<8)+(*(c+3));\r
- if (size>lus) size=lus;\r
- c+=4;\r
- if (version==2) while (c<buffer+size-10)\r
- {\r
- int size=(*(c+3)<<16)+(*(c+4)<<8)+(*(c+5));\r
- if (*c==0) break;\r
- if (strncmp(c,"TT2",3)==0)\r
- {\r
- strncpy(title,c+7,size-1);\r
- title[size-1]='\0';\r
- }\r
- if (strncmp(c,"TP1",3)==0)\r
- {\r
- strncpy(artist,c+7,size-1);\r
- artist[size-1]='\0';\r
- }\r
- c+=size+6;\r
- }\r
- if (version==3 || version==4) while (c<buffer+size-10)\r
- {\r
- int size=(*(c+4)<<24)+(*(c+5)<<16)+(*(c+6)<<8)+(*(c+7));\r
- if (*c==0) break;\r
- if (strncmp(c,"TIT2",4)==0)\r
- {\r
- strncpy(title,c+11,size-1);\r
- title[size-1]='\0';\r
- }\r
- if (strncmp(c,"TPE1",4)==0)\r
- {\r
- strncpy(artist,c+11,size-1);\r
- artist[size-1]='\0';\r
- }\r
- c+=size+10;\r
- }\r
- }\r
- \r
- while (c<buffer+lus-10)\r
- {\r
- if (*c==0xFF && (*(c+1)&0xF0)==0xF0)\r
- {\r
- int version;\r
- int lay;\r
- int bitrate_index;\r
- int bitrate;\r
- version=2-(*(c+1)>>3&1);\r
- lay=4-(*(c+1)>>1&3);\r
- bitrate_index=*(c+2)>>4&0xF;\r
- if (version>=1 && version<=2 && lay-1>=0 && lay-1<=2 && bitrate_index>=0 && bitrate_index<=14)\r
- bitrate=bitrates[version-1][lay-1][bitrate_index];\r
- else\r
- bitrate=0;\r
- if (bitrate!=0)\r
- {\r
- fseek(fic,0,SEEK_END);\r
- duration=(ftell(fic)+buffer-c)/125/bitrate;\r
- }\r
- else\r
- duration=0;\r
- break;\r
- }\r
- c++;\r
- }\r
-\r
- /* try ID3v1 */\r
- if (strlen(artist)==0 && strlen(title)==0)\r
- {\r
- fseek(fic,-128,SEEK_END);\r
- lus=fread(buffer,1,128,fic);\r
- if (lus==128 && buffer[0]=='T' && buffer[1]=='A' && buffer[2]=='G')\r
- {\r
- strncpy(title,buffer+3,30);\r
- title[30]='\0';\r
- c=title+29;\r
- while (c>title && *c==' ') *(c--)='\0';\r
- strncpy(artist,buffer+33,30);\r
- artist[30]='\0';\r
- c=artist+29;\r
- while (c>artist && *c==' ') *(c--)='\0';\r
- }\r
- }\r
-\r
- fclose(fic);\r
-} \r
-\r
-void parse_ogg(unsigned char *file)\r
-{\r
- FILE *fic;\r
- unsigned char *c;\r
- int lus;\r
- int sample_rate;\r
- int samples;\r
-\r
- if (debug) fprintf(stderr,"Debug >> parsing ogg : %s\n",file);\r
-\r
- /* read header */\r
- if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }\r
- lus=fread(buffer,1,MAX,fic);\r
-\r
- /* try Ogg */\r
- if (buffer[0]!='O' && buffer[1]!='g' && buffer[2]!='g')\r
- {\r
- fprintf(stderr,"Warning >> not a Ogg header : %s\n",file);\r
- return;\r
- }\r
- \r
- c=buffer+0x28;\r
- sample_rate=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);\r
-\r
- while (c<buffer+lus-10)\r
- {\r
- int size;\r
- if (strncmp(c,"TITLE=",6)==0)\r
- {\r
- size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);\r
- strncpy(title,c+6,size-6);\r
- title[size-6]='\0';\r
- c+=size;\r
- }\r
- if (strncmp(c,"ARTIST=",7)==0)\r
- {\r
- size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);\r
- strncpy(artist,c+7,size-7);\r
- artist[size-7]='\0';\r
- c+=size;\r
- }\r
- c++;\r
- }\r
- \r
- fseek(fic,-MAX,SEEK_END);\r
- lus=fread(buffer,1,MAX,fic);\r
- c=buffer+lus-1;\r
- while (strncmp(c,"OggS",4)!=0 && c>buffer) c--;\r
- if (c!=buffer)\r
- {\r
- c+=6;\r
- samples=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);\r
- duration=samples/sample_rate;\r
- }\r
- \r
- fclose(fic);\r
-}\r
-\r
-void parse_directory(unsigned char *path)\r
-{\r
- int i,n;\r
- struct dirent **namelist;\r
- unsigned char newpath[PATH_MAX];\r
- unsigned char *c;\r
- struct stat infos;\r
-\r
- void print_faketitle()\r
- {\r
- c=newpath+strlen(newpath);\r
- while (c>newpath && *c!='/') c--;\r
- while (c<newpath+strlen(newpath)-5)\r
- {\r
- c++;\r
- if (*c=='_') putchar(' '); else if (windows) putchar(unix2dos[*c]); else putchar(*c);\r
- }\r
- }\r
- \r
- void print_path()\r
- {\r
- printf(prefix);\r
- for (c=newpath+skip;*c!='\0';c++) if (*c=='/') putchar(separator); else if (windows) putchar(unix2dos[*c]); else putchar(*c);\r
- }\r
-\r
- if (debug) fprintf(stderr,"Debug >> parsing directory : %s\n",path);\r
- if ((n=scandir(path,&namelist,0,alphasort))<0) { fprintf(stderr,"Warning >> can't open directory : %s\n",path); return; }\r
- for (i=0;i<n;i++)\r
- {\r
- sprintf(newpath,"%s/%s",path,namelist[i]->d_name);\r
- if (stat(newpath,&infos)!=0) { fprintf(stderr,"Warning >> can't stat file : %s\n",newpath); continue; }\r
- if (recursive && S_ISDIR(infos.st_mode) && strcmp(namelist[i]->d_name,".")!=0 && strcmp(namelist[i]->d_name,"..")!=0) parse_directory(newpath);\r
- if (S_ISREG(infos.st_mode))\r
- {\r
- unsigned char ext[5];\r
- int j;\r
- for (j=0;j<5;j++) ext[j]=tolower(namelist[i]->d_name[strlen(namelist[i]->d_name)-4+j]);\r
- artist[0]='\0';\r
- title[0]='\0';\r
- duration=-2;\r
- if (strcmp(".mp3",ext)==0) { duration=-1; parse_mp3(newpath); }\r
- if (strcmp(".ogg",ext)==0) { duration=-1; parse_ogg(newpath); }\r
- if (strcmp(".wav",ext)==0) { duration=-1; /* parse_wav(newpath); */ }\r
- \r
- if (duration!=-2) /* is it an audio file ? */\r
- {\r
- counter++;\r
- switch (format)\r
- {\r
- case 0:\r
- if (duration!=-1)\r
- {\r
- printf("#EXTINF:%d,",duration);\r
- if (strlen(artist)==0 && strlen(title)==0) print_faketitle();\r
- else printf("%s - %s",artist,title);\r
- putchar('\n');\r
- }\r
- print_path();\r
- putchar('\n');\r
- break;\r
- case 1:\r
- printf("File%d=",counter);\r
- print_path();\r
- putchar('\n');\r
- printf("Title%d=",counter);\r
- if (strlen(artist)==0 && strlen(title)==0) print_faketitle();\r
- else printf("%s - %s",artist,title);\r
- putchar('\n');\r
- if (duration!=-1) printf("Length%d=%d\n",counter,duration);\r
- break;\r
- case 2:\r
- printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>",counter,artist,title);\r
- if (duration==-1) printf("?"); else printf("%d:%s%d",duration/60,duration%60<10?"0":"",duration%60);\r
- printf("</td></tr>\n");\r
- break;\r
- }\r
- }\r
- }\r
- free(namelist[i]);\r
- }\r
- free(namelist);\r
-}\r
-\r
-int main(int argc,char **argv)\r
-{\r
- parse_options(argc,argv);\r
- if (optind==argc) usage();\r
- switch (format)\r
- {\r
- case 0:\r
- printf("#EXTM3U\n");\r
- break;\r
- case 1:\r
- printf("[playlist]\n");\r
- break;\r
- case 2:\r
- 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.1</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n<style type=\"text/css\">\n<!--\n\nbody,td,tr {\n font-family: Verdana, Arial, Helvetica, sans-serif;\n font-size: 12px;\n color: #000000;\n}\n\nbody {\n background: #ffffff;\n}\n\nth {\n text-align: center;\n background: #ffcccc;\n padding-left: 15px;\n padding-right: 15px;\n border: 1px #dd8888 solid;\n}\n\ntd {\n text-align: center;\n background: #eeeeee;\n padding-left: 15px;\n padding-right: 15px;\n border: 1px #cccccc solid;\n}\n\nh1 {\n font-size: 25px;\n}\n\np {\n font-size: 10px;\n}\n\na {\n color: #993333;\n text-decoration: none;\n}\n\na:hover {\n text-decoration: underline;\n}\n\n-->\n</style>\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
- break;\r
- }\r
- for (;optind<argc;optind++)\r
- {\r
- if (strlen(prefix)!=0) skip=strlen(argv[optind]);\r
- parse_directory(argv[optind]);\r
- }\r
- switch(format)\r
- {\r
- case 1:\r
- printf("NumberOfEntries=%d\nVersion=2\n",counter);\r
- break;\r
- case 2:\r
- printf("</table>\n\n<p>Playlist generated by <a href=\"http://royale.zerezo.com/fapg/\">FAPG 0.1</a></p>\n\n</body>\n\n</html>");\r
- break;\r
- }\r
- exit(0);\r
-}\r
+/*
+ * FAPG 0.2 released under GPL
+ * http://royale.zerezo.com/fapg/
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <string.h>
+#include <limits.h>
+#include <unistd.h>
+#include <ctype.h>
+
+#define MP3_BASE 1024
+#define OGG_BASE 1024*10
+#define MAX 1024*200 /* 200ko for ID3 with JPEG images in it */
+
+int debug=0;
+int format=0; /* 0 = m3u ; 1 = pls ; 2 = html */
+unsigned char *prefix="";
+int recursive=0;
+int separator='/';
+int skip=0;
+int windows=0;
+unsigned char *eol="\n";
+unsigned char buffer[MAX];
+
+int counter=0;
+
+unsigned char artist[1024];
+unsigned char title[1024];
+int duration;
+
+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};
+
+void usage()
+{
+ 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");
+ exit(1);
+}
+
+void parse_options(int argc,char **argv)
+{
+ static char const short_options[]="bdf:o:p:rw";
+ static struct option long_options[]=
+ {
+ {"backslash",no_argument,NULL,'b'},
+ {"debug",no_argument,NULL,'d'},
+ {"format",required_argument,NULL,'f'},
+ {"output",required_argument,NULL,'o'},
+ {"prefix",required_argument,NULL,'p'},
+ {"recursive",no_argument,NULL,'r'},
+ {"windows",no_argument,NULL,'w'}
+ };
+ int c;
+ int option_index=0;
+ while ((c=getopt_long(argc,argv,short_options,long_options,&option_index))!=-1)
+ {
+ switch(c)
+ {
+ case 'b':
+ separator='\\';
+ break;
+ case 'd':
+ debug=1;
+ break;
+ case 'f':
+ if (strcmp(optarg,"m3u")==0) format=0; else
+ if (strcmp(optarg,"pls")==0) format=1; else
+ if (strcmp(optarg,"html")==0) format=2; else
+ usage();
+ break;
+ case 'o':
+ close(1);
+ if (fopen(optarg,"w")==NULL) { fprintf(stderr,"Error >> unable to open output file : %s\n",optarg); exit(2); }
+ break;
+ case 'p':
+ prefix=malloc(strlen(optarg)+1);
+ strcpy(prefix,optarg);
+ break;
+ case 'r':
+ recursive=1;
+ break;
+ case 'w':
+ windows=1;
+ eol="\r\n";
+ break;
+ default:
+ usage();
+ }
+ }
+}
+
+void parse_mp3(unsigned char *file)
+{
+ int bitrates[2][3][15]=
+ {{{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448},
+ {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
+ {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}},
+ {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
+ {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
+ {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}}};
+ FILE *fic;
+ unsigned char *c;
+ int lus;
+
+ if (debug) fprintf(stderr,"Debug >> parsing mp3 : %s\n",file);
+
+ /* read header */
+ if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }
+ lus=fread(buffer,1,MP3_BASE,fic);
+ c=buffer;
+
+ /* try ID3v2 */
+ if (buffer[0]=='I' && buffer[1]=='D' && buffer[2]=='3')
+ {
+ int size;
+ int version;
+ version=*(buffer+3);
+ if (version<2 || version>4)
+ fprintf(stderr,"Warning >> ID3 v2.%d not implemented ! trying anyway : %s\n",version,file);
+ if (*(buffer+5)!=0)
+ fprintf(stderr,"Warning >> specials headers not implemented (%d) ! trying anyway : %s\n",*(buffer+5),file);
+ c=buffer+6;
+ size=(*c<<21)+(*(c+1)<<14)+(*(c+2)<<7)+(*(c+3));
+ /* read more header */
+ if (size+lus>MAX)
+ {
+ lus+=fread(buffer+lus,1,MAX-lus,fic);
+ fprintf(stderr,"Warning >> ID3 header is huge (%d bytes) ! trying anyway : %s\n",size,file);
+ }
+ else
+ lus+=fread(buffer+lus,1,size,fic);
+ if (size>lus) size=lus;
+ c+=4;
+ if (version==2) while (c<buffer+size)
+ {
+ int size=(*(c+3)<<16)+(*(c+4)<<8)+(*(c+5));
+ if (*c==0) break;
+ if (strncmp(c,"TT2",3)==0)
+ {
+ strncpy(title,c+7,size-1);
+ title[size-1]='\0';
+ }
+ if (strncmp(c,"TP1",3)==0)
+ {
+ strncpy(artist,c+7,size-1);
+ artist[size-1]='\0';
+ }
+ c+=size+6;
+ }
+ if (version==3 || version==4) while (c<buffer+size)
+ {
+ int size=(*(c+4)<<24)+(*(c+5)<<16)+(*(c+6)<<8)+(*(c+7));
+ if (*c==0) break;
+ if (strncmp(c,"TIT2",4)==0)
+ {
+ strncpy(title,c+11,size-1);
+ title[size-1]='\0';
+ }
+ if (strncmp(c,"TPE1",4)==0)
+ {
+ strncpy(artist,c+11,size-1);
+ artist[size-1]='\0';
+ }
+ c+=size+10;
+ }
+ }
+
+ while (c<buffer+lus-10)
+ {
+ if (*c==0xFF && (*(c+1)&0xF0)==0xF0)
+ {
+ int version;
+ int lay;
+ int bitrate_index;
+ int bitrate;
+ version=2-(*(c+1)>>3&1);
+ lay=4-(*(c+1)>>1&3);
+ bitrate_index=*(c+2)>>4&0xF;
+ if (version>=1 && version<=2 && lay-1>=0 && lay-1<=2 && bitrate_index>=0 && bitrate_index<=14)
+ bitrate=bitrates[version-1][lay-1][bitrate_index];
+ else
+ bitrate=0;
+ if (bitrate!=0)
+ {
+ fseek(fic,0,SEEK_END);
+ duration=(ftell(fic)+buffer-c)/125/bitrate;
+ }
+ else
+ duration=0;
+ break;
+ }
+ c++;
+ }
+
+ /* try ID3v1 */
+ if (strlen(artist)==0 && strlen(title)==0)
+ {
+ fseek(fic,-128,SEEK_END);
+ lus=fread(buffer,1,128,fic);
+ if (lus==128 && buffer[0]=='T' && buffer[1]=='A' && buffer[2]=='G')
+ {
+ strncpy(title,buffer+3,30);
+ title[30]='\0';
+ c=title+29;
+ while (c>title && *c==' ') *(c--)='\0';
+ strncpy(artist,buffer+33,30);
+ artist[30]='\0';
+ c=artist+29;
+ while (c>artist && *c==' ') *(c--)='\0';
+ }
+ }
+
+ fclose(fic);
+}
+
+void parse_ogg(unsigned char *file)
+{
+ FILE *fic;
+ unsigned char *c;
+ int lus;
+ int sample_rate;
+ int samples;
+
+ if (debug) fprintf(stderr,"Debug >> parsing ogg : %s\n",file);
+
+ /* read header */
+ if ((fic=fopen(file,"r"))==NULL) { fprintf(stderr,"Warning >> can't open file : %s\n",file); return; }
+ lus=fread(buffer,1,OGG_BASE,fic);
+
+ /* try Ogg */
+ if (buffer[0]!='O' && buffer[1]!='g' && buffer[2]!='g')
+ {
+ fprintf(stderr,"Warning >> not a Ogg header : %s\n",file);
+ return;
+ }
+
+ c=buffer+0x28;
+ sample_rate=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
+
+ while (c<buffer+lus-10)
+ {
+ int size;
+ if (strncmp(c,"TITLE=",6)==0)
+ {
+ size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);
+ strncpy(title,c+6,size-6);
+ title[size-6]='\0';
+ c+=size;
+ }
+ if (strncmp(c,"ARTIST=",7)==0)
+ {
+ size=*(c-4)+(*(c-3)<<8)+(*(c-2)<<16)+(*(c-1)<<24);
+ strncpy(artist,c+7,size-7);
+ artist[size-7]='\0';
+ c+=size;
+ }
+ c++;
+ }
+
+ fseek(fic,-OGG_BASE,SEEK_END);
+ lus=fread(buffer,1,OGG_BASE,fic);
+ c=buffer+lus-1;
+ while (strncmp(c,"OggS",4)!=0 && c>buffer) c--;
+ if (c!=buffer)
+ {
+ c+=6;
+ samples=(*c)+(*(c+1)<<8)+(*(c+2)<<16)+(*(c+3)<<24);
+ duration=samples/sample_rate;
+ }
+
+ fclose(fic);
+}
+
+void parse_directory(unsigned char *path)
+{
+ int i,n;
+ struct dirent **namelist;
+ unsigned char newpath[PATH_MAX];
+ unsigned char *c;
+ struct stat infos;
+
+ void print_faketitle()
+ {
+ c=newpath+strlen(newpath);
+ while (c>newpath && *c!='/') c--;
+ while (c<newpath+strlen(newpath)-5)
+ {
+ c++;
+ if (*c=='_') putchar(' '); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
+ }
+ }
+
+ void print_path()
+ {
+ printf(prefix);
+ for (c=newpath+skip;*c!='\0';c++) if (*c=='/') putchar(separator); else if (windows) putchar(unix2dos[*c]); else putchar(*c);
+ }
+
+ if (debug) fprintf(stderr,"Debug >> parsing directory : %s\n",path);
+ if ((n=scandir(path,&namelist,0,alphasort))<0) { fprintf(stderr,"Warning >> can't open directory : %s\n",path); return; }
+ for (i=0;i<n;i++)
+ {
+ sprintf(newpath,"%s/%s",path,namelist[i]->d_name);
+ if (stat(newpath,&infos)!=0) { fprintf(stderr,"Warning >> can't stat file : %s\n",newpath); continue; }
+ if (recursive && S_ISDIR(infos.st_mode) && strcmp(namelist[i]->d_name,".")!=0 && strcmp(namelist[i]->d_name,"..")!=0) parse_directory(newpath);
+ if (S_ISREG(infos.st_mode))
+ {
+ unsigned char ext[5];
+ int j;
+ for (j=0;j<5;j++) ext[j]=tolower(namelist[i]->d_name[strlen(namelist[i]->d_name)-4+j]);
+ artist[0]='\0';
+ title[0]='\0';
+ duration=-2;
+ if (strcmp(".mp3",ext)==0) { duration=-1; parse_mp3(newpath); }
+ if (strcmp(".ogg",ext)==0) { duration=-1; parse_ogg(newpath); }
+ if (strcmp(".wav",ext)==0) { duration=-1; /* parse_wav(newpath); */ }
+
+ if (duration!=-2) /* is it an audio file ? */
+ {
+ counter++;
+ switch (format)
+ {
+ case 0:
+ if (duration!=-1)
+ {
+ printf("#EXTINF:%d,",duration);
+ if (strlen(artist)==0 && strlen(title)==0) print_faketitle();
+ else printf("%s - %s",artist,title);
+ printf("%s",eol);
+ }
+ print_path();
+ printf("%s",eol);
+ break;
+ case 1:
+ printf("File%d=",counter);
+ print_path();
+ printf("%s",eol);
+ printf("Title%d=",counter);
+ if (strlen(artist)==0 && strlen(title)==0) print_faketitle();
+ else printf("%s - %s",artist,title);
+ printf("%s",eol);
+ if (duration!=-1) printf("Length%d=%d%s",counter,duration,eol);
+ break;
+ case 2:
+ printf("<tr><td>%d</td><td>%s</td><td>%s</td><td>",counter,artist,title);
+ if (duration==-1) printf("?"); else printf("%d:%s%d",duration/60,duration%60<10?"0":"",duration%60);
+ printf("</td></tr>%s",eol);
+ break;
+ }
+ }
+ }
+ free(namelist[i]);
+ }
+ free(namelist);
+}
+
+int main(int argc,char **argv)
+{
+ parse_options(argc,argv);
+ if (optind==argc) usage();
+ switch (format)
+ {
+ case 0:
+ printf("#EXTM3U%s",eol);
+ break;
+ case 1:
+ printf("[playlist]%s,eol");
+ break;
+ case 2:
+ 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);
+ break;
+ }
+ for (;optind<argc;optind++)
+ {
+ if (strlen(prefix)!=0) skip=strlen(argv[optind]);
+ parse_directory(argv[optind]);
+ }
+ switch(format)
+ {
+ case 1:
+ printf("NumberOfEntries=%d%sVersion=2%s",counter,eol,eol);
+ break;
+ case 2:
+ 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);
+ break;
+ }
+ exit(0);
+}