如何将 MJPEG 流保存到磁盘 (C# .NET)?
我有一个应用程序可以从相机(MJPEG)读取流并将其实时显示在表单上(在图片框中)。这是有效的。当用户单击“开始”按钮时,该流读取开始。
我想要做的是,当用户单击“停止”按钮时,“开始”按钮和“停止”按钮之间的流将作为 .mpg 保存在磁盘上。
现在,它在磁盘上写入了一些内容,但我无法在 Windows Media Player 中打开它。
这是编写流的代码
private void ReadWriteStream(byte[] buffer, int start, int lenght, Stream writeStream)
{
Stream readStream = new MemoryStream(buffer, start, lenght);
int bytesRead = readStream.Read(buffer, 0, m_readSize);
// write the required bytes
while (bytesRead > 0 && !m_bStopLecture)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, m_readSize);
}
readStream.Close();
}
这是调用该函数的地方。这是一个循环,正如我所说,视频正在 PictureBox 中播放。
// image at stop
Stream towrite = new MemoryStream(buffer, start, stop - start);
Image img = Image.FromStream(towrite);
imgSnapshot.Image = img;
// write to the stream
ReadWriteStream(buffer, start, stop - start, writeStream);
多谢!
I have an application that read the stream from a camera (MJPEG) and show it on the form in real time (in a picture box). This is working. This stream reading start when the user click on the button "Start".
What I want to do is that when the user click on a button "Stop", the stream between the button "Start" and "Stop" would be save on disk as a .mpg.
Right now, it write something on the disk, but I can't open it in Windows Media Player.
Here is the code to write the stream
private void ReadWriteStream(byte[] buffer, int start, int lenght, Stream writeStream)
{
Stream readStream = new MemoryStream(buffer, start, lenght);
int bytesRead = readStream.Read(buffer, 0, m_readSize);
// write the required bytes
while (bytesRead > 0 && !m_bStopLecture)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, m_readSize);
}
readStream.Close();
}
Here is the place that call the function. This is in a loop and as I said, the video is playing in the PictureBox.
// image at stop
Stream towrite = new MemoryStream(buffer, start, stop - start);
Image img = Image.FromStream(towrite);
imgSnapshot.Image = img;
// write to the stream
ReadWriteStream(buffer, start, stop - start, writeStream);
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个实现 @ https://net7mma.codeplex.com/SourceControl/latest 特别是https://net7mma.codeplex.com/SourceControl/latest#Rtsp/Server /Streams/MJPEGSourceStream.cs
像这样:
There is an implementation @ https://net7mma.codeplex.com/SourceControl/latest specifically https://net7mma.codeplex.com/SourceControl/latest#Rtsp/Server/Streams/MJPEGSourceStream.cs
Something like this:
您需要在流上设置内容类型,并包含帧边界数据。我首先看一下问题 MJPG VLC 和 HTTP Streaming。
You need to set the content type on the stream, and include the frame boundry data. I would start by looking at the question MJPG VLC and HTTP Streaming.