Skip to content

Commit 2cf15c3

Browse files
committed
Add progress indicator
Based on pull request johmathe#3 by Olivier Aubert (https://github.com/oaubert). Add library {fmt} http://fmtlib.net/ for sane string handling. Will be used in upcoming commits, too.
1 parent 54df5fb commit 2cf15c3

5 files changed

Lines changed: 4819 additions & 6 deletions

File tree

src/commandline.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class film;
2323

2424
// Example
2525
// Put the file you want to analyse in the same directory of shotdetect (it not
26-
// mandatory but itÕs easier with the command line)
26+
// mandatory but it's easier with the command line)
2727

2828
// shotdetect -i myvideo.avi -o output_dir -s 75 -w -v -f -l -m -r
2929

@@ -51,6 +51,7 @@ void show_help(char **argv) {
5151
"-n : commandline mode (disable GUI)\n"
5252
"-s threshold : threshold (Default=%d)\n"
5353
"-i file : input file path\n"
54+
"-p : output progress report information\n"
5455
"-o path : output path\n"
5556
"-y year : set the year\n"
5657
"-a id : id of the movie\n"
@@ -86,7 +87,7 @@ int main(int argc, char **argv) {
8687
f.set_draw_yuv_graph(false); // YUV graph is still disabled, until it works.
8788

8889
for (;;) {
89-
int c = getopt(argc, argv, "?ht:y:i:o:a:x:s:flwvmrc");
90+
int c = getopt(argc, argv, "?ht:y:i:o:a:x:s:flpwvmrc");
9091

9192
if (c < 0) {
9293
break;
@@ -117,6 +118,10 @@ int main(int argc, char **argv) {
117118
/* Generate thumbnails? */
118119
case 'm':
119120
f.set_thumb(true);
121+
break;
122+
123+
case 'p':
124+
f.set_progress(true);
120125
break;
121126

122127
/* Generate XML with video metadata? */

src/film.cc

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,21 @@ extern "C" {
3232
#include "src/film.h"
3333
#include "src/graph.h"
3434

35+
#include "src/format.h"
36+
3537
#define DEBUG
3638

3739
int film::idfilm = 0;
3840

39-
void film::do_stats(int frame_number) {
41+
void film::log_progress(string type, int position, int total)
42+
{
43+
if (this->get_progress()) {
44+
cerr << type << " " << position << " " << total << endl;
45+
}
46+
}
47+
48+
void film::do_stats(int frame_number)
49+
{
4050
double perctmp = percent;
4151
struct timeval time_now;
4252
struct timezone timezone;
@@ -180,9 +190,7 @@ void film::CompareFrame(AVFrame *pFrame, AVFrame *pFramePrev) {
180190
s.msbegin = int((frame_number * 1000) / fps);
181191
s.myid = shots.back().myid + 1;
182192

183-
#ifdef DEBUG
184-
cerr << "Shot log :: " << s.msbegin << endl;
185-
#endif
193+
this->log_progress("shot", s.msbegin, duration.mstotal);
186194

187195
/*
188196
* Convert to ms
@@ -414,6 +422,10 @@ int film::process() {
414422

415423
checknumber = (samplerate * samplearg) / 1000;
416424

425+
const int progress_frame_interval = 100;
426+
timespec last_progress_log_cputime;
427+
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &last_progress_log_cputime);
428+
417429
/*
418430
* Main loop to control the movie processing flow
419431
*/
@@ -424,6 +436,26 @@ int film::process() {
424436
if (frameFinished) {
425437
frame_number = pCodecCtx->frame_number; // Current frame number
426438

439+
// Report progress information every N frames
440+
if (frame_number % progress_frame_interval == 0) {
441+
442+
timespec current_progress_log_cputime;
443+
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &current_progress_log_cputime);
444+
const double delta_s = [](auto start, auto end){
445+
return (double)(end.tv_sec - start.tv_sec) +
446+
1.0e-9*(double)(end.tv_nsec - start.tv_nsec);
447+
}(last_progress_log_cputime, current_progress_log_cputime);
448+
last_progress_log_cputime = current_progress_log_cputime;
449+
const double computation_fps = 1/(delta_s / progress_frame_interval);
450+
451+
// this->log_progress("progress", int((frame_number * 1000) / fps), duration.mstotal);
452+
const double current_secs = frame_number / fps;
453+
const double duration_secs = duration.mstotal / 1000.0;
454+
const double percent = current_secs / duration_secs * 100;
455+
this->shotlog(fmt::format("Progress: frame={:6}, time={:.1f}s, duration={:.1f}s, percent={:.3f}%%, fps={:.1f}",
456+
frame_number, current_secs, duration_secs, percent, computation_fps));
457+
}
458+
427459
// Convert the image into YUV444
428460
if (!img_ctx) {
429461
img_ctx =

src/film.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ class film {
186186
double percent;
187187
/* Showing state started */
188188
bool show_started;
189+
/* Output progress information */
190+
bool progress;
189191
/* Draw graphs: */
190192
bool draw_rgb_graph;
191193
bool draw_hsv_graph;
@@ -198,6 +200,7 @@ class film {
198200
void process_audio();
199201
void shotlog(string message);
200202
void create_main_dir(void);
203+
void log_progress(string message, int position, int total);
201204

202205
/* Constructor */
203206
film();
@@ -222,6 +225,12 @@ class film {
222225
inline void set_year(int year) { this->year = year; };
223226
inline void set_alphaid(string alphaid) { this->alphaid = alphaid; };
224227
inline void set_title(string title) { this->title = title; };
228+
inline void set_progress(bool progress) {
229+
this->progress = progress;
230+
};
231+
inline bool get_progress(void) {
232+
return this->progress;
233+
};
225234

226235
inline void set_draw_rgb_graph(bool val) { this->draw_rgb_graph = val; };
227236
inline void set_draw_hsv_graph(bool val) { this->draw_hsv_graph = val; };

0 commit comments

Comments
 (0)