我试图在我的WPF C#应用程序上显示MJPEG视频流。我使用的MJPEG解码器库是AForge.Video。到目前为止,我已经成功地在http上显示了视频,其内容如下:
using System.IO;
using System.Windows;
using System.Drawing;
using System.Windows.Media.Imaging;
using AForge.Video;
namespace VideoControl
{
public partial class MainWindow : Window
{
MJPEGStream stream;
public MainWindow()
{
InitializeComponent();
stream = new MJPEGStream("http://mylocaladdress:1220/video"); //Only works over http: and not ws:.
stream.NewFrame += GetNewFrame;
stream.Start();
}
void GetNewFrame(object sender, NewFrameEventArgs e)
{
Bitmap bmp = (Bitmap)e.Frame.Clone();
Dispatcher.Invoke(() => {imgStream.Source = BitmapToImageSource(bmp);}); //Convert bitmap to image source first before applying to image.
}
BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
}
}然而,我的目标是通过websockets (ws)而不是http获得视频流。我将如何为这个库实现websocket (ws)协议?简单地用websocket地址替换http地址是行不通的,因为库不理解websocket协议。
我使用这 FFMPEG包装器在节点红色服务器上托管我的流。视频流在浏览器中显示得很好,但在C#中却不显示。我之前在http上进行的成功测试是使用条眼进行的。除了AForge.Video,是否有其他的MJPEG解码器支持开箱即用的websockets?
发布于 2022-09-22 18:09:52
MJpeg被设计为通过HTTP请求广播。它的解析方法根据内容类型标头而改变,这在WebSockets中是没有的。我认为任何库都不支持这一点,但您可以像jsmpeg (谢谢@jdweng)那样创建一个解决方案。
或
您可以根据AForge自己的MJPegStream (GitHub)流读取器编写自己的流解码器。
https://stackoverflow.com/questions/73731064
复制相似问题