python - OpenCV没有得到视频的最后一帧
问题描述
我尝试从视频中获取第一帧、中间帧和最后一帧,但它只能获取第一帧和中间帧。
这是我的代码:
import cv2
import math
vidcap = cv2.VideoCapture("/home/data/input/video.MOV")
# Get number of frame of the video
length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
print(length)
# Define a tuple of frame need to get
frame = (0, math.ceil(length/2), length - 1)
# Get the frame
for f in frame:
vidcap.set(1, f)
_, image = vidcap.read()
cv2.imwrite("/home/data/output/frame%d.jpg" % f, image)
它确实保存了最后一帧图像,但没有任何数据。
我的代码有什么问题吗?
更新:
根据这篇文章,似乎cv2.CAP_PROP_FRAME_COUNT
可以计算错误的帧数。
那么有没有更好的方法来获取帧数而不读取整个帧?
解决方案
vidcap = cv2.VideoCapture("/home/data/input/video.MOV")
count = 1
while 1:
ret , frame = vidcap.read()
count += 1
if ret == False:
break
print(count)
可能会出现打字错误,但您可以理解。
推荐阅读
- c - 无法弄清楚 printf 语句
- java - 未触发 Android 通知
- c# - 如何更新 Y 轴标题的值?
- wildcard - DataFactory 在使用通配符时多次复制文件
- javascript - 从内容脚本发送消息以响应应用程序不起作用
- web - Pixi JS 填充行为与 SVG 填充行为
- azure - 出现 CreateServerCertificateAsync 错误:在 IoT Edge 设备上使用测试证书时无法获取服务器证书(天蓝色)
- python - 为什么我的 while 循环随机花费 100 倍以上的时间
- c - 如何每 1 秒重复一次 printf(),但使用特定的用户输入打开和关闭它?
- firebase - Flutter Firestore 我可以使用地图创建集合吗