希望通过cv2.VideoCapture类截取视频帧图片,期间只能截取视频的每帧图片,不能跳帧截取图片,原来是在读取视频帧的时候没有写入ret,
frame = cv2.VideoCapture.read()。

    ret, frame = cv2.VideoCapture.read()函数输出的是两个参数,第一个参数ret 为True
或者False,代表有没有读取到图片第二个参数frame表示截取到一帧的图片。

修改前代码(只能逐帧逐帧截取视频帧图片):
import cv2 import os def save_img(): video_path = r'你的视频绝对路径' videos =
os.listdir(video_path) for video_name in videos: file_name =
video_name.split('.')[0] folder_name = file_name
os.makedirs(folder_name,exist_ok=True) vc =
cv2.VideoCapture(video_path+video_name) #读入视频文件 c = 1 if vc.isOpened(): #
判断是否正常打开 rval, frame = vc.read() else: rval = False timeF = 300 # 视频帧计数间隔频率
while rval: # 循环读取视频帧 pic_path = folder_name + '/' if (c % timeF == 0): #
每隔timeF帧进行存储操作 cv2.imwrite(pic_path + file_name + '_' + str(c) + '.jpg',
frame)#存储图像 c = c + 1 cv2.waitKey(1) vc.release() save_img()
修改后代码(能够跳帧截取视频帧):
import cv2 import os def save_img(): video_path =
r'D:\python3-PycharmProjects1\video2picture\20180911-12-48-31\data\123/' videos
= os.listdir(video_path) for video_name in videos: file_name =
video_name.split('.')[0] folder_name = file_name
os.makedirs(folder_name,exist_ok=True) vc =
cv2.VideoCapture(video_path+video_name) #读入视频文件 c = 1 if vc.isOpened(): #
判断是否正常打开 rval, frame = vc.read() else: rval = False timeF = 300 # 视频帧计数间隔频率
while rval: # 循环读取视频帧 rval, frame = vc.read() pic_path = folder_name + '/' if
(c % timeF == 0): # 每隔timeF帧进行存储操作 cv2.imwrite(pic_path + file_name + '_' +
str(c) + '.jpg', frame) # 存储为图像,保存名为 文件夹名_数字(第几个文件).jpg c = c + 1
cv2.waitKey(1) vc.release() save_img()
以后写代码需要仔细检查。

参考博客:https://blog.csdn.net/qq_38214193/article/details/80997924
<https://blog.csdn.net/qq_38214193/article/details/80997924>