From ce6c2dcbaff65ef5f9f817f3a8c50c8a1a8094eb Mon Sep 17 00:00:00 2001
From: Mole Shang <135e2@135e2.dev>
Date: Sat, 2 Mar 2024 15:52:21 +0800
Subject: utils/ffmpeg: fix remux progress

---
 src/utils/ffmpeg.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

(limited to 'src/utils')

diff --git a/src/utils/ffmpeg.c b/src/utils/ffmpeg.c
index db11f87..9085caf 100644
--- a/src/utils/ffmpeg.c
+++ b/src/utils/ffmpeg.c
@@ -70,8 +70,6 @@ int merge_av(const char *videofn, const char *audiofn, const char *outfn) {
     goto end;
   }
 
-  stat_g->total = input1_format_context->duration / AV_TIME_BASE;
-
   avformat_alloc_output_context2(&output_format_context, NULL, NULL, outfn);
   if (!output_format_context) {
     append_log("Could not create output context\n");
@@ -153,6 +151,9 @@ int merge_av(const char *videofn, const char *audiofn, const char *outfn) {
     LOG("ffmpeg", "Error occurred when opening output file\n");
     goto end;
   }
+
+  stat_g->cur = 0ull;
+  stat_g->total = input1_format_context->duration / AV_TIME_BASE;
   while (1) {
     AVStream *in_stream, *out_stream;
     ret = av_read_frame(input1_format_context, &packet);
@@ -178,9 +179,8 @@ int merge_av(const char *videofn, const char *audiofn, const char *outfn) {
                                    out_stream->time_base);
     packet.pos = -1;
     // log_packet(output_format_context, &packet, "out");
-    stat_g->cur = (stat_g->cur <= stat_g->total)
-                      ? av_q2d(in_stream->time_base) * packet.pts
-                      : stat_g->total;
+    if (stat_g->cur <= stat_g->total)
+      stat_g->cur = av_q2d(in_stream->time_base) * packet.pts;
 
     ret = av_interleaved_write_frame(output_format_context, &packet);
     if (ret < 0) {
@@ -189,6 +189,9 @@ int merge_av(const char *videofn, const char *audiofn, const char *outfn) {
     }
     av_packet_unref(&packet);
   }
+
+  stat_g->cur = 0ull;
+  stat_g->total = input2_format_context->duration / AV_TIME_BASE;
   while (1) {
     AVStream *in_stream, *out_stream;
     ret = av_read_frame(input2_format_context, &packet);
@@ -216,9 +219,8 @@ int merge_av(const char *videofn, const char *audiofn, const char *outfn) {
                                    out_stream->time_base);
     packet.pos = -1;
     // log_packet(output_format_context, &packet, "out");
-    stat_g->cur = (stat_g->cur <= stat_g->total)
-                      ? av_q2d(in_stream->time_base) * packet.pts
-                      : stat_g->total;
+    if (stat_g->cur <= stat_g->total)
+      stat_g->cur = av_q2d(in_stream->time_base) * packet.pts;
     ret = av_interleaved_write_frame(output_format_context, &packet);
     if (ret < 0) {
       LOG("ffmpeg", "Error muxing packet\n");
@@ -282,6 +284,8 @@ int remux(const char *in_filename, const char *out_filename) {
 
   DEBUG_PRINT("duration: %.2Lf\n",
               (long double)ifmt_ctx->duration / AV_TIME_BASE);
+
+  stat_g->cur = 0ull;
   stat_g->total = ifmt_ctx->duration / AV_TIME_BASE;
 
   av_dump_format(ifmt_ctx, 0, in_filename, 0);
@@ -368,9 +372,8 @@ int remux(const char *in_filename, const char *out_filename) {
     av_packet_rescale_ts(pkt, in_stream->time_base, out_stream->time_base);
     pkt->pos = -1;
     // log_packet(ofmt_ctx, pkt, "out");
-    stat_g->cur = (stat_g->cur <= stat_g->total)
-                      ? av_q2d(in_stream->time_base) * pkt->pts
-                      : stat_g->total;
+    if (stat_g->cur <= stat_g->total)
+      stat_g->cur = av_q2d(in_stream->time_base) * pkt->pts;
     ret = av_interleaved_write_frame(ofmt_ctx, pkt);
     /* pkt is now blank (av_interleaved_write_frame() takes ownership of
      * its contents and resets pkt), so that no unreferencing is necessary.
-- 
cgit v1.2.3