若找不到下一分片,则时间轴暂停,而不是一直向前

This commit is contained in:
zqc
2026-02-05 15:23:57 +08:00
parent a282542772
commit 81e186859c

View File

@@ -52,6 +52,8 @@ class HLSFrameProcessor(threading.Thread):
self.last_frame_pts = None # 上一帧的PTS时间
self.pts_diff = 0
self.should_reset_time = False
def find_segment_files(self):
"""查找TS分片文件返回排序后的文件名列表"""
pattern = os.path.join(HLS_SEGMENT_DIR, "segment_*.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
@@ -217,6 +219,8 @@ class HLSFrameProcessor(threading.Thread):
# 下一个分片不存在,根据连续失败次数调整等待时间
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
# 重置容器到开始