Pygame播放视频(pygame+cv2)

可以利用cv2加载视频,然后将视频放入pygame的窗口中

就一个简单的视频播放器

import cv2
import time

import pygame


def play():
    stream = 'video/jiangnan.mp4'
    cap = cv2.VideoCapture(stream)

    ret, img = cap.read()
    if not ret:
        print("Can't read stream")
        # exit()

    # img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    print('shape:', img.shape)

    pygame.init()

    screen = pygame.display.set_mode((800, 600))
    surface = pygame.surface.Surface((800, 600))

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        ret, img = cap.read()
        if not ret:
            running = False
            break
        else:
            img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            surface = pygame.surfarray.make_surface(img)
            screen.blit(surface, (0, 0))
            pygame.display.flip()
        time.sleep(0.02)
        pygame.display.flip()

    pygame.quit()
play()

猜你喜欢

转载自blog.csdn.net/weixin_42636075/article/details/130393820
今日推荐