歌者文明清理员 发表于 2023-4-3 18:26:25

求问Pygame.Movie的例子

https://fishc.com.cn/thread-62180-1-1.html
给几个例子呗,比如我有一个名叫movie.mp4的视频(同一目录),如何播放?
ps:如果不想买,可以发个10鱼币的附件,我来买你的附件

isdkz 发表于 2023-4-3 18:30:50

以下是一个基本的Pygame.Movie的示例,可以用来播放名为"movie.mp4"的视频文件:

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Pygame Movie Demo")

# 加载视频文件
movie = pygame.movie.Movie("movie.mp4")

# 播放视频
movie.play()

# 循环显示视频直到用户关闭窗口
while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            movie.stop()
            pygame.quit()
            quit()

    screen.fill((0, 0, 0))

    # 检查视频是否结束,如果结束,则重复播放
    if not movie.get_busy():
      movie.rewind()
      movie.play()

    # 将当前视频帧绘制到屏幕上
    screen.blit(movie.get_surface(), (0, 0))

    pygame.display.update()


这个示例加载名为"movie.mp4"的视频文件并将其播放。在视频播放时,程序循环显示视频帧,直到用户关闭窗口。如果视频结束,程序将自动将其重复播放。

请注意,要使用pygame.movie.Movie类,您需要安装Pygame的完整版本,因为此功能不包含在Pygame基本安装中。另外,由于不同操作系统的支持情况不同,某些视频文件可能无法在Pygame中播放。

歌者文明清理员 发表于 2023-4-3 18:32:46

isdkz 发表于 2023-4-3 18:30
以下是一个基本的Pygame.Movie的示例,可以用来播放名为"movie.mp4"的视频文件:




1. 如何获取视频的fps?
2. pip install pygame -i 阿里云源 是基本安装,还是完整版本?

isdkz 发表于 2023-4-3 18:51:26

本帖最后由 isdkz 于 2023-4-3 18:54 编辑

歌者文明清理员 发表于 2023-4-3 18:32
1. 如何获取视频的fps?
2. pip install pygame -i 阿里云源 是基本安装,还是完整版本?

还是不要使用 pygame.movie了,这个已经弃用了

要在Pygame中播放视频,你可以使用以下几种方法:

- 使用pygamevideo模块,它可以帮助开发者将视频嵌入到Pygame显示中。
- 使用moviepy库,它需要Pygame,可以播放mp4文件。(最好使用这个)
- 使用pygame.movie.Movie()模块,它只支持mpg格式的文件。(在新版本中以已弃用)
- 使用cv2.VideoCapture()函数,它可以打开视频文件,但不播放音频。



- pygamevideo模块的例子:

import pygame
import pygamevideo

pygame.init()
screen = pygame.display.set_mode((800, 600))

video = pygamevideo.Video("video.mp4")
video.play()

while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            video.stop()
            pygame.quit()
            exit()

    screen.fill((0, 0, 0))
    video.render(screen)
    pygame.display.flip()


- moviepy库的例子:

import pygame
import moviepy.editor

pygame.init()
video = moviepy.editor.VideoFileClip("video.mp4")
video.preview()
pygame.quit()



- pygame.movie.Movie()模块的例子:

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))

movie = pygame.movie.Movie("video.mpg")
movie.play()

while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            movie.stop()
            pygame.quit()
            exit()

    screen.fill((0, 0, 0))
    movie.render(screen)
    pygame.display.flip()


- cv2.VideoCapture()函数的例子:

import cv2
import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))

video = cv2.VideoCapture("video.mp4")
fps = video.get(cv2.CAP_PROP_FPS)
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            video.release()
            pygame.quit()
            exit()

    screen.fill((0, 0, 0))
    ret, frame = video.read()
    if ret:
      frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
      frame = frame.swapaxes(0, 1)
      frame = pygame.surfarray.make_surface(frame)
      screen.blit(frame, (0, 0))
    else:
      break

    clock.tick(fps)
    pygame.display.flip()
页: [1]
查看完整版本: 求问Pygame.Movie的例子