鱼C论坛

 找回密码
 立即注册
查看: 2048|回复: 3

[已解决]求问Pygame.Movie的例子

[复制链接]
发表于 2023-4-3 18:26:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
https://fishc.com.cn/thread-62180-1-1.html
给几个例子呗,比如我有一个名叫movie.mp4的视频(同一目录),如何播放?
ps:如果不想买,可以发个10鱼币的附件,我来买你的附件
最佳答案
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()
[/b]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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中播放。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

1. 如何获取视频的fps?
2. pip install pygame -i 阿里云源 是基本安装,还是完整版本?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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()
[/b]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-24 01:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表