使用python切分mp4视频并保存每帧图像
程序员文章站
2022-03-25 14:56:25
...
import cv2
def splitFrames(videoFileName):
cap = cv2.VideoCapture(videoFileName) # 打开视频文件
num = 1
while True:
# success 表示是否成功,data是当前帧的图像数据;.read读取一帧图像,移动到下一帧
success, img = cap.read()
if not success:
break
img_new=img
# cv2.flip(img,0,img_new)
dst_im = cv2.flip(img_new, 1) #原型:cv2.flip(src, flipCode[, dst]) → dst flipCode表示对称轴 0:x轴 1:y轴. -1:both
img_new = cv2.transpose(dst_im)
width = int(img_new.shape[1]*0.5)
height = int(img_new.shape[0]*0.5)
dim = (width, height)
img_new = cv2.resize(img_new, dim, interpolation = cv2.INTER_AREA)
cv2.imwrite('result/' +str(num)+".jpg",img_new)
print(num)
num = num + 1
cap.release()
splitFrames('1.mp4')