如何在 C++ 中解析 MJPEG HTTP 流?
我需要访问和读取从网络摄像机发送实时 MJPEG 镜头的 http 流,以便对图像进行一些 opencv 图像处理。
我可以通过 VLC 访问摄像机的镜头,或者只需在 Chrome 或 Firefox 中访问 URL 即可。但是,当服务器只是发送连续的提要时,如何以编程方式访问 http 服务器并分离每个帧?
数据似乎被简单地格式化,在 HTTP 标头和 JPEG 数据之间循环。我能想到的解决这个问题的唯一方法是以某种方式向服务器发送请求,解析传入的数据,并将标头与实际的 jpeg 数据分开,然后将其传递给 opencv。
然而,这听起来非常复杂,我不太确定从哪里开始。你们知道是否有任何库,或者只是我忽略的更简单的方法,可以使这一切变得更容易?
多谢
I need to access and read an http stream which is sending live MJPEG footage from a network camera, in order to do some opencv image processing on the image.
I can access the camera's footage through VLC, or simply by going to the URL in chrome or firefox. But how can I programmatically access the http server and separate each frame, when the server is just sending a continuous feed?
The data seems to be simply formatted, looping between the HTTP Header and JPEG data. The only way I can think of approaching this is somehow sending a request to the server, parsing the data as it comes in, and separating the header from the actual jpeg data, and, in turn, passing that to opencv.
However, that sounds awfully convoluted and I'm not quite sure where I'd start. Do you guys know if there are any libraries out there, or just a simpler approach I'm overlooking, that could make all this easier?
Thanks alot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 HTTP 下载,您可以使用 Libcurl 库。
AFAIK MJPEG 格式不是标准化格式。其实际字节格式因实现而异。但基本上只是 jpeg 文件与分隔符的串联。如果您使用十六进制编辑器查看字节,您可以轻松地区分每个 jpeg 文件。
例如,ffmpeg 的 mjpeg 输出结构如下:
For HTTP download, you can use Libcurl library.
AFAIK MJPEG format is not a standardized format. Its actual byte format vary by implementations. But basically just concatenation of jpeg file with delimiters. If you look at bytes with a hex editor you could easily distinguish each jpeg file.
For example, ffmpeg's mjpeg output is structured like below:
在此页面中:
http:// /thistleshrub.net/www/index.php?controller=posts&action=show&id=2012-05-13DisplayingStreamedMJPEGinJava.txt
用 Java 解析 MJPEG 流,我在 Java 中以完美的结果实现了这一点。
如果你尝试使用 C++,你会发现缺少一些东西:套接字连接和渲染画布,libcurl 似乎是 http 请求的一个不错的选择,但仍然缺少画布,你可以使用 GLUT 或 Qt 之类的东西。
我在一些论坛上读到 OpenCV 可以读取 MJPEG Streamer 类型的输入流,但似乎它们需要是 OpenCV 的最新版本(从头开始编译 OpenCV 很困难)。
我希望这有帮助。
In this page:
http://thistleshrub.net/www/index.php?controller=posts&action=show&id=2012-05-13DisplayingStreamedMJPEGinJava.txt
Parse a MJPEG Stream with Java, I implemented this with flawlessly results in Java.
If you try to use with C++ you find some things missed: socket conection and render canvas, libcurl seems to be a good option to http request, but still missing the canvas, you can use something like GLUT or Qt.
I read in some forums that OpenCV can read input stream of type MJPEG Streamer, but seems they need to be the recent version of OpenCV (compile OpenCV from scratch it's hard).
I hope this help.