在 PyGame 2 中播放视频 [英] Playing Videos in PyGame 2

查看:36
本文介绍了在 PyGame 2 中播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pygame 1.x 有一个电影模块,可以让播放电影变得非常简单.互联网上充满了 this SO question中提供的答案的变体

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

但是,我使用的是 Pygame 2,并且电影模块似乎已被删除.或者也许只是还没有实施?我在 current docs 中找不到对它的引用,也没有任何在线示例.

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.

我是 Python 生态系统的新手,不了解所有的角落和缝隙以及惯用的工具.我希望有人能指出我正确的方向.谢谢!

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!

推荐答案

感谢 pygame 不和谐这个示例 将 FFMPEG 与子进程一起使用,我有一个可行的解决方案.希望这会在将来对某人有所帮助.

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))


这篇关于在 PyGame 2 中播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆