若找不到下一分片,则时间轴暂停,而不是一直向前
This commit is contained in:
@@ -51,6 +51,8 @@ class HLSFrameProcessor(threading.Thread):
|
|||||||
self.last_process_time = None # 上次处理时间
|
self.last_process_time = None # 上次处理时间
|
||||||
self.last_frame_pts = None # 上一帧的PTS时间
|
self.last_frame_pts = None # 上一帧的PTS时间
|
||||||
self.pts_diff = 0
|
self.pts_diff = 0
|
||||||
|
|
||||||
|
self.should_reset_time = False
|
||||||
|
|
||||||
def find_segment_files(self):
|
def find_segment_files(self):
|
||||||
"""查找TS分片文件,返回排序后的文件名列表"""
|
"""查找TS分片文件,返回排序后的文件名列表"""
|
||||||
@@ -146,7 +148,7 @@ class HLSFrameProcessor(threading.Thread):
|
|||||||
elif time_diff < -0.06: # 超前60ms,需要等待 (3帧)
|
elif time_diff < -0.06: # 超前60ms,需要等待 (3帧)
|
||||||
# 等待到正确的时间点
|
# 等待到正确的时间点
|
||||||
wait_time = -time_diff
|
wait_time = -time_diff
|
||||||
if wait_time > 0.1: # 如果等待时间过长,重置时间基准
|
if wait_time > 5: # 如果等待时间过长,重置时间基准
|
||||||
logger.info(f"[WARN] Too far ahead, resetting time base")
|
logger.info(f"[WARN] Too far ahead, resetting time base")
|
||||||
self.start_time = current_time
|
self.start_time = current_time
|
||||||
self.base_pts = frame_pts
|
self.base_pts = frame_pts
|
||||||
@@ -216,7 +218,9 @@ class HLSFrameProcessor(threading.Thread):
|
|||||||
else:
|
else:
|
||||||
# 下一个分片不存在,根据连续失败次数调整等待时间
|
# 下一个分片不存在,根据连续失败次数调整等待时间
|
||||||
consecutive_failures += 1
|
consecutive_failures += 1
|
||||||
|
|
||||||
|
self.should_reset_time = True
|
||||||
|
|
||||||
if consecutive_failures <= 10:
|
if consecutive_failures <= 10:
|
||||||
sleep_time = 0.02 # 前10次失败,等待0.02秒
|
sleep_time = 0.02 # 前10次失败,等待0.02秒
|
||||||
elif consecutive_failures <= 20:
|
elif consecutive_failures <= 20:
|
||||||
@@ -244,13 +248,19 @@ class HLSFrameProcessor(threading.Thread):
|
|||||||
container = av.open(segment_path)
|
container = av.open(segment_path)
|
||||||
video_stream = container.streams.video[0]
|
video_stream = container.streams.video[0]
|
||||||
|
|
||||||
# 如果是第一个分片,需要设置base_pts
|
# 重置时间轴:使用新分片的第一帧PTS重新设置时间基准
|
||||||
if self.base_pts is None:
|
current_time = time.time()
|
||||||
|
|
||||||
|
if self.base_pts is None or self.should_reset_time:
|
||||||
# 读取第一帧来获取PTS
|
# 读取第一帧来获取PTS
|
||||||
for packet in container.demux(video_stream):
|
for packet in container.demux(video_stream):
|
||||||
for frame in packet.decode():
|
for frame in packet.decode():
|
||||||
if frame.pts is not None:
|
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
|
||||||
break
|
break
|
||||||
# 重置容器到开始
|
# 重置容器到开始
|
||||||
|
|||||||
Reference in New Issue
Block a user