问题
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 Pygame 2, and it appears that the movie module has been removed. Or maybe just not implemented yet? I can't find a reference to it in the current docs, nor any examples online.
I found this example of using pygame.Overlay with pymedia, but it appears that pymedia does not run on Python 3.
I'm new to the Python ecosystem and don't know all the nooks and crannies and idiomatic tools. I'm hoping someone might be able to point me in the right direction. Thank you!
回答1:
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 * 3
class AnimationFullScreenView(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 frame
def draw(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))
来源:https://stackoverflow.com/questions/58980351/playing-videos-in-pygame-2