@@ -29,41 +29,36 @@ class Transcoder {
2929 Transcoder (ProcessParameter *process_parameter,
3030 EncodeParameter *encode_parameter)
3131 : process_parameter(process_parameter), encode_parameter(encode_parameter) {
32+ first_frame_time = std::chrono::system_clock::time_point{};
3233 last_ui_update = std::chrono::system_clock::now ();
3334 }
3435
3536 virtual ~Transcoder () = default ;
3637
3738 virtual bool transcode (std::string input_path, std::string output_path) = 0;
3839
39- double compute_smooth_duration (double new_duration) {
40- if (new_duration >= min_duration_threshold) {
41- duration_history.push_back (new_duration);
42- if (duration_history.size () > max_history_size) {
43- duration_history.erase (duration_history.begin ());
44- }
40+ void send_process_parameter (int64_t frame_number, int64_t frame_total_number) {
41+ if (first_frame_time == std::chrono::system_clock::time_point{}) {
42+ first_frame_time = std::chrono::system_clock::now ();
4543 }
46- return duration_history.empty ()
47- ? 0.0
48- : std::accumulate (duration_history.begin (),
49- duration_history.end (), 0.0 ) /
50- duration_history.size ();
51- }
5244
53- void send_process_parameter (int64_t frame_number, int64_t frame_total_number) {
54- process_number = frame_number * 100 / frame_total_number;
45+ double fraction = 0.0 ;
46+ if (frame_total_number > 0 ) {
47+ fraction = static_cast <double >(frame_number) / static_cast <double >(frame_total_number);
48+ if (fraction > 1.0 ) fraction = 1.0 ; // clamp just in case
49+ if (fraction < 0.0 ) fraction = 0.0 ;
50+ }
51+ process_number = static_cast <int >(fraction * 100.0 );
5552
56- static auto last_encoder_call_time = std::chrono::system_clock::now ();
5753 auto now = std::chrono::system_clock::now ();
5854
59- auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
60- now - last_encoder_call_time )
55+ auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
56+ now - first_frame_time )
6157 .count ();
62- last_encoder_call_time = now;
6358
64- double smooth_duration = compute_smooth_duration (duration);
6559 if (frame_number > 0 && frame_total_number > 0 ) {
66- remain_time = smooth_duration * (100 - process_number) / 1000 ;
60+ double elapsed_ms_d = static_cast <double >(elapsed_ms);
61+ remain_seconds = (elapsed_ms / fraction - elapsed_ms_d) / 1000.0 ;
6762 }
6863
6964 // Only update UI if enough time has passed (100ms)
@@ -74,15 +69,14 @@ class Transcoder {
7469 if (time_since_last_ui_update >= 100 ) {
7570 process_parameter->set_process_number (process_number);
7671 if (frame_number > 0 && frame_total_number > 0 ) {
77- process_parameter->set_time_required (remain_time );
72+ process_parameter->set_time_required (remain_seconds );
7873 }
7974 last_ui_update = now;
8075 }
8176
8277 std::cout << " Process Number (percentage): " << process_number << " %\t "
83- << " Current duration (milliseconds): " << duration << " \t "
84- << " Smoothed Duration: " << smooth_duration << " ms\t "
85- << " Estimated Rest Time (seconds): " << remain_time
78+ << " Elapsed Time (milliseconds): " << elapsed_ms << " \t "
79+ << " Estimated Rest Time (seconds): " << remain_seconds
8680 << std::endl;
8781 }
8882
@@ -92,17 +86,12 @@ class Transcoder {
9286 int64_t frame_number = 0 ;
9387 int64_t frame_total_number = 0 ;
9488 int process_number = 0 ;
95- double remain_time = 0 ;
89+ double remain_seconds = 0 ;
9690
91+ std::chrono::system_clock::time_point first_frame_time;
9792 std::chrono::system_clock::time_point
9893 last_ui_update; // Track last UI update time
99- std::vector<double >
100- duration_history; // Store recent durations for averaging
10194
102- static constexpr size_t max_history_size =
103- 20 ; // Limit for the number of durations tracked
104- static constexpr double min_duration_threshold =
105- 10.0 ; // Ignore durations < 10 ms
10695};
10796
10897#endif
0 commit comments