这篇教程pygame+opencv实现读取视频帧的方法示例写得很实用,希望能帮到您。 由于pygame.movie.Movie.play() 只支持MPEG格式的视频,且 pygame版本大于1.9.5好像已经不支持这个模块了,所以决定使用与opencv读取视频帧的画面,利用pygame的surface刷新窗口。
有基础的小伙伴,代码还是很好理解,直接上代码 pygame.time.Clock()同步时间
import pygamefrom pygame.locals import *import cv2import sysimport timeFPS = 30FramePerSec = pygame.time.Clock()video_path = './Selected Stimuli/noaudio_c_001_critical_swerve.mp4'video = cv2.VideoCapture(video_path)pygame.init()pygame.display.set_caption('OpenCV Video Player on Pygame')screen = pygame.display.set_mode((1280, 720), 0, 32)screen.fill([0,0,0])num = 0while True : T1 = time.time() ret, frame = video.read() if ret == False: print('Total Time:', time.time()-T0) sys.exit() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = cv2.transpose(frame) frame = pygame.surfarray.make_surface(frame) screen.blit(frame, (0,0)) if num == 0: T0 = time.time() pygame.display.update() FramePerSec.tick(FPS) num += 1 print('freq time:{}, frame num: {}'.format(time.time()-T1, num)) for event in pygame.event.get(): if event.type == QUIT: sys.exit() Django Auth应用实现用户身份认证 Pygame与OpenCV联合播放视频并保证音画同步
|