import cv2 as cv
import numpy
cap = cv.VideoCapture('./shangyiv.mp4')
fourcc = cv.VideoWriter_fourcc(*'mp4v')
width = int(cap.get(3))
height = int(cap.get(4))
out = cv.VideoWriter('new.mp4',fourcc,60,(width,height))
#fps=60,这里是视频的帧率,可以随意调整,大小只影响每张图片的播放速率
#(width,height)图片的大小,如果是用视频则是分辨率大小.这里的值需与写入的图片或视频保持一致
while cap.isOpened():
ret,frame = cap.read()
if not ret:
print('can not receive frame')
break
#图片像素翻转,0是x轴翻转,1是y轴翻转,-1是x和y一起
frame = cv.flip(frame,-1)
out.write(frame)
cv.namedWindow('frame',cv.WINDOW_NORMAL)
cv.resizeWindow('frame',200,100)
cv.imshow('frame', frame)
if cv.waitKey(1) == ord('q'):
break
cap.release()
out.release()
cv.destroyAllWindows()
注意点:
1.不同的视频格式,有不同的fourcc编码规则对应 mp4=>mp4v
2.创建videoWriter容器时,容器size大小应与将要写入的图片大小或视频分辨率大小保持一致,
否则会导致容器太小无法写入帧