本文通过OpenCV将视频裁剪成指定时间范围内的小视频,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import cv2

def capture_video(video_path, result_video_path, video, result_video, start_time, end_time):
"""
功能:截取短视频
参数:
video_path:需要截取的视频路径
result_video_path:截取后的视频存放的路径
video:需要截取的视频的名称(不带后缀)
result_video:截取了的视频的名称(不带后缀)
start_time:截取开始时间(单位s)
end_time:截取结束时间(单位s)
"""

# 读取视频
cap = cv2.VideoCapture(video_path + video)

# 读取视频帧率
fps_video = cap.get(cv2.CAP_PROP_FPS)

# 设置写入视频的编码格式
fourcc = cv2.VideoWriter_fourcc(*"mp4v")

# 获取视频宽度和高度
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# 设置写视频的对象
videoWriter = cv2.VideoWriter(result_video_path + result_video, fourcc, fps_video, (frame_width, frame_height))

# 初始化一个计数器
count = 0

while (cap.isOpened()):

# 读取视屏里的图片
ret, frame = cap.read()

# 如果视屏没有读取结束
if ret == True:

# 计数器加一
count += 1

# 截取相应时间内的视频信息
if(count > (start_time * fps_video) and count <= (end_time * fps_video)):
# 将图片写入视屏
videoWriter.write(frame)

if(count == (end_time * fps_video)):
break

else:
# 写入视屏结束
videoWriter.release()
break

if __name__ == '__main__':
video_path = "original_video/"
result_video_path = "result_video/"
video = "test_video.mp4"
result_video = "result_test_video.mp4"
start_time = 5 * 60 + 30
end_time = 6 * 60 + 30
capture_video(video_path, result_video_path, video, result_video, start_time, end_time)