基于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
import os
import cv2

# 视频文件夹路径
folder_path = 'D:/Users/video'

# 获取文件夹中的所有视频文件
video_files = [os.path.join(folder_path, file) for file in os.listdir(folder_path)
if file.lower().endswith(('.mp4', '.avi', '.mkv'))]

# 创建一个视频播放窗口
cv2.namedWindow('Video Player', cv2.WINDOW_NORMAL)

# 初始化视频索引
current_index = 0
total_videos = len(video_files)

while True:
# 读取当前索引处的视频文件
video_path = video_files[current_index]
video = cv2.VideoCapture(video_path)

while video.isOpened():
# 读取视频帧
ret, frame = video.read()
if not ret:
break

# 显示视频帧
cv2.imshow('Video Player', frame)

# 按下 'q' 键退出当前视频
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# 释放当前视频的资源
video.release()

# 等待用户按键
key = cv2.waitKey(0)

# 根据按键进行操作
if key == ord('a'): # 按下 'a' 键退出播放器
break
elif key == ord('s'): # 按下 's' 键播放下一个视频
current_index = (current_index + 1) % total_videos
elif key == ord('d'): # 按下 'd' 键播放上一个视频
current_index = (current_index - 1) % total_videos

# 关闭窗口
cv2.destroyAllWindows()