opencv视频抽帧保存图片video2pic
opencv视频按帧率抽帧
import cv2
import os
video_path='C:/Users/Desktop/video/1.mp4'
name=os.path.basename(video_path).split(".")[0]
output_dir_folder = 'C:/Users/Desktop/pic/'
output_folder=os.path.join(output_dir_folder,name)
fps = 1
if not os.path.exists(output_folder):
os.makedirs(output_folder)
cap = cv2.VideoCapture(video_path)
step = max(1, int(cap.get(cv2.CAP_PROP_FPS) / fps))
i = 0
count=1
while True:
ret, frame = cap.read()
if not ret:
break
if i % step == 0:
filename = os.path.join(output_folder, name+f"_{count:03d}.jpg")
cv2.imwrite(filename, frame)
count+=1
print(f"Saved frame: {i}")
i += 1
cap.release()
opencv视频按帧数抽帧
import cv2
import time
cap = cv2.VideoCapture(1)
count=0
i=0
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
if i%10==0:
cv2.imwrite("C:/Users/Desktop/0527_%s.jpg"%(count),frame)
count+=1
i+=1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()