Skip to content

Commit fec4186

Browse files
JackLau1222Jimmy385898
authored andcommitted
transcoder: fix abnormal progress bar display
Compute the progress based on current frame number or pts and totoal_frame_numbder or duration rather than base on every frame encode duration Signed-off-by: Jack Lau <[email protected]>
1 parent cc47af3 commit fec4186

File tree

2 files changed

+24
-33
lines changed

2 files changed

+24
-33
lines changed

src/transcoder/include/transcoder.h

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/transcoder/src/transcoder_ffmpeg.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C" {
2323
TranscoderFFmpeg::TranscoderFFmpeg(ProcessParameter *process_parameter,
2424
EncodeParameter *encode_parameter)
2525
: Transcoder(process_parameter, encode_parameter) {
26+
frame_number = 0;
2627
frame_total_number = 0;
2728
total_duration = 0;
2829
current_duration = 0;
@@ -546,8 +547,6 @@ bool TranscoderFFmpeg::transcode(std::string input_path,
546547

547548
int TranscoderFFmpeg::open_media() {
548549
int ret = -1;
549-
/* set the frame_number to zero to avoid some bugs */
550-
frame_number = 0;
551550
// open the multimedia file
552551
if ((ret = avformat_open_input(&decoder->fmtCtx, decoder->filename, NULL,
553552
NULL)) < 0) {
@@ -574,9 +573,12 @@ void TranscoderFFmpeg::adjust_frame_pts_to_encoder_timebase(AVFrame *frame, int
574573
AVFilterContext *filter = filters_ctx[index].buffersink_ctx;
575574
AVRational filter_tb = av_buffersink_get_time_base(filter);
576575
AVRational av_tb = {1, AV_TIME_BASE};
576+
int64_t td;
577577
frame->pts =
578578
av_rescale_q(frame->pts, filter_tb, tb) -
579579
av_rescale_q(start_time, av_tb, tb);
580+
td = av_rescale_q(total_duration, av_tb, tb);
581+
send_process_parameter(frame->pts, td);
580582
return;
581583
}
582584

0 commit comments

Comments
 (0)