python-3.x - 在 PyGame 2 中播放视频

标签 python-3.x pygame

<分区>

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

但是,我使用的是 Pygame 2,似乎电影模块已被删除。或者可能还没有实现?我在 current docs 中找不到对它的引用,也没有在线示例。

我找到了 this example将 pygame.Overlay 与 pymedia 结合使用,但 pymedia 似乎无法在 Python 3 上运行。

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

最佳答案

编辑,2021 年 11 月 9 日: - 我在下面的回答将完成工作,但还有一个更惯用的答案(即,更少的 hack)here


感谢 pygame discord 的人们和 this example将 FFMPEG 与子进程一起使用,我有一个可行的解决方案。希望这对将来的人有所帮助。

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


关于python-3.x - 在 PyGame 2 中播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58980351/

相关文章:

python - 为什么使用 int() 内置从 .txt 文件读取部分行时抛出 ValueError ?

python - 如何在 PyGame 中最大化显示屏幕?

python - 如何在Mac上轻松安装pygame Python模块

python - 如何使用 pygame.USEREVENT 让敌人定期开火

python - 导致无限循环的逻辑错误

python - 使用 Python 自动输入以暴力破解在 cli 中运行的程序

python - 使用迭代器的最快(最 Pythonic)方式

python - py2exe,可执行文件 : How to pack data into single file?

Python Pygame 力量

pygame - pygame.mask.set_at()第二个参数的值