如何使用 C# 流式传输网络摄像头视频?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/342774/
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
How can I stream webcam video with C#?
提问by The.Anti.9
I want to make a simple server application where people can connect using a browser-based client (which I will make later) to watch streaming video. And I want to use C#.
我想制作一个简单的服务器应用程序,人们可以在其中使用基于浏览器的客户端(我稍后会制作)进行连接以观看流媒体视频。我想使用 C#。
What do I need to capture video or rapid images through a webcam and send them over the network?
通过网络摄像头捕捉视频或快速图像并通过网络发送它们需要什么?
采纳答案by CMPalmer
If you want a "capture/streamer in a box" component, there are several out there as others have mentioned.
如果你想要一个“盒子里的捕获/流光”组件,正如其他人提到的那样,有几个。
If you want to get down to the low-level control over it all, you'll need to use DirectShow as thealliedhacker points out. The best way to use DirectShow in C# is through the DirectShow.Netlibrary - it wraps all of the DirectShow COM APIs and includes many useful shortcut functions for you.
如果你想深入控制这一切,你需要像 thealliedhacker 指出的那样使用 DirectShow。在 C# 中使用 DirectShow 的最佳方法是通过DirectShow.Net库——它封装了所有 DirectShow COM API,并为您提供了许多有用的快捷功能。
In addition to capturing and streaming, you can also do recording, audio and video format conversions, audio and video live filters, and a whole lot of stuff.
除了捕获和流式传输,您还可以进行录制、音频和视频格式转换、音频和视频实时过滤器等等。
Microsoft claims DirectShow is going away, but they have yet to release a new library or API that does everything that DirectShow provides. I suspect many of the latest things they have released are still DirectShow under the hood. Because of its status at Microsoft, there aren't a whole lot of books or references on it other than MSDN and what you can find on forums. Last year when we started a project using it, the best book on the subject - Programming Microsoft DirectShow- was out of print and going for around $350 for a used copy!
微软声称 DirectShow 正在消失,但他们还没有发布一个新的库或 API 来完成 DirectShow 提供的所有功能。我怀疑他们发布的许多最新内容仍然是 DirectShow。由于它在 Microsoft 的地位,除了 MSDN 和您可以在论坛上找到的内容之外,没有太多关于它的书籍或参考资料。去年,当我们开始一个使用它的项目时,关于这个主题的最好的书——Programming Microsoft DirectShow——已经绝版了,一本二手书的售价约为 350 美元!
回答by faulty
I've used VideoCapXfor our project. It will stream out as MMS/ASF stream which can be open by media player. You can then embed media player into your webpage.
我在我们的项目中使用了VideoCapX。它将以 MMS/ASF 流的形式流出,可由媒体播放器打开。然后,您可以将媒体播放器嵌入到您的网页中。
If you won't need much control, or if you want to try out VideoCapX without writing a code, try U-Broadcast, they use VideoCapX behind the scene.
如果您不需要太多控制,或者如果您想在不编写代码的情况下试用 VideoCapX,请尝试U-Broadcast,他们在幕后使用 VideoCapX。
回答by Excel Kobayashi
The usual API for this is DirectShow.
常用的 API 是 DirectShow。
You can use P/Invoke to import the C++ APIs, but I think there are already a few projects out there that have done this.
您可以使用 P/Invoke 来导入 C++ API,但我认为已经有一些项目已经这样做了。
http://channel9.msdn.com/forums/TechOff/93476-Programatically-Using-A-Webcam-In-C/
http://channel9.msdn.com/forums/TechOff/93476-Programatically-Using-A-Webcam-In-C/
http://www.codeproject.com/KB/directx/DirXVidStrm.aspx
http://www.codeproject.com/KB/directx/DirXVidStrm.aspx
To get the streaming part, you probably want to use DirectShow to apply a compression codec to reduce lag, then you can get a Stream and transmit it. You could consider using multicast to reduce network load.
要获得流媒体部分,您可能希望使用 DirectShow 应用压缩编解码器以减少延迟,然后您可以获得一个 Stream 并传输它。您可以考虑使用多播来减少网络负载。
回答by Chris Holmes
You could just use VideoLAN. VideoLAN will work as a server (or you can wrap your own C# application around it for more control). There are also .NET wrappers for the viewer that you can use and thus embed in your C# client.
你可以只使用VideoLAN。VideoLAN 将用作服务器(或者您可以将自己的 C# 应用程序包装在它周围以获得更多控制)。您还可以使用查看器的 .NET 包装器,从而将其嵌入到您的 C# 客户端中。
回答by gillonba
If you want to record video from within a web browser, I think your only option is Flash. We are looking to do the same thing. We are also primarily a .NET house and I don't see a way to use .NET to capture the webcam _from_within_the_browser_. All of the other solutions mentioned here would probably work great if you are happy to settle for a desktop app
如果您想从网络浏览器中录制视频,我认为您唯一的选择是 Flash。我们正在寻求做同样的事情。我们也主要是一家 .NET 公司,我看不到使用 .NET 来捕获网络摄像头 _from_within_the_browser_ 的方法。如果您乐于使用桌面应用程序,这里提到的所有其他解决方案可能会很好用
回答by mark gamache
Another option to stream images from a webcam to a browser is via mjpeg. This is just a series of jpeg images that most modern browsers support as part of the tag. Here's a sample server written in c#:
将图像从网络摄像头流式传输到浏览器的另一种选择是通过 mjpeg。这只是大多数现代浏览器支持的一系列 jpeg 图像作为标签的一部分。这是一个用 c# 编写的示例服务器:
https://www.codeproject.com/articles/371955/motion-jpeg-streaming-server
https://www.codeproject.com/articles/371955/motion-jpeg-streaming-server
This works well over a LAN, but not as well over the internet as mjpeg is not as effcient as other video codecs (h264, VP8 etc..)
这在 LAN 上运行良好,但在 Internet 上效果不佳,因为 mjpeg 不如其他视频编解码器(h264、VP8 等)有效。