python编程,将视频剪切成图像,图像再合成视频
a. 视频剪切成图像:
importcv2importos
vidcap= cv2.VideoCapture('./video/7.mp4')
success,image=vidcap.read()
count=0
success=True
savedir='./f3/'
if notos.path.exists(savedir):
os.mkdir(savedir)whilesuccess:
success,image=vidcap.read()
cv2.imwrite(savedir+"7frame%d.jpg" % count, image) #save frame as JPEG file
if cv2.waitKey(10) == 27:breakcount+= 1num=len(os.listdir(savedir))-1os.remove(savedir+"7frame%d.jpg" % num)
注:
1、将 './video/7.mp4' 换成你的视频路径加名称
2、将 './f3/' 换成你的图像存储路径
3、"7frame%d.jpg" 表示每一帧的图像名称。第一帧图像,这里命名为 "7frame1.jpg" ,你也可以换成自己喜欢的命名
4、下面这一句,注释掉还是留着,取决与你的最后一张图像是否可视,在我的工作中,最后一帧的图像打不开,直接采用下面这句把最后一张图像给删除了
num=len(os.listdir(savedir))-1os.remove(savedir+"7frame%d.jpg" % num)
b. 图像合成视频:
#!/usr/bin/env python#-*-coding:utf-8-*-
importcv2importos
fsp= 20fourcc= cv2.VideoWriter_fourcc(*'avc1')
video_path= './hecheng.mp4'video_out= cv2.VideoWriter(video_path, fourcc, fsp, (1280,720))
img_path= "./f3/"
for i in range(0,1200):if os.path.exists(img_path+"7frame"+str(i)+'.jpg'):
frame= cv2.imread(img_path+"7frame"+str(i)+'.jpg')
video_out.write(frame)
video_out.release()
注:
1、fsp表示帧率,即一秒钟播放多少张图像。
播放快点(类似快进的效果),fsp设置大点。
播放慢点(类似慢动作的效果),fsp设置小点。
2、将'./hecheng.mp4'换成你想要的命名。
3、(1280,720)表示每一帧图像的大小。具体如图所示:
4、img_path = "./f3/" 是图像的存储路径。即你将"./f3/"路径下的图像合成视频
5、(0,1200)中,1200是一个数,需要大于等于"./f3/"路径下图像的数目。0固定不变。
6、"7frame"+str(i)+'.jpg'表示图像名称。和切图时候的图像命名一致。