Skip to content Skip to sidebar Skip to footer

How Would I Go About Playing A Video Stream With Ffpyplayer?

First time poster here, so go easy on me. I'm working on a fun little project for myself and friends, basically I want to be able to stream and recieve video using ffmpeg, as a sor

Solution 1:

You are only extracting frames from video.mp4 in your code.

test = MediaPlayer("tcp://127.0.0.1:1234?listen")
while True:
    test.get_frame()
    iftest == "eof":
        break

Now, you need to display them using some third-party library since ffpyplayer doesn't provide any inbuilt feature to display frames in a loop.

Below code uses OpenCV to display extracted frames. Install OpenCV and numpy using below command

pip3 install numpy opencv-python

Change your receiver code to

from ffpyplayer.player import MediaPlayer
import numpy as np
import cv2

player = MediaPlayer("tcp://127.0.0.1:1234?listen")
val = ''whileval != 'eof':
    frame, val = player.get_frame()
    ifval != 'eof' and frame is not None:
        img, t = frame
        w = img.get_size()[0] 
        h = img.get_size()[1]
        arr = np.uint8(np.asarray(list(img.to_bytearray()[0])).reshape(h,w,3)) # h - height of frame, w - width of frame, 3 - number of channels in frame
        cv2.imshow('test', arr)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

you can also run ffplay command directly using python subprocess

Post a Comment for "How Would I Go About Playing A Video Stream With Ffpyplayer?"