Skip to content Skip to sidebar Skip to footer

Playing Videos In Pygame 2

Pygame 1.x has a movie module which makes playing movies pretty straightforward. The internet is full of variations of the answer provided in this SO question However, I'm using Py

Solution 1:

Edit, 2021-11-09: - My answer below will get the job done, but there is a more idiomatic answer (ie., less of a hack) here


Thanks to the folks over at the pygame discord and this example of using FFMPEG with a subprocess, I have a working solution. Hopefully this will help someone out in the future.

import pygame
from lib.constants import SCREEN_WIDTH, SCREEN_HEIGHT
from viewcontrollers.Base import BaseView
import subprocess as sp

FFMPEG_BIN = "ffmpeg"
BYTES_PER_FRAME = SCREEN_WIDTH * SCREEN_HEIGHT * 3classAnimationFullScreenView(BaseView):

    def__init__(self):
        super(AnimationFullScreenView, self).__init__()

        command = [
            FFMPEG_BIN,
            '-loglevel', 'quiet',
            '-i', 'animations/__BasicBoot.mp4',
            '-f', 'image2pipe',
            '-pix_fmt', 'rgb24',
            '-vcodec', 'rawvideo', '-'
        ]
        self.proc = sp.Popen(command, stdout = sp.PIPE, bufsize=BYTES_PER_FRAME*2)

    # draw() will be run once per framedefdraw(self):
        raw_image = self.proc.stdout.read(BYTES_PER_FRAME)
        image = pygame.image.frombuffer(raw_image, (SCREEN_WIDTH, SCREEN_HEIGHT), 'RGB')
        self.proc.stdout.flush()
        self.surf.blit(image, (0, 0))


Post a Comment for "Playing Videos In Pygame 2"