cap = cv2.VideoCapture(video_name)
进行指定视频文件的读取def _open_video_(self):
self.video_name = os.path.split(self.video_path_name)[-1]
if self.video_name.split('.')[-1]not in ['mp4','avi','h264']:
loggingdata('video name error, {}'.format(self.video_path_name))
return -1
self.frame_now = 0
self.cap = cv2.VideoCapture(self.video_path_name)
self.frame_num = int(self.cap.get(7))
self.FPS = int(self.cap.get(5))
self.frame_w = int(self.cap.get(3))
self.frame_h = int(self.cap.get(4))
cap.get(0) CV_CAP_PROP_POS_MSEC 视频文件的当前位置(播放)以毫秒为单位
cap.get(1) CV_CAP_PROP_POS_FRAMES 基于以0开始的被捕获或解码的帧索引
cap.get(2) CV_CAP_PROP_POS_AVI_RATIO 视频文件的相对位置(播放):0 = 电影开始,1 = 影片的结尾。
cap.get(3) CV_CAP_PROP_FRAME_WIDTH 在视频流的帧的宽度
cap.get(4) CV_CAP_PROP_FRAME_HEIGHT 在视频流的帧的高度
cap.get(5) CV_CAP_PROP_FPS 帧速率
cap.get(6) CV_CAP_PROP_FOURCC 编解码的4字-字符代码
cap.get(7) CV_CAP_PROP_FRAME_COUNT 视频文件中的帧数
00:00:15
转换成秒,【注】这里默认输入多组时间段def _time2second(self,start_end_time):
将输入的时间转换成秒 ['00:10:40','00:10:47'] - > [600,900]
second_time = []
for span_time in start_end_time:
start_time = span_time[0].split(':')
print(start_time)
start_time = int(start_time[0])*3600 + int(start_time[1])*60 + int(start_time[2])
end_time = span_time[1].split(':')
end_time = int(end_time[0])*3600 + int(end_time[1])*60 + int(end_time[2])
# print(end_time)
if end_time < start_time:
return -1
second_time.append([start_time,end_time])
return second_time
cap.set(cv2.CAP_PROP_POS_FRAMES,start_frame)
指定读取位置,这种方式较快.h264
利用第二种不能实现定位,每次还是从视频起点开始读,所以本例采用前者进行抽帧;def video2frame(self,method = 0,frame_span = 1,ex_frame_time = [0,10]):
# 1. 视频抽帧
# method = 0 整体抽帧 method = 1 时间段抽帧
loggingdata('---------------start video2frame---------------------')
self._open_video_()
ex_frame_time = [x*self.FPS for x in ex_frame_time] if method == 1 else [0,self.frame_num]
while True:
if method == 1 and self.frame_now >= ex_frame_time[1]:
loggingdata('video2frame save picture number is {},save frame form {} to {}'.format(
ex_frame_time[1],ex_frame_time[0],self.frame_now))
break
success, origin_img = self.cap.read()
if not success or len(origin_img) < 2:
break
self.frame_now +=1
if self.frame_now%frame_span != 0:
continue
if self.frame_now < ex_frame_time[1] and self.frame_now >= ex_frame_time[0]:
cv2.imwrite(os.path.join(self.img_save_path,self.video_name.split('.')[0])+"_%.6d.jpg"%self.frame_now,origin_img)
loggingdata('==============save image stop==================')
cut_video
中,帧率默认原视频,名称在原名称上增加裁剪起始帧def cut_video(self,save_fps = 20,cut_frame_time = [0,10]):
self._open_video_()
save_fps = self.FPS
cut_frame_time = [x*self.FPS for x in cut_frame_time]
cut_video = cv2.VideoWriter(os.path.join(self.cut_video_save_path,self.video_name.split('.')[0])+\
'_'+str(cut_frame_time[0]) + '.mp4', cv2.VideoWriter_fourcc('M','P','E','G'), save_fps, (self.frame_w,self.frame_h))
while self.frame_now < cut_frame_time[1]:
success, origin_img = self.cap.read()
if not success or len(origin_img) < 2 or self.frame_now >= cut_frame_time[1]:
break
self.frame_now +=1
# print(self.frame_now)
if self.frame_now > cut_frame_time[0] and self.frame_now < cut_frame_time[1]:
cut_video.write(origin_img)
print('cut end')
cut_video.release()
def img2video(self,save_fps = 20):
img_h = 0
img_w = 0
for img_name in os.listdir(self.img_files):
if img_name.split('.')[-1] not in ['bmp','jpg','jpeg','png']:
continue
tmp_img = cv2.imread(os.path.join(self.img_files,img_name))
img_h = tmp_img.shape[0]
img_w = tmp_img.shape[1]
break
img2video = cv2.VideoWriter(self.video_save_path, cv2.VideoWriter_fourcc('M','P','E','G'), save_fps, (img_w,img_h))
for img_name in os.listdir(self.img_files):
if img_name.split('.')[-1] not in ['bmp','jpg','jpeg','png']:
continue
tmp_img = cv2.imread(os.path.join(self.img_files,img_name))
img2video.write(tmp_img)
img2video.release()
20210413更新
可执行文件下载
https://download.csdn.net/download/wangxiaobei2017/16651184
20210519更新
def video_link(self,save_fps = 20,video_w=1280,video_h=1024):
if len(self.video_file_name) < 1:
loggingdata("video_link ERROR : no video file!!!")
return -1
# 多段视频拼接在一起
self.frame_now = 0
tmp_video_link = cv2.VideoWriter(self.video_save_path, cv2.VideoWriter_fourcc('M','P','E','G'), save_fps, (video_w,video_h))
for video_name in os.listdir(self.video_file_name):
if video_name.split('.')[-1]not in ['mp4','avi','h264']:
loggingdata("can not open this video {}".format(video_name))
continue
cap = cv2.VideoCapture(os.path.join(self.video_file_name,video_name))
# print(os.path.join(self.video_file_name,video_name))
while self.stop_flag:
success, origin_img = cap.read()
if not success or len(origin_img) < 2:
break
# print(self.frame_now)
self.frame_now += 1
tmp_video_link.write(cv2.resize(origin_img,(video_w,video_h)))
tmp_video_link.release()
while self.stop_flag and self.frame_now < cut_frame_time[1]:
success, origin_img = self.cap.read()
if not success or len(origin_img) < 2 or self.frame_now > cut_frame_time[1]:
break
self.frame_now +=1
if self.frame_now > cut_frame_time[0] and self.frame_now < cut_frame_time[1]:
if video_speed >= 1:
if self.frame_now%int(video_speed) == 0:
cut_video.write(origin_img)
else:
for i in range(int(1/video_speed)):
cut_video.write(origin_img)
可执行文件下载 版本:20210519
https://download.csdn.net/download/wangxiaobei2017/18862562