视频播放

视频播放

使用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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*-coding:utf-8 -*-
import cv2
import glob
import os
'''
单个视频和视频文件夹依次播放
按键:
Space >> 暂停与播放
ESC >> 退出程序
q >> 退出当前视频,进入下一个
s >> 保存当前帧为图片
o >> 慢进播放
i >> 快进播放
d >> 倒退一帧
l >> 前进5帧
k >> 前进50帧
'''
__author__ = 'Heroinlj <heroinlj@gmail.com>'


# 单个视频播放
def play_video(video_path):
'''
:param video_path: 需要播放的视频地址
:return: None
'''
video = cv2.VideoCapture()
auto_play_flag = False
decay_time = 27 if auto_play_flag else 0
cv2.namedWindow("video", cv2.WINDOW_NORMAL)
if not video.open(video_path):
print("can not open the video.")
exit(1)
video_name = os.path.basename(video_path)
num_frame = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
print("total_frame: ", num_frame)
count_frame = 0
while True:
ret, frame = video.read()
if frame is None:
break
count_frame += 1
cv2.imshow("video", frame)
key = cv2.waitKey(decay_time)
if key == 32:
auto_play_flag = not auto_play_flag
decay_time = 27 if auto_play_flag else 0
if key == ord('s') or key == ord('S'):
cv2.imwrite("{}/{}_{:07d}.jpg".format(image_save_path, video_name.split('.')[0], count_frame), frame)
if key == ord('d') or key == ord('D'):
count_frame = max(0, count_frame - 2)
video.set(cv2.CAP_PROP_POS_FRAMES, count_frame)
if key == ord('o') or key == ord('O'):
decay_time += 10
if key == ord('i') or key == ord('I'):
decay_time -= 10
decay_time = max(1, decay_time)
if key == ord('l') or key == ord('L'):
count_frame = min(num_frame, count_frame + 5)
video.set(cv2.CAP_PROP_POS_FRAMES, count_frame)
if key == ord('k') or key == ord('K'):
count_frame = min(num_frame, count_frame + 50)
video.set(cv2.CAP_PROP_POS_FRAMES, count_frame)
if key == ord('q') or key == ord('Q'):
break
if key == 27:
exit(1)
cv2.destroyAllWindows()


def play_videos_folder(videos_folder_path, image_save_path="."):
'''
:param videos_folder_path: 需要播放的视频文件夹地址
:return: None
'''
video = cv2.VideoCapture()
auto_play_flag = False
decay_time = 27 if auto_play_flag else 0
cv2.namedWindow("videos", cv2.WINDOW_NORMAL)
for video_path in sorted(glob.glob("{}/*.avi".format(videos_folder_path))):
print(video_path)
video_name = os.path.basename(video_path)
if not video.open(video_path):
print("can not open the video.")
exit(1)
num_frame = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
print("total_frame: ", num_frame)
count_frame = 0
while True:
ret, frame = video.read()
if frame is None:
break
count_frame += 1
cv2.imshow("videos", frame)
key = cv2.waitKey(decay_time)
if key == 32:
auto_play_flag = not auto_play_flag
decay_time = 27 if auto_play_flag else 0
if key == ord('s')or key == ord('S'):
cv2.imwrite("{}/{}_{:07d}.jpg".format(image_save_path, video_name.split('.')[0], count_frame), frame)
if key == ord('d')or key == ord('D'):
count_frame = max(0, count_frame - 2)
video.set(cv2.CAP_PROP_POS_FRAMES, count_frame)
if key == ord('o')or key == ord('O'):
decay_time += 10
if key == ord('i') or key == ord('I'):
decay_time -= 10
decay_time = max(1, decay_time)
if key == ord('l')or key == ord('L'):
count_frame = min(num_frame, count_frame + 5)
video.set(cv2.CAP_PROP_POS_FRAMES, count_frame)
if key == ord('k')or key == ord('K'):
count_frame = min(num_frame, count_frame + 50)
video.set(cv2.CAP_PROP_POS_FRAMES, count_frame)
if key == ord('q')or key == ord('Q'):
break
if key == 27:
exit(1)
cv2.destroyAllWindows()


def main():
play_video(r"1.avi")
# play_videos_folder(r"1")


if __name__ == '__main__':
main()
total_frame:  130