LoginSignup
3
2

More than 5 years have passed since last update.

ffmpeg + pygameで動画を表示する

Last updated at Posted at 2019-02-08
  • ストリーミング動画のプレイヤーにちょっとしたUIがついたものをつくりたい
  • WebRTCをRaspberryPiやPINE64のブラウザ使うのは厳しかったので、代替案を探している
  • ffplayは割と普通に動いているのでネイティブに近いやつなら大丈夫かも
  • ffmpegでデコードしてpygame(中身はSDL)で表示させてみることにした

コード

video_player.py
import pygame
import subprocess as sp
import numpy as np

cmd = ["ffmpeg", "-i", "kemono_960x540.mp4", "-loglevel", "quiet", "-an", "-r", "10", "-pix_fmt", "rgb24", "-f", "image2pipe", "-vcodec", "rawvideo", "-"]
pipe = sp.Popen(cmd, stdin = sp.PIPE, stdout = sp.PIPE)

pygame.init()
display = pygame.display.set_mode((960, 540))
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    row_img = pipe.stdout.read(960*540*3)
    img = np.frombuffer(row_img, dtype='uint8').reshape((540,960,3))
    img = np.swapaxes(img, 0, 1)
    #print(img);break
    surf = pygame.surfarray.make_surface(img)
    display.blit(surf, (0, 0))
    pygame.display.update()
pygame.quit()

動かす

とりあえずmacOSで。

$ brew install ffmpeg
$ pipenv install numpy
$ pipenv install pygame
$ pipenv run python video_player.py

スクリーンショット 2019-02-08 9.21.07.png

参考

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2