c# 摄像头及保存视频记录
using System.IO; using ICameraDll.DirectX.Capture; Capture capture; //摄像头录像操作 Filters filters = new Filters(); //Filter集合 // 函数 int GetffshowIndex() { FilterCollection videoCompressors = this.filters.VideoCompressors; for (var i = 0; i < videoCompressors.Count; i++) { if ((videoCompressors[i] != null) && videoCompressors[i].Name.Equals("ffdshow video encoder")) { return i; } } return -1; } void CreateFilepath(string FilePath) { var dir = FilePath.Remove(FilePath.LastIndexOf("\\")); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } } private void button1_Click(object sender, EventArgs e) { Control videoControl = pictureBox1; string filePath = "d:\\Camera\\test\\Video\\"; string fileName = "test.avi"; // 检测摄像头是否被占用 if (capture != null) { capture.Stop(); capture.DisposeCapture(); } //获取ffshow视频解码器索引 var ffshowIndex = GetffshowIndex(); if (ffshowIndex > 0) { var Flie = filePath + fileName; CreateFilepath(Flie); capture = new Capture(new Filters().VideoInputDevices[0], null); //设置承载控件 capture.PreviewWindow = videoControl; //设置视频解码器 capture.VideoCompressor = filters.VideoCompressors[ffshowIndex]; //设置要保存的文件路径和文件名 capture.Filename = Flie; //设置帧 capture.FrameRate = 15; //设置视频分辨率 capture.FrameSize = new Size(320, 240); //开启录制 capture.Start(); } } private void button2_Click(object sender, EventArgs e) { if (capture != null) { // 停止录制 capture.Stop(); capture.DisposeCapture(); capture = null; } }
另外运行这个代码可能需要给电脑安装视频解码器, 用的是 codecs_pack 这款软件。
参考网址https://blog.csdn.net/lyshark_lyshark/article/details/125852235
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-05-10 .netcore 自带依赖注入
2021-05-10 [HttpPost]和[AcceptVerbs(HttpVerbs.Post)]区别