python - OpenCV 和来自 m3u8 url 的实时视频流视频
问题描述
我正在尝试构建一个需要从流 url 读取视频的应用程序。当 OpenCV 显示图像时,我看到了一些滞后,但是使用一个很棒的 url 来测试我的应用程序的视频,结果证明问题不是 OpenCV 速度太慢而是速度太快。
如果我尝试以下代码,我可以看到视频的再现速度至少比应有的速度快 3 倍。
我的代码:
import cv2
url="http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
vcap = cv2.VideoCapture(url)
while(True):
# Capture frame-by-frame
ret, frame = vcap.read()
if frame is not None:
# Display the resulting frame
cv2.imshow('frame',frame)
# Press q to close the video windows before it ends if you want
if cv2.waitKey(22) & 0xFF == ord('q'):
break
else:
print("Frame is None")
break
# When everything done, release the capture
vcap.release()
cv2.destroyAllWindows()
print("Video stop")
结果
应该如何
你知道让openCV正确读取视频的方法吗,我的意思是,我想要与视频相同的速度。
提前致谢
解决方案
这里是:
import cv2
import time
url = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
vcap = cv2.VideoCapture(url)
fps = vcap.get(cv2.CAP_PROP_FPS)
wt = 1 / fps
while True:
start_time = time.time()
# Capture frame-by-frame
ret, frame = vcap.read()
if frame is not None:
# Display the resulting frame
cv2.imshow('frame', frame)
# Press q to close the video windows before it ends if you want
if cv2.waitKey(22) & 0xFF == ord('q'):
break
dt = time.time() - start_time
if wt - dt > 0:
time.sleep(wt - dt)
else:
print("Frame is None")
break
# When everything done, release the capture
vcap.release()
cv2.destroyAllWindows()
print("Video stop")
推荐阅读
- android - 重新加载片段
- sql - T-SQL 查找最近没有预订任何后续应用的客户记录
- excel - 在 Excel 中一次获得多个答案
- c - 在c中将节点添加到双循环链表的开头
- reactjs - 在 React / Redux 中,将通用调度作为 props 来调度任何操作是否可以?
- python - 如果其他结果到 LCD,Raspberry Pi 4 输出 python:随机字符
- reactjs - 如何测试在 Jest 中返回随机 React 组件的函数
- excel - VBA Excel选择并删除具有相同ID的所有形状并删除
- adaboost - 如何计算 ADABoost 模型的形状值?
- angular - 多个对话框,for循环,打开 - angular 7 + angular material