How to parse MJPEG HTTP Stream within C++?

jonas picture jonas · Jan 26, 2011 · Viewed 8.4k times · Source

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

Answer

9dan picture 9dan · Jan 26, 2011

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:

0xff 0xd8 // start of jpeg
{ ... }   // jpeg body
0xff 0xd9 // end of jpeg
...
0xff 0xd8 // start of jpeg
{ ... }   // jpeg body
0xff 0xd9 // end of jpeg
...