在 C# 中创建 MJPEG 视频流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/216664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an MJPEG video stream in c#
提问by typemismatch
I have images being sent to my database from a remote video source at about 5 frames per second as JPEG images. I am trying to figure out how to get those images into a video format so I can stream a live video feed to Silverlight.
我有图像从远程视频源以每秒 5 帧的速度作为 JPEG 图像发送到我的数据库。我想弄清楚如何将这些图像转换为视频格式,以便我可以将实时视频源流式传输到 Silverlight。
It seems to make sense to create a MJPEG stream but I'm having a few problems. Firstly I was trying to stream via an HTTP request so I didn't have a deal with sockets but maybe this is breaking my code.
创建 MJPEG 流似乎很有意义,但我遇到了一些问题。首先,我试图通过 HTTP 请求进行流式传输,所以我没有处理套接字,但这可能会破坏我的代码。
If I try surf to my stream from QT I get a video error, Media player shows the first frame image and Silverlight crashes :)
如果我尝试从 QT 浏览我的流,我会收到一个视频错误,媒体播放器显示第一帧图像并且 Silverlight 崩溃:)
Here is the code that streams - since I content type used this way can only be sent once I know that it isn't ideal and might be the root cause. All images are coming in via a LINQ2SQL object.
这是流式传输的代码 - 因为我以这种方式使用的内容类型只能在我知道它不理想并且可能是根本原因后才发送。所有图像都是通过 LINQ2SQL 对象传入的。
I did already try simply updating the image source of an image control in Silverlight but the flicker isn't acceptable. If Silverlight doesn't support MJPEG then no point even continuing but it looks like it does. I do have access to the h.264 frames coming in but that seemed more complicated via MP4.
我已经尝试在 Silverlight 中简单地更新图像控件的图像源,但闪烁是不可接受的。如果 Silverlight 不支持 MJPEG,那么即使继续也没有意义,但看起来确实如此。我确实可以访问传入的 h.264 帧,但通过 MP4 看起来更复杂。
Response.Clear();
Response.ContentType = "multipart/x-mixed-replace; boundary=--myboundary";
ASCIIEncoding ae = new ASCIIEncoding();
HCData data = new HCData();
var videos = (from v in data.Videos
select v).Take(50); // sample the first 50 frames
foreach (Video frame in videos)
{
byte[] boundary = ae.GetBytes("\r\n--myboundary\r\nContent-Type: image/jpeg\r\nContent-Length:" + frame.VideoData.ToArray().Length + "\r\n\r\n");
var mem = new MemoryStream(boundary);
mem.WriteTo(Response.OutputStream);
mem = new MemoryStream(frame.VideoData.ToArray());
mem.WriteTo(Response.OutputStream);
Response.Flush();
Thread.Sleep(200);
}
Thanks!
谢谢!
EDIT: I have the stream working in firefox so if I surf to the page I see video! but nothing else accepts the format. Not IE, SL, Media player - nothing.
编辑:我的流在 Firefox 中工作,所以如果我浏览到该页面,我会看到视频!但没有其他东西接受这种格式。不是 IE、SL、媒体播放器 - 什么都没有。
回答by dicroce
First, write your mjpeg frames out to separate files. You should then be able to open these in Phototshop (this will independently verify that you are parsing the stream correctly). If this fails, by bet is that you have HTTP headers embedded in your image data.
首先,将您的 mjpeg 帧写入单独的文件。然后,您应该能够在 Phototshop 中打开这些文件(这将独立验证您是否正确解析了流)。如果这失败了,可以打赌您的图像数据中嵌入了 HTTP 标头。
回答by John Dyer
Have you looked at various web cam setups that exist on the net? A lot of them do some sort of low res update without flicker. You should be able to reverse engineer these types of sites for additional clues to your problem.
您是否查看过网络上存在的各种网络摄像头设置?他们中的很多人都会进行某种低分辨率更新而不会出现闪烁。您应该能够对这些类型的网站进行逆向工程,以获得解决您问题的其他线索。
Some sites create a GIF animation, maybe that is an option so that the user can see the past minute or so.
一些网站会创建 GIF 动画,也许这是一个选项,以便用户可以看到过去一分钟左右。
回答by csgero
I'm far from being an expert in MJPEG streaming, but looking at the source of mjpg-streameron sourcefourge I think you should send each frame separately, writing the boundary before and after each of them. You should of course not write the content-type in the closing boundary.
我远不是 MJPEG 流媒体的专家,但是在 sourcefourge上查看 mjpg-streamer的来源,我认为您应该单独发送每个帧,在每个帧之前和之后写下边界。您当然不应该在结束边界中写入内容类型。
回答by dviljoen
I did MJPEG a long time ago (3-4 years ago) and I'm scratching my head trying to remember the details and I simply can't. But, if its possible, I would suggest finding some kind of web site that streams MJPEG content and fire up wireshark/ethereal and see what you get over the wire. My guess is you are missing some required HTTP headers that firefox is little more forgiving about.
我很久以前(3-4 年前)做过 MJPEG,我正在挠头试图记住细节,但我就是做不到。但是,如果可能的话,我会建议找到某种可以流式传输 MJPEG 内容并启动wireshark/ethereal 的网站,然后看看您通过网络获得了什么。我的猜测是您缺少一些 Firefox 更宽容的必需 HTTP 标头。
If you can't find a sample MJPEG stream over the internet, a lot of web cams have software that give you an MJPEG stream. The app I worked on it with was a console for multiple security cameras, so I know that is a common implementation for cams of all types (if they support a web interface).
如果您在 Internet 上找不到示例 MJPEG 流,许多网络摄像头都有软件可以为您提供 MJPEG 流。我使用的应用程序是一个用于多个安全摄像头的控制台,所以我知道这是所有类型摄像头的常见实现(如果它们支持 Web 界面)。
回答by QueueHammer
About your edit: MJPEG is supported by Firefox and Safari. However other applications do not, like Explorer or Silverlight depending on what you are doing with it.
关于您的编辑:Firefox 和 Safari 支持 MJPEG。但是其他应用程序不会,例如 Explorer 或 Silverlight,具体取决于您使用它做什么。