调整PyGame视频大小

时间:2017-03-21 06:01:16

标签: python video resize pygame

我有一个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但是如果我将窗口的分辨率设置为比视频大,那么它只是位于左上角。

提前致谢!

1 个答案:

答案 0 :(得分:0)

对于那些想知道如何实现这一点的人而言,不是在pygame中调整窗口大小,另一种方法是使用适合您分辨率的不同视频。如果您安装了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中传播的消息,但是你不必为了pygame的目的而担心这个问题。尽管有很多错误,pygame似乎可以完美地播放这些文件,但是你的标准媒体播放器不会出现大量帧丢失。

希望这有帮助,快乐编码!