使用Python一键提取视频中的帧图片

本文最后更新于 2025年4月22日 下午

为了从视频中提取每一帧图片,编写Python脚本实现该功能,脚本中的关键参数根据实际情况进行修改

  • video_path为指定的视频路径

  • interval为指定分割视频是是否跳帧,默认不跳帧,即全部分割

  • width, height 为指定对分割帧图片调整大小,默认不调整

分割单视频文件

该脚本自动对帧图片编号,设置为7位编码,最多可分割9999999帧图片,即92小时的30FPS视频

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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ============================================================
# @Date : 2021/12/08 14:40:31
# @Author : LiShan
# @Email : lishan@st.xatu.edu.com
# @File : extract.py
# @IDE : PyCharm
# @Func : Extract video image
# ============================================================
import os.path
import time
import cv2

video_path = "./assets/intersection.mp4"
save_path = video_path[:video_path.rfind('.')]
os.makedirs(save_path, exist_ok=True)
cap = cv2.VideoCapture(video_path)
fps = int(cap.get(cv2.CAP_PROP_FPS))
print('FPS:{:.2f}'.format(fps)
rate = cap.get(5)
frame_num = cap.get(7)
duration = frame_num/rate
print('video total time:{:.2f}s'.format(duration))

# width, height = 1920, 1080
# interval = int(fps) * 4
interval = 1
process_num = frame_num // interval
print('process frame:{:.0f}'.format(process_num))

cnt = 0
num = 0

t0 = time.time()
while cap.isOpened():
ret, frame = cap.read()
if ret:
cnt += 1
if cnt % interval == 0:
num += 1
# frame = cv.resize(frame, (width, height))
cv2.imwrite(save_path + "/%07d.jpg" % num, frame)
remain_frame = process_num - num
t1 = time.time() - t0
t0 = time.time()
print("Processing %07d.jpg, remain frame: %d, remain time: %.2fs" % (num, remain_frame, remain_frame * t1))
else:
break
if cv2.waitKey(1) & 0xff == 27:
break

cap.release()
cv2.destroyAllWindows()
print("done")

分割效果如下:

img

分割多视频文件

增加多对个视频文件提取制作数据集的功能

img

文件夹内的视频文件如下

img

运行下列脚本文件,分割后的图片将保存到JPEGImage文件夹

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
66
67
68
69
70
71
72
73
74
75
76
77
78
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ============================================================
# @Date : 2021/12/08 14:40:31
# @Author : LiShan
# @Email : lishan@st.xatu.edu.com
# @File : extract.py
# @IDE : PyCharm
# @Func : Extract video image
# ============================================================
import os.path
import time
import cv2


def get_video_list(path):
video_ext = [".mp4", ".avi"]
video_names = []
for maindir, subdir, file_name_list in os.walk(path):
for filename in file_name_list:
apath = os.path.join(maindir, filename)
ext = os.path.splitext(apath)[1]
if ext in video_ext:
video_names.append(apath)
return video_names


video_path = "./video/"
save_path = "./JPEGImages/"
print("frame image save path:{}".format(save_path))
os.makedirs(save_path, exist_ok=True)

num = 0
video_cnt = 0
if os.path.isdir(video_path):
files = get_video_list(video_path)
else:
files = [video_path]
files.sort()
video_num = len(files)
for i in range(video_num):
video_name = files[i]
print('video name: {}'.format(video_name))
print('progress: {:.0f}/{:.0f}'.format(i + 1, video_num))
cap = cv2.VideoCapture(video_name)
fps = int(cap.get(cv2.CAP_PROP_FPS))
print('FPS: {:.2f}'.format(fps))
rate = cap.get(5)
frame_num = cap.get(7)
duration = frame_num/rate
print('video total time: {:.2f}s'.format(duration))

# width, height = 1920, 1080
# interval = 1
interval = int(fps) * 4
process_num = frame_num // interval
print('process frame: {:.0f}'.format(process_num))
cnt = 0
current_num = 0
t0 = time.time()
while cap.isOpened():
ret, frame = cap.read()
if ret:
cnt += 1
if cnt % interval == 0:
num += 1
current_num += 1
# frame = cv.resize(frame, (width, height))
cv2.imwrite(save_path + "/%07d.jpg" % num, frame)
remain_frame = process_num - current_num
t1 = time.time() - t0
t0 = time.time()
print("Processing %07d.jpg, remain frame: %d, remain time: %.2fs" %
(num, remain_frame, remain_frame * t1))
else:
break
cap.release()
print("done")

分割结果如下:

img

使用Python一键提取视频中的帧图片
https://www.bit01.top/2025/03/26/python-extracts-images/
作者
李珊
发布于
2025年3月26日
更新于
2025年4月22日
许可协议