如何使用Python+SecureCRT实现视频卡顿的自动化测试
今日在工作中遇到一个项目就是对某信电视播放的4K视频进行压测由于第一次接触过这个缺少自动化方案,我变产生了需求:python 录制视频根据帧率,计算每秒处理的帧数。若连续5秒内FPS持续低于20,保存文件并打开SecureCRT输入指令hisense_bugreport,等待3分钟后将U盘中的文件复制到电脑的D盘temp的文件下。于是我将需求提给了deepsek,下面就是人工智能给我的自动化方案。
根据您的需求,结合Python帧率监测与SecureCRT自动化操作,以下是完整实现方案:
一、帧率监测核心逻辑
import cv2
import time
from threading import Thread
class FPSMonitor:
def __init__(self, src=0, threshold=20, duration=5):
self.cap = cv2.VideoCapture(src)
self.threshold = threshold
self.duration = duration
self.fps_list = []
self.lock = threading.Lock()
self.triggered = False
def calculate_fps(self):
start_time = time.time()
frame_count = 0
while True:
ret, _ = self.cap.read()
if not ret:
break
frame_count += 1
elapsed_time = time.time() - start_time
if elapsed_time >= 1:
current_fps = frame_count / elapsed_time
with self.lock:
self.fps_list.append(current_fps)
if len(self.fps_list) > self.duration:
self.fps_list.pop(0)
print(f"当前FPS: {current_fps:.2f}")
start_time = time.time()
frame_count = 0
if self.check_threshold():
self.trigger_event()
def check_threshold(self):
with self.lock:
if len(self.fps_list) < self.duration:
return False
return all(fps < self.threshold for fps in self.fps_list)
def trigger_event(self):
if not self.triggered:
self.triggered = True
print("触发卡顿处理流程")
# 启动后续操作线程
Thread(target=self.post_process).start()
def post_process(self):
# 保存当前视频文件(示例路径需根据实际情况修改)
self.cap.release()
cv2.imwrite("temp_video.mp4", ...) # 需补充帧数据保存逻辑
self.send_securecrt_command()
self.wait_and_copy()
def send_securecrt_command(self):
# 通过SecureCRT发送指令(需提前配置会话)
import os
os.system("echo 'hisense_bugreport' | c:\Program Files\VanDyke SecureCRT\SecureCRT.exe -s /your_session_name")
该方案通过多线程分离帧处理与事件响应,结合自动化工具链实现全流程闭环。具体实现时需根据实际硬件环境调整路径和参数。