So I've been trying to find ways to play mp4 or videos in general in pygame and cant really find anything that works apart from opencv-python. I Have managed to play the video but it's very laggy and choppy so i tried to first read the video and save the frames inside a list then play the video But then the video still turned out to be very choppy?
import pygame
from pygame.locals import *
import cv2
import numpy as np
import sys
import threading
pygame.init()
pygame.display.set_caption("OpenCV video stream on Pygame")
surface = pygame.display.set_mode([1280,720])
video = cv2.VideoCapture("YouTube.mp4")
fps = video.get(cv2.CAP_PROP_FPS)
print(fps)
video.set(cv2.CAP_PROP_FPS, 60)
vid = []
clock = pygame.time.Clock()
print(clock)
def vid_cap():
while True:
success, frame = video.read()
if not success:
break
#for some reasons the frames appeared inverted
frame = np.fliplr(frame)
frame = np.rot90(frame)
# The video uses BGR colors and PyGame needs RGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
vid.append(frame)
#cap = threading.Thread(target = vid_cap)
#cap.start()
vid_cap()
try:
while True:
clock.tick(60)
surface.fill([0,0,0])
try:
surf = pygame.surfarray.make_surface(vid[0])
vid.pop(0)
for event in pygame.event.get():
if event.type == pygame.KEYUP:
background_color = red
surface.fill(background_color)
pygame.display.update
end_time = self.time()
# Show the PyGame surface!
surface.blit(surf, (0,0))
pygame.display.flip()
except:
surface.fill([255,255,255])
except SystemExit:
pygame.quit()
cv2.destroyAllWindows()
YouTube.mp4 can be any 60 fps video you got