opencv 设置超时时间
经常爬视频数据,然后用opencv做成图片
因此设置超时时间很重要
cap.set(cv2.CAP_PROP_FPS, timeout_ms)
for idx, row in data.iterrows():
if idx < 400:
continue
try:
# 打开视频文件
timeout_ms = 5000
cap = cv2.VideoCapture(row['PLAY_URL'])
cap.set(cv2.CAP_PROP_FPS, timeout_ms)
frame_rate = int(cap.get(cv2.CAP_PROP_FPS))
# 获取视频帧总数
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_id = 0
while frame_id < total_frames:
frame_id += int(10 * frame_rate)
print(frame_id)
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_id) # 设置要读取的帧的索引
ret, frame = cap.read()
if ret:
img_path = os.path.join(r"D:\code\baidu-spider\dataset","{}_{}.jpg".format(idx,frame_id))
res = cv2.imwrite(img_path, frame)
print("--------------",img_path,res)
更多推荐
所有评论(0)