你如何在 Windows 10 上的 pygame 1.9.5 中播放视频 [英] How do you play videos in pygame 1.9.5 on windows 10

查看:59
本文介绍了你如何在 Windows 10 上的 pygame 1.9.5 中播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm making a game and on windows i can't play a movie file. But in Ubuntu, i can play it. So how do you play a video in pygame 1.9.5 on windows.

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

i tried changing the pygame version, tried adding modules. But none of them works.

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))

我希望它能工作,但它没有也不能播放视频

i expect it to work, but it didn't and can't play a video

推荐答案

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

PyGame 1.9.5 doesn't have module to dispaly movie (at least not in documentation) maybe because it was incomplet and they try to convert code from library SDL 1.2 to SDL 2.0 which doesn't have module for movie.

您可以尝试将 cv2pygame

此代码从任何流(内置摄像头、本地文件、删除流)直接显示在屏幕上.窗口必须与流具有相同的大小.我不知道为什么,但我必须在显示之前转置图像.

This code display directly on screen from any stream (built-in camera, local file, remove stream). Window must have the same size as the stream. I don't know why but I had to transpose image before displaying.

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 从流中获取帧,因此窗口可能具有不同的大小.

This code use Surface to get frame from stream so window may have different size.

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()

这篇关于你如何在 Windows 10 上的 pygame 1.9.5 中播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆