python - 你如何在 Windows 10 上的 pygame 1.9.5 中播放视频
问题描述
我正在制作游戏,但在 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))
我希望它可以工作,但它没有也无法播放视频
解决方案
PyGame 1.9.5 没有显示电影的模块(至少在文档中没有)可能是因为它不完整,他们尝试将代码从库 SDL 1.2 转换为没有电影模块的 SDL 2.0。
你可以尝试cv2
使用pygame
此代码直接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()
推荐阅读
- python - 地图必须至少有 2 个参数
- azure - 是否可以在 Azure DevOps 中明确将 task.status 设置为 Succeeded?
- typescript - vue 3属性'...'在类型上不存在
- javascript - 如何在 typescript/javascript 中创建类数组
- javascript - 如何通过在angular8动态表单中刷新页面来选择下拉列表和日期?
- laravel - Laravel Jetstream 添加谷歌分析
- python - 如何使用 tkinter 中的按钮将值从一个函数返回到另一个函数
- javascript - 卷曲到 Axios 请求
- datagrid - 材质 UI 数据网格粘页眉
- python-3.x - 如何使用opencv python从图像中删除背景颜色而不影响文本