视频播放 发表于 2017-09-27 | 更新于 2019-06-13 | 分类于 scripts 视频播放使用opencv进行简单地视频播放功能,加入按键事件 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128# -*-coding:utf-8 -*-import cv2import globimport 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