先放代码:
import cv2
vid=cv2.VideoCapture('12.mp4')
fps=(vid.get(cv2.CAP_PROP_FPS))
print(fps)
size=(int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#print(size)
wri=cv2.VideoWriter("E://ab.avi",cv2.VideoWriter_fourcc('M','J','P','G'),fps,size)
【opencv写视频】success,frame=vid.read()
print(frame.shape)
#cv2.imshow("NEWSHOW",frame)
#cv2.waitKey(30)
#print(success)
while success:
wri.write(frame)
success,frame=vid.read()
print("DONE")
完成了从MP4视频读取并写入到新视频的过程但是现在还存在问题,就是在程序运行过程中,不能打开视频进行播放,需要关闭,或者再write新视频之后才能打开播放。
此外对于文件的尺寸有需求,可对其进行相应的压缩:
cv2.VideoWriter_fourcc('I','4','2','0') 文件极其大
cv2.VideoWriter_fourcc('M','J','P','G')较大
cv2.VideoWriter_fourcc('P','I','M','1') 比XVID稍大
cv2.VideoWriter_fourcc('X','V','I','D')比较小