Opencv 剪切视频文件 Ubuntu16.04
本文参考自 :https://blog.csdn.net/u011436429/article/details/80522886
原文中有些地方,本人看这有些不太适合自己,所以就自己写一下自己的,环境:ubunut 16.04,Python,opencv
import cv2
import os
# 读入视频文件
# path 视频文件路径 start_frame:起始时间:用秒计算 end_frame: 结束时间 i:文件名
#cut_video(path,start_time=10,end_time=350,new_file_name=1)
def cut_video(path,start_time,end_time,new_file_name):
video_capture = cv2.VideoCapture(path)
# video_capture.get 参数网址 :https://blog.csdn.net/u011436429/article/details/80604590
# FPS
fps = video_capture.get(5)
start_frame = start_time*fps
end_frame = end_time*fps
print(video_capture.isOpened())
print("fps", video_capture.get(5))
# 总帧数
print("COUNT", video_capture.get(7))
# 视频的宽和高
size = (int(video_capture.get(3)), int(video_capture.get(4)))
print("宽: %d高: %d" % (int(video_capture.get(3)), int(video_capture.get(4))))
# 当前帧
frame_index = 0
success, bgr_image = video_capture.read()
# fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
v = cv2.VideoWriter(new_file_name + '.avi', fourcc, fps, size)
while success: # 循环读取视频帧
# cv2.imshow('frame', bgr_image)
# 每次读取视频帧都写进文件,当到达start_frame 时,开始写帧
if frame_index >= start_frame:
if v.isOpened():
v.write(bgr_image)
# 如果到达了end_frame即 2 结束写该文件
if frame_index == end_frame:
if v.isOpened():
v.release()
video_capture.release()
break
success, bgr_image = video_capture.read() # 获取下一帧
frame_index = frame_index + 1 # 当前帧位置