00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "stdafx.h"
00036
00037 #include <libOOOgg/libOOOgg.h>
00038 #include <libOOOgg/dllstuff.h>
00039 #include "VorbisComments.h"
00040 #include <iostream>
00041 #include <fstream>
00042 #include <vector>
00043
00044 unsigned long bytePos = 0;
00045 bool needComments = false;
00046 bool inStream = false;
00047 fstream outFile;
00048 string inFileName;
00049 string outFileName;
00050 VorbisComments currentComment;
00051 unsigned long chainCount = 0;
00052 bool isOK = true;
00053
00054 bool renameCurrentFile() {
00055 cout<<"Renaming File..."<<endl;
00056 if ((currentComment.numUserComments() > 0)) {
00057 vector<SingleVorbisComment*> locArtists = currentComment.getCommentsByKey("artist");
00058 vector<SingleVorbisComment*> locTitles = currentComment.getCommentsByKey("title");
00059 string locArtist = "Unknown_Artist";
00060 string locTitle = "Unknown_Title";
00061
00062 cout<<"Basics done..."<<endl;
00063 if (locArtists.size() > 0) {
00064 locArtist = locArtists[0]->value();
00065 }
00066
00067 if (locTitles.size() > 0) {
00068 locTitle = locTitles[0]->value();
00069 }
00070
00071 string locPartialName = locArtist+"_"+locTitle;
00072
00073 size_t locSlashPos = outFileName.find_last_of('\\');
00074 if (locSlashPos == string::npos) {
00075 locSlashPos = -1;
00076 }
00077 size_t locDotPos = outFileName.find_last_of('.');
00078
00079 cout<<"Before substitution..."<<endl;
00080 string locNewName = outFileName.substr(0,locSlashPos + 1) + locPartialName + outFileName.substr(locDotPos);
00081 cout<<"After subst."<<endl;
00082 cout<<"Renaming "<<outFileName<<" to "<<locNewName<<endl;
00083 #ifdef WIN32
00084 MoveFile(outFileName.c_str(), locNewName.c_str());
00085 #else
00086 rename(outFileName.c_str(), locNewName.c_str());
00087 #endif
00088 }
00089
00090 return true;
00091
00092 }
00093 bool writePage(OggPage* inOggPage) {
00094
00095 unsigned char* pageBuff = inOggPage->createRawPageData();
00096
00097 outFile.write((char*)pageBuff, inOggPage->pageSize());
00098
00099 delete[] pageBuff;
00100
00101 return true;
00102 }
00103
00104 bool pageCB(OggPage* inOggPage, void* ) {
00105 bool retVal = false;
00106 if (inStream == false) {
00107
00108 if (inOggPage->header()->isBOS()) {
00109
00110 char* locNum = new char[32];
00111 sprintf(locNum, "%d", chainCount);
00112
00113 outFileName = inFileName + "__" + locNum + ".ogg";
00114 cout<<"New output file "<<outFileName<<endl;
00115
00116 delete locNum;
00117 locNum = NULL;
00118 outFile.open(outFileName.c_str(), ios_base::out | ios_base::binary);
00119 retVal = writePage(inOggPage);
00120 inStream = retVal;
00121 needComments = true;
00122
00123 } else {
00124
00125 cout << "BOS Page expected at "<<bytePos<<endl;
00126 retVal = false;
00127 }
00128
00129 } else {
00130
00131 if (inOggPage->header()->isEOS()) {
00132
00133 retVal = writePage(inOggPage);
00134 outFile.close();
00135 renameCurrentFile();
00136 cout<<"Closing file number "<<chainCount<<endl;
00137 chainCount++;
00138 inStream = false;
00139
00140 } else {
00141
00142 if (needComments) {
00143 currentComment.parseOggPacket(inOggPage->getPacket(0), 7);
00144 }
00145 retVal = writePage(inOggPage);
00146 }
00147 }
00148
00149 isOK = retVal;
00150 delete inOggPage;
00151 return retVal;
00152
00153 }
00154
00155 #ifdef WIN32
00156 int __cdecl _tmain(int argc, _TCHAR* argv[])
00157 #else
00158 int main(int argc, char * argv[])
00159 #endif
00160 {
00161
00162
00163
00164
00165
00166 bytePos = 0;
00167
00168 if (argc < 2) {
00169 cout<<"Usage : oggChainSplitter <filename>"<<endl;
00170 cout<<"Only splits vorbis chained streams... careful with command line... no error checks"<<endl;
00171 } else {
00172 cout << "Starting..."<<endl;
00173 OggDataBuffer testOggBuff;
00174
00175 testOggBuff.registerStaticCallback(&pageCB, NULL);
00176
00177 fstream testFile;
00178 inFileName = argv[1];
00179 testFile.open(argv[1], ios_base::in | ios_base::binary);
00180
00181 const unsigned short BUFF_SIZE = 8092;
00182 char* locBuff = new char[BUFF_SIZE];
00183 while ((!testFile.eof()) && (isOK)) {
00184 testFile.read(locBuff, BUFF_SIZE);
00185 unsigned long locBytesRead = testFile.gcount();
00186 testOggBuff.feed((const unsigned char*)locBuff, locBytesRead);
00187 }
00188
00189 delete[] locBuff;
00190 }
00191
00192 return 0;
00193 }
00194