pythonffmpegrtmppyav

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


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'


Solution

  • 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.