开发者

Extract Jpg from Mjpeg

Trying to extract jpg from Mjpeg which return from an Axis IPCam.

From the document, Axis IPCam will return HttpResponse which contain the Mjpeg.

Here , I use this code and I have no idea to proceed futher.

HttpWebRequest webrequest = null;
HttpWebResponse webresponse = null;  
webrequest = (HttpWebRequest)WebRequest.Create(uri);
webresponse = (HttpWebResponse)webrequest.GetResponse();

StreamReader sr = new StreamReader(
    webresponse.GetResponseStream().Encoding.ASCII);
string result = sr.ReadToEnd();

The Mjpeg in the HttpResponse will look like tis:

--myboundary
Content-Type: image/jpeg
Content-Length: 3159
(BINARY)  <-----------This is Jpeg 
--myboundary

Content-Type: image/jpeg
Content-Length: 316开发者_开发技巧2
(BINARY)
--myboundary
Content-Type: image/jpeg
Content-Length: 3151
(BINARY)

Question:

  1. Can I use Encoding.Ascii for the for the return Httpresponse Stream? This way all the byte will in string form ?

  2. From (1), How do I get the (binary out) by parsing?

I hope I can get all the help I can get here.

Thanks


Parsing Mjpeg streams manually could be cumbersome and error prone. That's why there's an excellent library designed to do exactly this. I would recommend you looking at aforge which is capable, among other things, to handle Mjpeg streams. Download the library, add reference to AForge.Video.dll into your project and enjoy.

Here's a sample that captures a single frame from an Axis camera source:

class Program
{
    static void Main(string[] args)
    {
        MJPEGStream stream = new MJPEGStream("http://146.176.65.10/axis-cgi/mjpg/video.cgi");
        stream.NewFrame += (sender, e) =>
        {
            e.Frame.Save("test.jpg", ImageFormat.Jpeg);
            Console.WriteLine("frame saved into test.jpg");
            // stop capturing frames
            ((MJPEGStream)sender).Stop();
        };
        // start capturing frames
        stream.Start();
        Console.ReadLine();
    }
}


Try this:

int i = 0;
foreach (Match m in Regex.Matches(input, 
         @"Content-Length:.*?\r\n\r\n(?<base64>[\s\S]*)\r\n\r\n"))
{
    byte[] data = Convert.FromBase64String(m.Groups["base64"].Value);
    //File.WriteAllBytes(String.Format(@"c:\image{0}.jpg", i++), data);
}


There appears to be gstreamer bindings called gstreamer-sharp for C#. You may want to look at that. I could convert the command below to C code. C# API should be similar.

Are you trying to save images directly using command line? If so, use gstreamer tools from command line to save series of jpeg images.

gst-launch souphttpsrc location="http://[ip]:[port]/[dir]/xxx.cgi" do-timestamp=true is_live=true ! multipartdemux ! jpegdec !  videoflip method=vertical-flip ! jpegenc !  multifilesink location=image-out-%05d.jpg
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜