This example allows you to stream frames using MediaMTX, which is a media server and proxy that allows you to publish/subscribe to media streams.
Install MediaMTX, you can either go with standalone binary release or docker image.
You will also need to install ffmpeg
library, as Python script uses it to forward encoded frames to the MediaMTX server.
# On Ubuntu 24.04 it should install 6.1.1:
sudo apt install ffmpeg
# On MacOs, should install 7.0.2:
brew install ffmpeg
# On Windows (within Admin PowerShell), should install 7.0.2:
choco install ffmpeg
# Or download from https://ffmpeg.org/download.html
On Ubuntu 22.04 we encountered that ffmpeg
is available up to only 4.2.2
with the default apt repo, which doesn't recognize the H264 stream correctly. After upgrading to Ubuntu 24.04, we were able to install ffmpeg==6.1.1
and the code works as expected.
First, run the MediaMTX server:
$ ./mediamtx
2024/08/21 15:26:08 INF MediaMTX v1.8.5
2024/08/21 15:26:08 INF configuration loaded from /Users/erik/Downloads/mediamtx_v1.8.5_darwin_arm64/mediamtx.yml
2024/08/21 15:26:08 INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
2024/08/21 15:26:08 INF [RTMP] listener opened on :1935
2024/08/21 15:26:08 INF [HLS] listener opened on :8888
2024/08/21 15:26:08 INF [WebRTC] listener opened on :8889 (HTTP), :8189 (ICE/UDP)
2024/08/21 15:26:08 INF [SRT] listener opened on :8890 (UDP)
Now let's run the main.py script, which will start publishing H264-encoded stream to the MediaMTX server.
python3 main.py
To see the streamed frames, use a RTSP Client (e.g. VLC Network Stream) with the following link
rtsp://localhost:8554/mystream
On Ubuntu or Mac OS, you can use ffplay
(part of the ffmpeg
library) to preview the stream, which will provide better performance than VLC (400ms latency vs >1sec latency).
ffplay -fflags nobuffer -fflags discardcorrupt -flags low_delay -framedrop rtsp://localhost:8554/mystream
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。