How to listen to and decode an RTMP video stream with Python

Question:

How can I listen to and decode an RTMP video stream with Python? Such as with the ffmpeg command

ffmpeg -listen 1 -i rtmp://localhost:1935/live/app
I tried this:

import av
container = av.open('rtmp://localhost:1935/live/app',listen=True)
for frame in container.decode(video=0):
    frame = frame.to_ndarray(format='bgr24')
    ...do some thing with the frame...

But I got an error:

open() got an unexpected keyword argument ‘listen’

Asked By: Li-GanMa

||

Answers:

PyAV correct syntax is:

container = av.open('rtmp://localhost:1935/live/app', options={'listen': '1'})

av.open accepts an options dictionary argument.
In the code, 'listen' is the key and '1' is the value.
This is the equivalent to -listen 1 argument of FFmpeg CLI.

Answered By: Rotem
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.