From 81e186859c159a40b7e2ae6ba0ed62986548373d Mon Sep 17 00:00:00 2001 From: zqc <835569504@qq.com> Date: Thu, 5 Feb 2026 15:23:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8B=A5=E6=89=BE=E4=B8=8D=E5=88=B0=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E5=88=86=E7=89=87=EF=BC=8C=E5=88=99=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E8=BD=B4=E6=9A=82=E5=81=9C=EF=BC=8C=E8=80=8C=E4=B8=8D=E6=98=AF?= =?UTF-8?q?=E4=B8=80=E7=9B=B4=E5=90=91=E5=89=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hls_service_ws_kadian.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/hls_service_ws_kadian.py b/hls_service_ws_kadian.py index 4f9e4a9..9914b29 100644 --- a/hls_service_ws_kadian.py +++ b/hls_service_ws_kadian.py @@ -51,6 +51,8 @@ class HLSFrameProcessor(threading.Thread): self.last_process_time = None # 上次处理时间 self.last_frame_pts = None # 上一帧的PTS时间 self.pts_diff = 0 + + self.should_reset_time = False def find_segment_files(self): """查找TS分片文件,返回排序后的文件名列表""" @@ -146,7 +148,7 @@ class HLSFrameProcessor(threading.Thread): elif time_diff < -0.06: # 超前60ms,需要等待 (3帧) # 等待到正确的时间点 wait_time = -time_diff - if wait_time > 0.1: # 如果等待时间过长,重置时间基准 + if wait_time > 5: # 如果等待时间过长,重置时间基准 logger.info(f"[WARN] Too far ahead, resetting time base") self.start_time = current_time self.base_pts = frame_pts @@ -216,7 +218,9 @@ class HLSFrameProcessor(threading.Thread): else: # 下一个分片不存在,根据连续失败次数调整等待时间 consecutive_failures += 1 - + + self.should_reset_time = True + if consecutive_failures <= 10: sleep_time = 0.02 # 前10次失败,等待0.02秒 elif consecutive_failures <= 20: @@ -244,13 +248,19 @@ class HLSFrameProcessor(threading.Thread): container = av.open(segment_path) video_stream = container.streams.video[0] - # 如果是第一个分片,需要设置base_pts - if self.base_pts is None: + # 重置时间轴:使用新分片的第一帧PTS重新设置时间基准 + current_time = time.time() + + if self.base_pts is None or self.should_reset_time: # 读取第一帧来获取PTS for packet in container.demux(video_stream): for frame in packet.decode(): if frame.pts is not None: - self.base_pts = frame.pts * video_stream.time_base * 1000 + # 重置时间基准 + self.start_time = current_time + self.base_pts = float(frame.pts * video_stream.time_base) * 1000 + self.should_reset_time = False + logger.info(f"[INFO] Time axis reset: start_time={current_time}, base_pts={self.base_pts:.1f}") break break # 重置容器到开始