2008-09-12 01:39:59 +02:00
|
|
|
// Copyright (c) 2008 Karl Blomster
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
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;
|
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
|
|
|
|
2008-09-12 19:40:58 +02:00
|
|
|
FrameIndex *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
|
|
|
|
<< "-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;
|
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;
|
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
|
|
|
|
2008-09-15 21:27:12 +02:00
|
|
|
static int FFMS_CC UpdateProgress(int State, 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;
|
|
|
|
|
|
|
|
if (Percentage < 10)
|
|
|
|
cout << "\b\b";
|
|
|
|
else
|
|
|
|
cout << "\b\b\b";
|
|
|
|
|
|
|
|
cout << Percentage << "%";
|
|
|
|
|
|
|
|
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) {
|
2008-09-12 01:39:59 +02:00
|
|
|
std::cout << "Indexing, please wait... 0%";
|
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-09-12 18:06:57 +02:00
|
|
|
std::string Err = "Indexing error: ";
|
|
|
|
Err.append(FFMSErrMsg);
|
|
|
|
throw Err;
|
2008-09-12 01:39:59 +02:00
|
|
|
}
|
|
|
|
|
2008-09-12 18:15:49 +02:00
|
|
|
if (Progress != 100)
|
|
|
|
std::cout << "\b\b\b100%";
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "done." << std::endl;
|
|
|
|
} 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
|
|
|
|
2008-09-12 19:40:58 +02:00
|
|
|
FFMS_Init();
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2008-09-12 19:40:58 +02:00
|
|
|
try {
|
|
|
|
DoIndexing();
|
|
|
|
} catch (const char *Error) {
|
|
|
|
std::cout << Error << std::endl;
|
|
|
|
FFMS_DestroyFrameIndex(Index);
|
|
|
|
return 1;
|
2008-09-24 18:01:03 +02:00
|
|
|
} catch (std::string Error) {
|
|
|
|
std::cout << std::endl << Error << std::endl;
|
|
|
|
FFMS_DestroyFrameIndex(Index);
|
|
|
|
return 1;
|
2008-09-12 19:40:58 +02:00
|
|
|
} catch (...) {
|
|
|
|
std::cout << std::endl << "Unknown error" << std::endl;
|
|
|
|
FFMS_DestroyFrameIndex(Index);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-09-12 01:39:59 +02:00
|
|
|
|
2008-09-12 19:40:58 +02:00
|
|
|
FFMS_DestroyFrameIndex(Index);
|
2008-09-12 01:39:59 +02:00
|
|
|
return 0;
|
2008-09-12 19:40:58 +02:00
|
|
|
}
|