python-opencv剪辑视频

import cv2
class VideoTool:
‘’’
用于视频剪辑,视频转图片,图片转视频
‘’’
def init(self):#,video_path,videosavepath,photopath,photosavepath
self.video_path=video_path
#self.video_savpath=videosavepath
#self.photopath=photopath
#self.photosavepath=photosavepath
#self.size=size
#self.frameTOend=frameTOend
#self.video_savpath=videosavepath
def videocut(self,video_savepath,size,frameToStart,frameToend):
video_path=self.video_path
video = cv2.VideoCapture(video_path)
# 需要明确视频保存的格式
fps = video.get(cv2.CAP_PROP_FPS)
print(‘帧率:%d’ % fps)
#size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#size=(1200,600)#想要转换的分辨率
out = cv2.VideoWriter(video_savepath, cv2.VideoWriter_fourcc(*‘XVID’), fps, size)
print(“视频总帧数”, video.get(7))
# 设置视频截取的开始时间
#frameToStart = 160
#frameToend = 500
video.set(cv2.CAP_PROP_POS_FRAMES, frameToStart)
count = 0
while video.isOpened():
ret, frame = video.read() # 捕获一帧图像
if ret :
count += 1
if count<=frameToend:
frame_new = cv2.resize(frame,size)
out.write(frame_new)
else:
break
else:
break
video.release()
out.release()
cv2.destroyAllWindows()
if name == ‘main’:
video_path = r’F:\07 递归函数.avi’
vt=VideoTool()
result=vt.videocut(r’F:\07 递归函数out.avi’,(1200,640),500,1000)
#分辨率修改1200X640,截取500帧到1000帧的视频。

你可能感兴趣的:(python,opencv,视频处理)