伊地知ニジカ放送局だぬ゛ん゛. https://www.youtube.com/@deerjika
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

  1. import cv2
  2. import pygame
  3. import sys
  4. def main (
  5. screen: pygame.Surface,
  6. video_path: str,
  7. ) -> None:
  8. # OpenCV で動画を読み込む
  9. cap = cv2.VideoCapture (video_path)
  10. if not cap.isOpened ():
  11. return
  12. # screen の幅、高さ
  13. (width, height) = screen.get_size ()
  14. fps = cap.get (cv2.CAP_PROP_FPS)
  15. clock = pygame.time.Clock ()
  16. while cap.isOpened ():
  17. # 動画のフレームを読み込む
  18. (ret, frame) = cap.read ()
  19. if not ret:
  20. break
  21. # OpenCV の BGR フォーマットを RGB に変換
  22. frame = cv2.cvtColor (frame, cv2.COLOR_BGR2RGB)
  23. # Numpy 配列を Pygame サーフェスに変換
  24. frame_surface = pygame.surfarray.make_surface (frame)
  25. frame_surface = pygame.transform.rotate (frame_surface, -90)
  26. frame_surface = pygame.transform.flip (frame_surface, True, False)
  27. frame_surface = pygame.transform.scale (frame_surface, (width, height))
  28. # フレームを描画
  29. screen.blit (frame_surface, (0, 0))
  30. pygame.display.update ()
  31. # FPS に応じて待機
  32. clock.tick (fps)
  33. cap.release ()