调整 PyGame 视频

Resize PyGame Video

我有一个 240x160 的视频,我希望用户能够在最终游戏中将他们的分辨率更改为几个不同的尺寸(如源代码所示)。我在 docs 上读到,他们说视频应该自动调整大小,但根据我的经验,它没有。这是我的源代码:

import pygame

#Scales
GAMEBOY         = 1        #240x160  (Standard gameboy resolution)
FOUREIGHTYP     = 2        #480x320  (480p resolution)
SEVENTWENTYP    = 4        #960x640  (720p resolution)
TENEIGHTYP      = 6        #1440x960 (1080p resolution)

chosenScale = SEVENTWENTYP

resolution = (240*chosenScale, 160*chosenScale)

FPS = 60

movie1Counter = 0
movie2Counter = 1

movie1Playing = True
movie2Playing = False
playedEarly = False
earlyCounter = 0

keypressed = False

pygame.init()

pygame.mixer.quit()

clock = pygame.time.Clock()

movie = pygame.movie.Movie('./intromovie1.mpg')

screen = pygame.display.set_mode(movie.get_size())

movie_screen = pygame.Surface(movie.get_size()).convert()

movie.set_display(movie_screen)

movie.play()
movie.set_volume(0.5)

playing = True
while playing:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            movie1.stop()
            playing = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                if movie1Playing:
                    movie.stop()
                    playedEarly = True
                    movie1Playing = False
                    movie2Playing = True
                    movie = pygame.movie.Movie('./intromovie2.mpg')
                    movie = pygame.movie.Movie('./intromovie2.mpg')
                    movie_screen = pygame.Surface(movie.get_size()).convert()
                    movie.set_display(movie_screen)
                    movie.play()
                    movie.set_volume(0)
        if event.type == pygame.KEYUP:
            pass

    if movie.get_busy() == 0:
        if movie1Playing == True:
            movie.stop()
            movie1Playing = False
            movie2Playing = True
            movie = pygame.movie.Movie('./intromovie2.mpg')
            movie = pygame.movie.Movie('./intromovie2.mpg')
            movie_screen = pygame.Surface(movie.get_size()).convert()
            movie.set_display(movie_screen)
            movie.play()
            movie.set_volume(0.5)
        else:
            movie.stop()
            movie1Playing = True
            movie2Playing = False
            movie = pygame.movie.Movie('./intromovie1.mpg')
            movie = pygame.movie.Movie('./intromovie1.mpg')
            movie_screen = pygame.Surface(movie.get_size())
            movie.set_display(movie_screen)
            movie.play()
            movie.set_volume(0.5)

    if playedEarly:
        earlyCounter += 1
        if earlyCounter > 67:
            movie.set_volume(0.5)
            playedEarly = False
            earlyCounter = 0

    screen.blit(movie_screen,(0,0))
    pygame.display.update()
    clock.tick(FPS)

pygame.quit()

抱歉篇幅太长了,我是 PyGame 的新手,我不确定你们需要什么来解决这个问题。此外,我已经尝试更改屏幕和 movie_screen,但是如果我将 window 的分辨率设置为比视频大,那么它就会位于左上角。

提前致谢!

对于那些想知道如何实现这一点的人,与其在 pygame 中调整 window 的大小,另一种方法是让不同的视频适合您的分辨率。如果你安装了 ffmpeg 那么这真的很容易。这是我用来解决问题的命令:

ffmpeg -i infile -target ntsc-vcd -vcodec mpeg1video -s WIDTHxHEIGHT -sws_flags neighbor outfile
  - Where infile is the file (including the extension) you converting with
  - Where WIDTHxHEIGHT is the desired resoution of the outfile (ex: 480x320)
  - Where outfile is the file (including the extension) that is generated

请注意,如果您尝试生成的视频需要高比特率,那么您将在 ffmpeg 中收到大量 "error" 消息,但您无需担心这一点为了 pygame 的目的。尽管有很多错误 pygame 似乎可以完美地播放这些文件,但是您的标准媒体播放器在出现大量帧丢失时效果不佳。

希望这对您有所帮助,编码愉快!