如何在Windows 10上的pygame 1.9.5中播放视频

时间:2019-04-06 04:26:13

标签: python pygame

我正在制作游戏,在Windows上我无法播放电影文件。但是在Ubuntu中,我可以玩。那么如何在Windows的pygame 1.9.5中播放视频。

我尝试更改pygame版本,尝试添加模块。但是它们都不起作用。

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

movie.set_display(movie_screen)
movie.play()


cnt = 0
playing = True
while playing:
    screen.fill( (0,0,0) )
    cnt+=1
    if cnt>=1875:
        cnt=0
        movie.rewind()
        movie.play()
    for event in pygame.event.get():
        if controlstart == True:
            if event.type == pygame.KEYDOWN:
                if event.key==pygame.K_KP_ENTER or 
                event.key==pygame.K_RETURN:
                    pygame.mixer.music.stop()
                    pygame.mixer.Channel(2).play(enter_sfx)
                    y += 1
                    fade(1280, 720)
                    xb = 0
                    yb = 0
                    if y == 3236:
                        controlstart = False
                    if y == 436:
                        movie.stop()
                        playing = False
                        pygame.quit()
                        quit()
                if event.key == pygame.K_UP:
                    pygame.mixer.Channel(3).play(move_sfx)
                    y += 1
                    if y == 3236:
                        y = 235
                        y1 = 3000
                    if y == 236:
                        y = 435
                        y1 = 3000
                    if y == 436:
                        y1 =335
                        y = 3235

                if event.key == pygame.K_DOWN:   
                    pygame.mixer.Channel(4).play(move_sfx)
                    y += 1
                    if y == 236:
                        y = 3235
                        y1 = 335
                    if y == 3236:
                        y1 = 3000
                        y = 435
                    if y == 436:
                        y1 = 3000
                        y = 235
        if event.type == pygame.QUIT:
            movie.stop()
            playing = False
            pygame.quit()
            quit()

    screen.blit(movie_screen,(0, 0))

我希望它能正常工作,但是不能,也不能播放视频

1 个答案:

答案 0 :(得分:0)

PyGame 1.9.5没有显示电影的模块(至少在文档中没有),可能是因为它不完整,他们试图将代码从库SDL 1.2转换为没有电影模块的SDL 2.0。 / p>

您可以尝试将cv2pygame一起使用

此代码可从任何流(内置相机,本地文件,删除流)直接显示在screen上。窗口的大小必须与流相同。我不知道为什么,但是在显示之前我必须转置图像。

import pygame
import cv2

# --- local (built-in) camera ---
#stream = 0

# --- local file ---
#stream = '2019-03-26_08-43-15.mkv'

# --- http stream ---
# doesn't work any more
#stream = 'http://media.dumpert.nl/tablet/9f7c6290_Verstappen_vs._Rosberg_with_Horner_Smile___Streamable.mp4.mp4.mp4'

# --- rtsp stream ---
#stream = 'rtsp://streaming1.osu.edu/media2/ufsap/ufsap.mov'

# --- rtmp stream ---
# Big Buck Bunny
stream = 'rtmp://184.72.239.149/vod/mp4:bigbuckbunny_1500.mp4'

# open stream
cap = cv2.VideoCapture(stream)

# read one frame and check if there was no problem
ret, img = cap.read()
if not ret:
    print("Can't read stream")
    #exit()

# transpose/rotate frame 
#img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
img = cv2.transpose(img)

# display its width, height, color_depth
print('shape:', img.shape)

pygame.init()

# create window with the same size as frame
screen = pygame.display.set_mode((img.shape[0], img.shape[1]))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # read one frame and check if there was no problem
    ret, img = cap.read()
    if not ret:
        running = False
        break
    else:
        # transpose/rotate frame
        #img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
        #img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
        img = cv2.transpose(img)

        # blit directly on screen         
        pygame.surfarray.blit_array(screen, img)

    pygame.display.flip()

pygame.quit()

此代码使用Surface从流中获取帧,因此窗口可能具有不同的大小。

import pygame
import cv2

# --- local (built-in) camera ---
#stream = 0

# --- local file ---
#stream = '2019-03-26_08-43-15.mkv'

# --- http stream ---
# doesn't work any more
#stream = 'http://media.dumpert.nl/tablet/9f7c6290_Verstappen_vs._Rosberg_with_Horner_Smile___Streamable.mp4.mp4.mp4'

# --- rtsp stream ---
#stream = 'rtsp://streaming1.osu.edu/media2/ufsap/ufsap.mov'

# --- rtmp stream ---
# Big Buck Bunny
stream = 'rtmp://184.72.239.149/vod/mp4:bigbuckbunny_1500.mp4'

cap = cv2.VideoCapture(stream)

ret, img = cap.read()
if not ret:
    print("Can't read stream")
    #exit()

#img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
img = cv2.transpose(img)
print('shape:', img.shape)

pygame.init()

screen = pygame.display.set_mode((800, 600))
surface = pygame.surface.Surface((img.shape[0], img.shape[1]))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    ret, img = cap.read()
    if not ret:
        running = False
        break
    else:
        #img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
        #img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
        img = cv2.transpose(img)

        pygame.surfarray.blit_array(surface, img)
        screen.blit(surface, (0,0))

    pygame.display.flip()

pygame.quit()