python将视频按帧读取并转换成图片

python将视频按帧读取并转换成图片

代码如下

from glob import glob
import cv2
import tqdm
import os

video_path = glob(r’F:\my_demo\file_operation\video_files\*.mp4’)#视频所在路径
for videos in video_path:
vc = cv2.VideoCapture(videos)
name = videos.split(‘\’)[-1].split(‘.’)[0]
# print(name)
save_path = os.path.join(os.getcwd(), name+‘_file’)#多个视频文件,创建对应的文件夹
if not os.path.exists(save_path):
file = os.mkdir(save_path) # 如果不存在这个path,就自动创建一个
# n = 0 #计数
# print(vc)
if vc.isOpened(): #判断是否打开
rval,frame = vc.read()
else:
rval = False
FPS = 1
i = 0
while rval : #循环读取视频
rval,frame = vc.read()
if (i % FPS == 0):
i += 1
print(f’第{i}张图片已经生成’)
try:
save = save_path + f’/image{i}.jpg’
cv2.imwrite(save,frame)
except:
break
vc.release()

你可能感兴趣的:(python,音视频,opencv)