ファイル
nizika_broadcast/play_movie.py
T

45 行
1.2 KiB
Python

import cv2
import pygame
import sys
def main (
screen: pygame.Surface,
video_path: str,
) -> None:
# OpenCV で動画を読み込む
cap = cv2.VideoCapture (video_path)
if not cap.isOpened ():
return
# screen の幅、高さ
(width, height) = screen.get_size ()
fps = cap.get (cv2.CAP_PROP_FPS)
clock = pygame.time.Clock ()
while cap.isOpened ():
# 動画のフレームを読み込む
(ret, frame) = cap.read ()
if not ret:
break
# OpenCV の BGR フォーマットを RGB に変換
frame = cv2.cvtColor (frame, cv2.COLOR_BGR2RGB)
# Numpy 配列を Pygame サーフェスに変換
frame_surface = pygame.surfarray.make_surface (frame)
frame_surface = pygame.transform.rotate (frame_surface, -90)
frame_surface = pygame.transform.flip (frame_surface, True, False)
frame_surface = pygame.transform.scale (frame_surface, (width, height))
# フレームを描画
screen.blit (frame_surface, (0, 0))
pygame.display.update ()
# FPS に応じて待機
clock.tick (fps)
cap.release ()