redesign of the ffms2 indexing app

Originally committed to SVN as r2345.
This commit is contained in:
Karl Blomster 2008-09-12 16:06:57 +00:00
parent 28fa817737
commit 0838d3afe4
2 changed files with 72 additions and 60 deletions

View file

@ -22,32 +22,48 @@
#include "ffmsindex.h" #include "ffmsindex.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
IndexingOptions *Options = FFMSIndexApp::ParseCMDLine(argc, argv); FFMSIndexApp *App;
if (!Options)
try {
App = new FFMSIndexApp(argc, argv);
} catch (const char *Error) {
std::cout << std::endl << Error << std::endl;
return 1; return 1;
} catch (std::string Error) {
std::cout << std::endl << Error << std::endl;
return 1;
} catch (...) {
std::cout << std::endl << "Unknown error" << std::endl;
return 1;
}
FFMS_Init(); FFMS_Init();
if (FFMSIndexApp::DoIndexing(Options)) try {
App->DoIndexing();
} catch (const char *Error) {
std::cout << Error << std::endl;
delete App;
return 1; return 1;
else }
delete App;
return 0; return 0;
} }
IndexingOptions *FFMSIndexApp::ParseCMDLine (int argc, char *argv[]) { FFMSIndexApp::FFMSIndexApp (int argc, char *argv[]) {
if (argc <= 1) { if (argc <= 1) {
PrintUsage(); PrintUsage();
return NULL; throw "";
} }
// defaults // defaults
IndexingOptions *Options = new IndexingOptions(); InputFile = "";
Options->InputFile = ""; CacheFile = "";
Options->CacheFile = ""; AudioFile = "";
Options->AudioFile = ""; TrackMask = 0;
Options->TrackMask = 0; Overwrite = false;
Options->Overwrite = false;
// argv[0] = name of program // argv[0] = name of program
int i = 1; int i = 1;
@ -59,17 +75,17 @@ IndexingOptions *FFMSIndexApp::ParseCMDLine (int argc, char *argv[]) {
OptionArg = argv[i+1]; OptionArg = argv[i+1];
if (!Option.compare("-f")) { if (!Option.compare("-f")) {
Options->Overwrite = true; Overwrite = true;
} else if (!Option.compare("-t")) { } else if (!Option.compare("-t")) {
Options->TrackMask = atoi(OptionArg.c_str()); TrackMask = atoi(OptionArg.c_str());
i++; i++;
} else if (!Option.compare("-a")) { } else if (!Option.compare("-a")) {
Options->AudioFile = OptionArg; AudioFile = OptionArg;
i++; i++;
} else if (!Options->InputFile.compare("")) { } else if (InputFile.empty()) {
Options->InputFile = argv[i]; InputFile = argv[i];
} else if (!Options->CacheFile.compare("")) { } else if (CacheFile.empty()) {
Options->CacheFile = argv[i]; CacheFile = argv[i];
} else { } else {
std::cout << "Warning: ignoring unknown option " << argv[i] << std::endl; std::cout << "Warning: ignoring unknown option " << argv[i] << std::endl;
} }
@ -77,19 +93,21 @@ IndexingOptions *FFMSIndexApp::ParseCMDLine (int argc, char *argv[]) {
i++; i++;
} }
if (!Options->InputFile.compare("")) { if (InputFile.empty()) {
std::cout << "Error: no input file specified" << std::endl; throw "Error: no input file specified";
return NULL;
} }
if (!Options->CacheFile.compare("")) { if (CacheFile.empty()) {
Options->CacheFile = Options->InputFile; CacheFile = InputFile;
Options->CacheFile.append(".ffindex"); CacheFile.append(".ffindex");
} }
if (!Options->AudioFile.compare("")) { if (AudioFile.empty()) {
Options->AudioFile = Options->InputFile; AudioFile = InputFile;
} }
}
FFMSIndexApp::~FFMSIndexApp () {
FFMS_DestroyFrameIndex(Index);
return Options;
} }
@ -101,45 +119,37 @@ void FFMSIndexApp::PrintUsage () {
<< "Options:" << endl << "Options:" << endl
<< "-f Overwrite existing index file if it exists (default: no)" << endl << "-f Overwrite existing index file if it exists (default: no)" << endl
<< "-t N Set the audio trackmask to N (-1 means decode all tracks, 0 means decode none; default: 0)" << endl << "-t N Set the audio trackmask to N (-1 means decode all tracks, 0 means decode none; default: 0)" << endl
<< "-a NAME Set the audio output base filename to NAME (default: input filename)" << endl; << "-a NAME Set the audio output base filename to NAME (default: input filename)" << endl << endl;
} }
int FFMSIndexApp::DoIndexing (IndexingOptions *Options) { void FFMSIndexApp::DoIndexing () {
FrameIndex *Index;
char FFMSErrMsg[1024]; char FFMSErrMsg[1024];
int MsgSize = sizeof(FFMSErrMsg); int MsgSize = sizeof(FFMSErrMsg);
int TrackMask = Options->TrackMask;
std::string InputFileName = Options->InputFile;
std::string CacheFileName = Options->CacheFile;
std::string AudioFileName = Options->AudioFile;
int Progress = 0; int Progress = 0;
Index = FFMS_ReadIndex(CacheFileName.c_str(), FFMSErrMsg, MsgSize); Index = FFMS_ReadIndex(CacheFile.c_str(), FFMSErrMsg, MsgSize);
if (Options->Overwrite || Index == NULL) { if (Overwrite || Index == NULL) {
std::cout << "Indexing, please wait... 0%"; std::cout << "Indexing, please wait... 0%";
Index = FFMS_MakeIndex(InputFileName.c_str(), TrackMask, AudioFileName.c_str(), FFMSIndexApp::UpdateProgress, &Progress, FFMSErrMsg, MsgSize); Index = FFMS_MakeIndex(InputFile.c_str(), TrackMask, AudioFile.c_str(), FFMSIndexApp::UpdateProgress, &Progress, FFMSErrMsg, MsgSize);
if (Index == NULL) { if (Index == NULL) {
std::cout << std::endl << "Indexing error: " << FFMSErrMsg << std::endl; std::string Err = "Indexing error: ";
return 1; Err.append(FFMSErrMsg);
throw Err;
} }
std::cout << "\b\b\b100%" << std::endl << "Writing index... "; std::cout << "\b\b\b100%" << std::endl << "Writing index... ";
if (FFMS_WriteIndex(CacheFileName.c_str(), Index, FFMSErrMsg, MsgSize)) { if (FFMS_WriteIndex(CacheFile.c_str(), Index, FFMSErrMsg, MsgSize)) {
std::cout << std::endl << "Error writing index: " << FFMSErrMsg << std::endl; std::string Err = "Error writing index: ";
return 1; Err.append(FFMSErrMsg);
throw Err;
} }
FFMS_DestroyFrameIndex(Index);
std::cout << "done." << std::endl; std::cout << "done." << std::endl;
} else { } else {
std::cout << "Error: index file already exists, use -f if you are sure you want to overwrite it." << std::endl; throw "Error: index file already exists, use -f if you are sure you want to overwrite it.";
return 1;
} }
return 0;
} }
int FFMSIndexApp::UpdateProgress(int State, int64_t Current, int64_t Total, void *Private) { int FFMSIndexApp::UpdateProgress(int State, int64_t Current, int64_t Total, void *Private) {

View file

@ -24,20 +24,22 @@
#include <string> #include <string>
#include "ffms.h" #include "ffms.h"
struct IndexingOptions {
int TrackMask;
std::string InputFile;
std::string CacheFile;
std::string AudioFile;
bool Overwrite;
};
class FFMSIndexApp { class FFMSIndexApp {
private: private:
int TrackMask;
bool Overwrite;
std::string InputFile;
std::string CacheFile;
std::string AudioFile;
FrameIndex *Index;
void PrintUsage();
public: public:
static IndexingOptions *ParseCMDLine(int argc, char *argv[]); FFMSIndexApp(int argc, char *argv[]);
static void PrintUsage(); ~FFMSIndexApp();
static int DoIndexing(IndexingOptions *Options); void DoIndexing();
static int __stdcall UpdateProgress(int State, int64_t Current, int64_t Total, void *Private); static int __stdcall UpdateProgress(int State, int64_t Current, int64_t Total, void *Private);
}; };