[英]Cannot interupt a video clip preview from moviepy in a pygame display using a MOUSEBUTTONDOWN
在前幾周,我一直在開發一款使用 pygame 和 moviepy 的井字游戲。 moviepy 用於介紹屏幕視頻,供用戶單擊並顯示井字游戲板,但movie py 不會對鼠標單擊做出反應。
以下是相關代碼:
def introscreen():
while True:
vid.preview()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
vid.close()
maingame()
然而,視頻不斷重復,並且對 MOUSEBUTTONDOWN 沒有反應。
.preview()
阻止代碼,它不能運行檢查event
的代碼。
But if it would run it then other problem could be that event
checks mouse click only inside pygame window but preview
uses OpenCV
to display own window (probably created with PyQt
)
您可以直接使用OpenCV
讀取視頻,調整大小,轉換為 PyGame 的表面,並在 PyGame 的 window 中顯示 - 然后您可以使用event
停止/關閉它。
我在評論中保留了一些代碼,因為它可以用於創建更復雜的視頻 - 即。 您可以直接在視頻上顯示一些元素。
import pygame
import cv2
video = cv2.VideoCapture("BigBuckBunny.mp4")
#w = video.get(cv2.CAP_PROP_FRAME_WIDTH)
#h = video.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = video.get(cv2.CAP_PROP_FPS) # video's speed (Frames Per Second)
pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
running = False
# read single frame from video (as `numpy.array`)
ret, frame = video.read()
if ret: # check `return status` because it may have problem to read frame
#print(frame.shape)
# cv2 keeps image as `BGR` and it needs to convert to `RGB`
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# it gives first `height`, next `width`
#h, w = frame.shape[:2]
# it needs first `width`, next `height`
#image = pygame.image.frombuffer(frame, (w, h), "RGB")
# resize to pygame's window
frame = cv2.resize(frame, (800, 600))
# convert frame to PyGame surface
# cv2 keeps image as `BGR` and it needs to convert to `RGB`
image = pygame.image.frombuffer(frame, (800, 600), "BGR")
# display it in PyGame's window
screen.blit(image, (0,0))
# slow down to correct speed
clock.tick(fps)
pygame.display.flip()
# --- end ---
pygame.quit()
#exit()
用視頻大巴克兔子測試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.