2009-04-06 21:14:55 +02:00
|
|
|
// Copyright (c) 2008-2009 Karl Blomster
|
2008-09-12 01:39:59 +02:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
2009-05-12 22:50:40 +02:00
|
|
|
extern "C" {
|
|
|
|
#include <libavutil/log.h>
|
|
|
|
}
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2008-09-12 19:40:58 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
2008-09-15 21:27:12 +02:00
|
|
|
#include <stdlib.h>
|
2008-09-12 19:40:58 +02:00
|
|
|
#include "ffms.h"
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2008-09-12 19:40:58 +02:00
|
|
|
int TrackMask;
|
2008-09-24 18:01:03 +02:00
|
|
|
int DumpMask;
|
2008-09-12 19:40:58 +02:00
|
|
|
bool Overwrite;
|
2008-09-24 18:01:03 +02:00
|
|
|
bool IgnoreErrors;
|
2009-04-07 03:12:12 +02:00
|
|
|
bool Verbose;
|
2008-09-12 19:40:58 +02:00
|
|
|
std::string InputFile;
|
|
|
|
std::string CacheFile;
|
|
|
|
std::string AudioFile;
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
FFIndex *Index;
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2008-09-12 18:06:57 +02:00
|
|
|
|
2008-09-12 19:40:58 +02:00
|
|
|
void PrintUsage () {
|
|
|
|
using namespace std;
|
|
|
|
cout << "FFmpegSource2 indexing app" << endl
|
|
|
|
<< "Usage: ffmsindex [options] inputfile [outputfile]" << endl
|
|
|
|
<< "If no output filename is specified, inputfile.ffindex will be used." << endl << endl
|
|
|
|
<< "Options:" << endl
|
2008-09-24 18:01:03 +02:00
|
|
|
<< "-f Force overwriting of existing index file, if any (default: no)" << endl
|
|
|
|
<< "-s Silently skip indexing of audio tracks that cannot be read (default: no)" << endl
|
2009-04-07 03:12:12 +02:00
|
|
|
<< "-v Be verbose; i.e. print FFmpeg warnings/diagnostics, if any (default: no)" << endl
|
2008-09-24 18:01:03 +02:00
|
|
|
<< "-t N Set the audio indexing mask to N (-1 means index all tracks, 0 means index none, default: 0)" << endl
|
|
|
|
<< "-d N Set the audio decoding mask to N (mask syntax same as -t, default: 0)" << endl
|
2008-09-12 19:40:58 +02:00
|
|
|
<< "-a NAME Set the audio output base filename to NAME (default: input filename)";
|
2008-09-12 01:39:59 +02:00
|
|
|
}
|
|
|
|
|
2008-09-24 18:01:03 +02:00
|
|
|
|
2008-09-12 18:50:21 +02:00
|
|
|
void ParseCMDLine (int argc, char *argv[]) {
|
2008-09-12 01:39:59 +02:00
|
|
|
if (argc <= 1) {
|
|
|
|
PrintUsage();
|
2008-09-12 18:06:57 +02:00
|
|
|
throw "";
|
2008-09-12 01:39:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// defaults
|
2008-09-12 18:06:57 +02:00
|
|
|
InputFile = "";
|
|
|
|
CacheFile = "";
|
|
|
|
AudioFile = "";
|
|
|
|
TrackMask = 0;
|
2008-09-24 18:01:03 +02:00
|
|
|
DumpMask = 0;
|
2008-09-12 18:06:57 +02:00
|
|
|
Overwrite = false;
|
2008-09-24 18:01:03 +02:00
|
|
|
IgnoreErrors = false;
|
2009-04-07 03:12:12 +02:00
|
|
|
Verbose = false;
|
2008-09-12 01:39:59 +02:00
|
|
|
|
|
|
|
// argv[0] = name of program
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
while (i < argc) {
|
|
|
|
std::string Option = argv[i];
|
|
|
|
std::string OptionArg = "";
|
|
|
|
if (i+1 < argc)
|
|
|
|
OptionArg = argv[i+1];
|
|
|
|
|
|
|
|
if (!Option.compare("-f")) {
|
2008-09-12 18:06:57 +02:00
|
|
|
Overwrite = true;
|
2008-09-24 18:01:03 +02:00
|
|
|
} else if (!Option.compare("-s")) {
|
|
|
|
IgnoreErrors = true;
|
2009-04-07 03:12:12 +02:00
|
|
|
} else if (!Option.compare("-v")) {
|
|
|
|
Verbose = true;
|
2008-09-12 01:39:59 +02:00
|
|
|
} else if (!Option.compare("-t")) {
|
2008-09-12 18:06:57 +02:00
|
|
|
TrackMask = atoi(OptionArg.c_str());
|
2008-09-12 01:39:59 +02:00
|
|
|
i++;
|
2008-09-24 18:01:03 +02:00
|
|
|
} else if (!Option.compare("-d")) {
|
|
|
|
DumpMask = atoi(OptionArg.c_str());
|
|
|
|
i++;
|
2008-09-12 01:39:59 +02:00
|
|
|
} else if (!Option.compare("-a")) {
|
2008-09-12 18:06:57 +02:00
|
|
|
AudioFile = OptionArg;
|
2008-09-12 01:39:59 +02:00
|
|
|
i++;
|
2008-09-12 18:06:57 +02:00
|
|
|
} else if (InputFile.empty()) {
|
|
|
|
InputFile = argv[i];
|
|
|
|
} else if (CacheFile.empty()) {
|
|
|
|
CacheFile = argv[i];
|
2008-09-12 01:39:59 +02:00
|
|
|
} else {
|
|
|
|
std::cout << "Warning: ignoring unknown option " << argv[i] << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2008-09-12 18:06:57 +02:00
|
|
|
if (InputFile.empty()) {
|
|
|
|
throw "Error: no input file specified";
|
2008-09-12 01:39:59 +02:00
|
|
|
}
|
2008-09-12 18:06:57 +02:00
|
|
|
if (CacheFile.empty()) {
|
|
|
|
CacheFile = InputFile;
|
|
|
|
CacheFile.append(".ffindex");
|
2008-09-12 01:39:59 +02:00
|
|
|
}
|
2008-09-12 18:06:57 +02:00
|
|
|
if (AudioFile.empty()) {
|
|
|
|
AudioFile = InputFile;
|
2008-09-12 01:39:59 +02:00
|
|
|
}
|
2008-09-12 18:06:57 +02:00
|
|
|
}
|
|
|
|
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2009-05-18 00:02:43 +02:00
|
|
|
static int FFMS_CC UpdateProgress(int64_t Current, int64_t Total, void *Private) {
|
2008-09-12 01:39:59 +02:00
|
|
|
using namespace std;
|
2008-09-12 19:40:58 +02:00
|
|
|
int *LastPercentage = (int *)Private;
|
|
|
|
int Percentage = int((double(Current)/double(Total)) * 100);
|
|
|
|
|
|
|
|
if (Percentage <= *LastPercentage)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*LastPercentage = Percentage;
|
|
|
|
|
2008-11-20 20:31:22 +01:00
|
|
|
/*if (Percentage < 10)
|
2008-09-12 19:40:58 +02:00
|
|
|
cout << "\b\b";
|
|
|
|
else
|
2008-11-20 20:31:22 +01:00
|
|
|
cout << "\b\b\b"; */
|
2008-09-12 19:40:58 +02:00
|
|
|
|
2009-03-31 00:09:33 +02:00
|
|
|
cout << "Indexing, please wait... " << Percentage << "% \r" << flush;
|
2008-09-12 19:40:58 +02:00
|
|
|
|
|
|
|
return 0;
|
2008-09-12 01:39:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-12 18:50:21 +02:00
|
|
|
void DoIndexing () {
|
2008-09-12 01:39:59 +02:00
|
|
|
char FFMSErrMsg[1024];
|
|
|
|
int MsgSize = sizeof(FFMSErrMsg);
|
|
|
|
int Progress = 0;
|
|
|
|
|
2008-09-12 18:06:57 +02:00
|
|
|
Index = FFMS_ReadIndex(CacheFile.c_str(), FFMSErrMsg, MsgSize);
|
|
|
|
if (Overwrite || Index == NULL) {
|
2009-03-31 00:09:33 +02:00
|
|
|
std::cout << "Indexing, please wait... 0% \r" << std::flush;
|
2008-09-24 18:01:03 +02:00
|
|
|
Index = FFMS_MakeIndex(InputFile.c_str(), TrackMask, DumpMask, AudioFile.c_str(), IgnoreErrors, UpdateProgress, &Progress, FFMSErrMsg, MsgSize);
|
2008-09-12 01:39:59 +02:00
|
|
|
if (Index == NULL) {
|
2008-11-20 20:31:22 +01:00
|
|
|
std::string Err = "\nIndexing error: ";
|
2008-09-12 18:06:57 +02:00
|
|
|
Err.append(FFMSErrMsg);
|
|
|
|
throw Err;
|
2008-09-12 01:39:59 +02:00
|
|
|
}
|
|
|
|
|
2008-09-12 18:15:49 +02:00
|
|
|
if (Progress != 100)
|
2009-03-31 00:09:33 +02:00
|
|
|
std::cout << "Indexing, please wait... 100% \r" << std::flush;
|
2008-09-12 18:15:49 +02:00
|
|
|
|
|
|
|
std::cout << std::endl << "Writing index... ";
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2008-09-12 18:06:57 +02:00
|
|
|
if (FFMS_WriteIndex(CacheFile.c_str(), Index, FFMSErrMsg, MsgSize)) {
|
|
|
|
std::string Err = "Error writing index: ";
|
|
|
|
Err.append(FFMSErrMsg);
|
|
|
|
throw Err;
|
2008-09-12 01:39:59 +02:00
|
|
|
}
|
|
|
|
|
2009-03-31 00:09:33 +02:00
|
|
|
std::cout << "done." << std::endl << std::flush;
|
2008-09-12 01:39:59 +02:00
|
|
|
} else {
|
2008-09-12 18:06:57 +02:00
|
|
|
throw "Error: index file already exists, use -f if you are sure you want to overwrite it.";
|
2008-09-12 01:39:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-12 18:50:21 +02:00
|
|
|
|
2008-09-12 19:40:58 +02:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
try {
|
|
|
|
ParseCMDLine(argc, argv);
|
|
|
|
} catch (const char *Error) {
|
|
|
|
std::cout << std::endl << Error << std::endl;
|
|
|
|
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;
|
|
|
|
}
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2009-05-25 16:05:04 +02:00
|
|
|
FFMS_Init();
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2009-05-12 22:50:40 +02:00
|
|
|
if (Verbose)
|
|
|
|
FFMS_SetLogLevel(AV_LOG_INFO);
|
2009-04-07 03:12:12 +02:00
|
|
|
|
2008-09-12 19:40:58 +02:00
|
|
|
try {
|
|
|
|
DoIndexing();
|
|
|
|
} catch (const char *Error) {
|
|
|
|
std::cout << Error << std::endl;
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-12 19:40:58 +02:00
|
|
|
return 1;
|
2008-09-24 18:01:03 +02:00
|
|
|
} catch (std::string Error) {
|
|
|
|
std::cout << std::endl << Error << std::endl;
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-24 18:01:03 +02:00
|
|
|
return 1;
|
2008-09-12 19:40:58 +02:00
|
|
|
} catch (...) {
|
|
|
|
std::cout << std::endl << "Unknown error" << std::endl;
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-12 19:40:58 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
FFMS_DestroyFFIndex(Index);
|
2008-09-12 01:39:59 +02:00
|
|
|
return 0;
|
2008-09-12 19:40:58 +02:00
|
|
|
}
|