Skip to content

Commit

Permalink
ffmsindex: Add option to specify progress update interval
Browse files Browse the repository at this point in the history
Also add  a line break..

Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
  • Loading branch information
dwbuiten committed May 5, 2021
1 parent 05bb818 commit b8d17ec
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/index/ffmsindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#include <string>
#include <stdexcept>

extern "C" {
#include <libavutil/time.h>
}

namespace {

long long IndexMask = 0;
Expand All @@ -44,6 +48,7 @@ bool Overwrite = false;
bool PrintProgress = true;
bool WriteTC = false;
bool WriteKF = false;
int64_t ProgressInterval = 0;
std::string InputFile;
std::string CacheFile;

Expand All @@ -55,6 +60,11 @@ struct Error {
}
};

struct Progress {
int Percent;
int64_t Time;
};

void PrintUsage() {
std::cout <<
"FFmpegSource2 indexing app\n"
Expand All @@ -69,9 +79,16 @@ void PrintUsage() {
"-k Write keyframes for all video tracks to outputfile_track00.kf.txt (default: no)\n"
"-t N Set the audio indexing mask to N (-1 means index all tracks, 0 means index none, default: 0)\n"
"-s N Set audio decoding error handling. See the documentation for details. (default: 0)\n"
"-u N Set the progress update frequency in seconds. Set to 0 for every percent. (default: 0)\n"
<< std::endl;
}

int64_t parseSecondsToMicroseconds(const char *str) {
double val = std::strtod(str, nullptr);
int64_t ret = ((int64_t) val) * 1000000;
return ret;
}

void ParseCMDLine(int argc, const char *argv[]) {
for (int i = 1; i < argc; ++i) {
const char *Option = argv[i];
Expand All @@ -91,6 +108,8 @@ void ParseCMDLine(int argc, const char *argv[]) {
OPTION_ARG(IndexMask, "t", std::stoll);
} else if (!strcmp(Option, "-s")) {
OPTION_ARG(IgnoreErrors, "s", std::stoi);
} else if (!strcmp(Option, "-u")) {
OPTION_ARG(ProgressInterval, "u", parseSecondsToMicroseconds);
} else if (InputFile.empty()) {
InputFile = Option;
} else if (CacheFile.empty()) {
Expand Down Expand Up @@ -118,13 +137,15 @@ int FFMS_CC UpdateProgress(int64_t Current, int64_t Total, void *Private) {
int Percentage = int((double(Current) / double(Total)) * 100);

if (Private) {
int *LastPercentage = (int *)Private;
if (Percentage <= *LastPercentage)
Progress *LastProgress = (Progress *)Private;
int64_t CurTime = av_gettime();
if (Percentage <= LastProgress->Percent || (LastProgress->Time != 0 && (CurTime - LastProgress->Time) <= ProgressInterval))
return 0;
*LastPercentage = Percentage;
LastProgress->Percent = Percentage;
LastProgress->Time = CurTime;
}

std::cout << "Indexing, please wait... " << Percentage << "% \r" << std::flush;
std::cout << "Indexing, please wait... " << Percentage << "% \r" << std::endl << std::flush;

return 0;
}
Expand All @@ -144,7 +165,7 @@ void DoIndexing() {
E.Buffer = ErrorMsg;
E.BufferSize = sizeof(ErrorMsg);

int Progress = 0;
Progress ProgressTracker = { 0, av_gettime() };

FFMS_Index *Index = FFMS_ReadIndex(CacheFile.c_str(), &E);
if (Index) {
Expand All @@ -158,7 +179,7 @@ void DoIndexing() {
if (Indexer == nullptr)
throw Error("\nFailed to initialize indexing: ", E);

FFMS_SetProgressCallback(Indexer, UpdateProgress, &Progress);
FFMS_SetProgressCallback(Indexer, UpdateProgress, &ProgressTracker);

// Treat -1 as meaning track numbers above sizeof(long long) * 8 too, dumping implies indexing
if (IndexMask == -1)
Expand Down

0 comments on commit b8d17ec

Please sign in to comment.