#include #include #include #include #include #include #include #include #include #include "lamefuncs.h" #include "opts.h" using namespace std; int main(int argc, char **argv) { int preIdent; int ident=1; //this variable sets the filname format in readtag() ie [ album - artist - track - title.mp3 ] etc. string preset, presetStr; bool deleteFlac = false; bool silent = false; parseCmdLn (argc, argv, preset, preIdent, ident, deleteFlac, silent); if(silent) cout << "\nencoding please wait...\n"; struct dirent *direct; DIR *flacdir = opendir("."); if(flacdir != NULL) { while((direct = readdir(flacdir)) != NULL) { if(strstr(direct->d_name, ".flac")) { string file = (direct->d_name); //start tagcode string mp3name; string album; string artist; string title; string genre; string date; //Start of code to read tags from the flac file. int trcknbr; //Track number to be converted to int from string using sstream. string tracknumber; string systrMeta = "metaflac --export-tags-to=metadata --no-utf8-convert \"" + file + "\""; system(systrMeta.c_str()); fstream meta; meta.open("metadata", ios::in | ios::binary); if (meta.is_open()) { while(! meta.eof() ) { string testAlb = "album="; string testAlbUc = "ALBUM="; string testAlbUc1 = "Album="; string testArt = "artist="; string testArtUc = "ARTIST="; string testArtUc1 = "Artist="; string testTitl = "title="; string testTitlUc = "TITLE="; string testTitlUc1 = "Title="; string testGen = "genre="; string testGenUc = "GENRE="; string testGenUc1 = "Genre="; string testDate = "date="; string testDateUc = "DATE="; string testDateUc1 = "Date="; string testTrack = "tracknumber="; string testTrackUc = "TRACKNUMBER="; string testTrackUc1 = "TrackNumber="; string testTrackUc2 = "Tracknumber="; string line; // String to hold lines in metadata. getline(meta, line); static int i; for(i=0; i*\?\"|:", line[i])) line[i]=','; if(!(line.find(testAlb, 0)==string::npos) || !(line.find(testAlbUc, 0)==string::npos) || !(line.find(testAlbUc1, 0)==string::npos)) { album = line.substr (6,(line.length()-6)); } else if(!(line.find(testArt, 0)==string::npos) || !(line.find(testArtUc, 0)==string::npos) || !(line.find(testArtUc1, 0)==string::npos)) { artist = line.substr (7,(line.length()-7)); } else if(!(line.find(testTitl, 0)==string::npos) || !(line.find(testTitlUc, 0)==string::npos) || !(line.find(testTitlUc1, 0)==string::npos)) { title = line.substr (6,(line.length()-6)); } else if(!(line.find(testGen, 0)==string::npos) || !(line.find(testGenUc, 0)==string::npos) || !(line.find(testGenUc1, 0)==string::npos)) { genre = line.substr (6,(line.length()-6)); } else if(!(line.find(testDate, 0)==string::npos) || !(line.find(testDateUc, 0)==string::npos) || !(line.find(testDateUc1, 0)==string::npos)) { date = line.substr (5,(line.length()-5)); } else if(!(line.find(testTrack, 0)==string::npos) || !(line.find(testTrackUc, 0)==string::npos) || !(line.find(testTrackUc1, 0)==string::npos) || !(line.find(testTrackUc2, 0)==string::npos)) { tracknumber = line.substr (12,(line.length()-12)); } else continue; } } meta.close(); unlink("metadata"); stringstream (tracknumber) >> trcknbr; stringstream strm; strm << trcknbr; string track; string str(strm.str()); if(trcknbr < 10) track = "0" + str; else track = str; string noTagStr = file.substr(0, (file.length()-5)) + ".mp3"; if(ident==1) { if(!title.empty() && !tracknumber.empty()) mp3name = track + " - " + title + ".mp3"; else mp3name = noTagStr; } else if(ident==2) { if(!title.empty() && !artist.empty()) mp3name = artist + " - " + title + ".mp3"; else mp3name = noTagStr; } else if(ident==3) { if(!title.empty() && !tracknumber.empty() && !album.empty() && !artist.empty()) mp3name = artist + " - " + album + " - " + track + " - " + title + ".mp3"; else mp3name = noTagStr; } else mp3name = noTagStr; //End of code reading tags from flac file. //cout << mp3name << endl; //Make lame string int i=0; if(preset.empty()) preset = "-h "; if(silent) presetStr = "lame --silent "; else presetStr = "lame "; presetStr.append(preset); char titleC[]="", genreC, trackC[3]="", mp3nameC[100]=""; strcat(mp3nameC, mp3name.c_str()); mp3nameC[strlen(mp3nameC)-1] = '\0'; //strncat(trackC, track.c_str(), 2); //strcat(titleC, title.c_str()); //trackC[strlen(trackC) - strlen(titleC)] = '\0'; titleC[strlen(titleC) - 1] = '\0'; if(!silent) { cout << "\n****************************************************************" << endl; cout << "File: " << file << endl; cout << "Artist:\t\t " << artist << endl; cout << "Track:\t\t " << track << endl; cout << "Album:\t\t " << album << endl; cout << "Title:\t\t " << title << endl; cout << "Genre:\t\t " << genre << endl; cout << "Date:\t\t " << date << endl; cout << "Tracknumber:\t " << tracknumber << endl; printf("%s\n", mp3nameC); //printf("mp3 filename:\t %s - %s", trackC, titleC); //printf(" - %s", titleC); printf(".mp3\n"); cout << "****************************************************************\n" << endl; } string lameSysStr = presetStr + " temp.wav --tt \"" + title + "\" --ta \"" + artist + "\" --tl \"" + album + "\" --ty \"" + date + "\" --tc \"ÜberStandard - Ubershare.org\"" + " --tn \"" + track + "\" --tg \"" + genre + "\" --add-id3v2 \"" + mp3name + "\""; //finish tagcode string systr = "flac -ds -o temp.wav \"" + file + "\""; system(systr.c_str()); system(lameSysStr.c_str()); if (deleteFlac) unlink(file.c_str()); unlink("temp.wav"); } } } closedir(flacdir); return 0; }