Python pipe ffmpeg stream to numpy in realtime

Viewed 857

im trying to get the frames out of a rtp stream via ffmpeg in realtime in python. At my server the frames are piped from numpy arrays into ffmpeg and should be at the client piped back to numpy for further manipulation. The server creates a sdp file which is given as input to the client. It looks like this:

v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
a=tool:libavformat 58.42.102
m=video 5004 RTP/AVP 96
b=AS:19000
a=rtpmap:96 H265/90000

With ffplay or VLC the stream can be received but with my python code not, at first needs ca. 5sec for each frame and the image is just some random coloured pixels. The client:

dimension = '{}x{}'.format(480, 240)
sdf_file = "video.sdp"

command = ['FFMPEG',
           '-protocol_whitelist', 'udp,rtp,file,pipe,crypto,data',
           '-i', sdf_file,
           '-pix_fmt', 'bgr24',
           '-r', '24',
           '-video_size', dimension,
           '-f', 'image2pipe', '-']

size = 480 * 240 * 3
proc = sp.Popen(command, stdout=sp.PIPE)

while True:
    try:
        tic = time.perf_counter()
        frame = proc.stdout.read(size)
        print(frame)
        if frame is None:
            print('no input')
            break
        image = np.frombuffer(frame, dtype='uint8').reshape(240, 480, 3)
        toc = time.perf_counter()
        print(f"performed calc in {(toc - tic) * 1000:0.4f} miliseconds")
        cv2.imshow('received', image)
    except Exception as e:
        print(e)

cv2.destroyAllWindows()
proc.stdin.close()
proc.wait()

Any ideas, how i can get the frames in realtime? Thanky for your help

0 Answers
Related