如何捕获 SRT 流?
我已经建立了从 Raspberry Pi 到我的 PC 的 SRT Stream。 一切正常。我还可以使用 VLC 观看直播。 现在我想用Python捕获流。
对于 RTMP 和 HTTP,该命令
cap = cv2.VideoCapture("srt://IP:PORT")
frame = cap.read()
可以正常工作。
但对于 SRT,OpenCV 甚至不会尝试捕获流。
我能做什么?
编辑:
有关我的项目的更多信息: 我有一个 USB 摄像头与我的 Raspberry Pi 连接,并通过 SRT 传输此实时视频。我从 Haivision 安装了 SRT,并使用以下命令创建了流:
./srt-live-transmit udp://:1234 srt://:4201 -v
UDP 路径是 USB 摄像头的输入。 通过 ffplay 在 Raspberry Pi 上本地观看流。 正如我之前所说,在我的电脑上使用视频也是可行的。 使用OBS,到目前为止,我还无法开始直播。
I have established an SRT Stream from a Raspberry Pi to my PC.
Everything works fine. I am also able to watch the stream with VLC.
Now I want to capture the stream in Python.
With RTMP and HTTP the command
cap = cv2.VideoCapture("srt://IP:PORT")
frame = cap.read()
worked without any problem.
But with SRT, OpenCV does not even try to capture the stream.
What could I do?
Edit:
Some more information about my project:
I have a USB Camera connected with my Raspberry Pi and stream this live video over SRT. I installed SRT from Haivision and created the stream with the following command:
./srt-live-transmit udp://:1234 srt://:4201 -v
The UDP path is the input of the USB camera.
To watch the stream locally on the Raspberry Pi over ffplay works.
As I said before, to consume the Video on my PC also worked.
With OBS, until now, I was not able to start the stream.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近也遇到了类似的情况,看来原则上是可以的。
我已经能够使用 OpenCV 从 Python 读取本地 SRT 流中的帧,并进行以下设置:
Python 代码(关键部分是端口后面的
?mode=listener
)通过 ffmpeg 从文件流式传输
为什么我说“原则上”?这是因为它需要在流媒体和接收端都有一个与 SRT 兼容的 ffmpeg 版本(据我所知,它应该 >= 4.4)(在我的例子中,OpenCV 在后台使用 ffmpeg 进行视频捕获)。
幸运的是,在 M1 Mac Air 上的初始测试期间,我的管道的两个部分都是一致的:
当我尝试在 Ubuntu 18.04 机器上运行相同的管道时 - 流式传输和接收都不可能:
流式传输端
接收端
在创建视频捕获后立即返回(与“正确”的情况不同,在与流传输端建立连接之前它会阻塞),并尝试从中读取()返回
ret=False
结论
如果您可以确保这两个部分都有最新的 ffmpeg 版本(可能通过从源代码构建它们) - 这是可能的,但实际上这可能具有挑战性。
I have recently encountered a similar situation and it seems that it is possible in principle.
I've been able to read frames from a local SRT stream using OpenCV from Python with a following setup:
Python code (crucial part was
?mode=listener
after the port)Streaming from a file via ffmpeg
Why do I say "in principle"? That's because it requires to have a version of ffmpeg that is compatible with SRT (afaik it should be >= 4.4) both on the streaming and the receiving side (in my case OpenCV was using ffmpeg under the hood for video capture).
Luckily enough both parts of my pipeline were conformant during the initial testing on M1 Mac Air:
When I tried to run the same on a Ubuntu 18.04 machine - neither streaming nor receiving were possible:
Streaming side
Receiving side
Immediately returns upon creating a video capture (unlike a "proper" case where it blocks until connection with a streaming side is established), and attempts to read() from it return
ret=False
Conclusion
If you could ensure that both parts have a recent ffmpeg version (potentially by building them from source) - it is possible, but in practice it might be challenging.
共有三种方式:
1.使用 RTSP 代替 SRT
我找不到任何关于如何在python中解码SRT的数据,并且在Haivision中有一个唯一的官方库和C的示例代码 他们的 GitHub 存储库,因此最简单的方法是更改 RPI 的视频流协议。
有关于如何流式传输的很好的示例说明来自 RPI 摄像头的 RTSP 视频。。
优点:容易做。
缺点:如果有要求,则不是 SRT。
2.使用 SRT 而不是 Python
有记录的代码示例用于创建 SRT 客户端脚本,但它们是用 C 语言编写的,我没有找到有关 python 库或 SRT 示例代码的任何数据。但是 C 语言中有一个很好的例子: 来自 Git 的示例海威视
另一方面,OpenCV也是一个基于C的库。这样,你就可以开始学习C语言了。
优点:Haivision 的 OpenCV 和 SRT 是基于 C 的,所以使用 C 更好。
缺点:C语言很难学。
3.开发 Python 的 C 包装器
如果您有朋友可以用 C 语言进行开发,那么可以为 python 创建一个客户端库甚至客户端 API。
容易
优点:它更 你。
缺点:这对你的朋友来说并不容易。
There are three ways:
1. Use RTSP instead of SRT
I couldn`t find any data on how to decode SRT in python, and there is an only official library and example codes for C in Haivision in their GitHub repo, so the easiest way is to change the video streaming protocol for RPI.
There is good example instruction on how to stream video in RTSP from RPI camera..
Pros: Easy to do.
Cons: Not SRT if it is a requirement.
2. Use SRT but not Python
There are documented examples of code to create an SRT client script, but they are in C and I did not find any data about the python library or example code for SRT. But there is a good example in C: example from Git Haivision
On the other hand, OpenCV is also a C-based library. As a result, you may start to learn the C language.
Pros: OpenCV and SRT by Haivision are C-based, so it is even better to use C.
Cons: C is hard to learn.
3. Develop C wrapper for Python
If you have a friend that can develop in C, then create a client library or even client API for python.
There is how C wrappers work.
Pros: it is easier for you.
Cons: it is not easy for your friend.