树莓派简单摄像头录像并保存视频文件
一、简介
本文讲使用OpenCV,不使用FFMPEG的方法进行保存视频。
二、代码
1、引用
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="OpenCvSharp3-AnyCPU" version="3.4.1.20180830" targetFramework="net47" /> </packages>
2、代码
using OpenCvSharp; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace OpenCVDemo { class Program { static void Main(string[] args) { var cameraIndex = 0; if (args != null && args.Length > 0) { int.TryParse(args[0], out cameraIndex); } Console.WriteLine("Begin At Cam Index " + cameraIndex + " ..."); var path = Path.Combine(AppContext.BaseDirectory, "imgs"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); int count = 0; var videoSize = new Size(640, 480); var timePoint = new Point(0, 15); VideoWriter videoWriter = new VideoWriter(Path.Combine(path, "Video.mp4"), FourCC.XVID, 30, videoSize, true); using (var camera = VideoCapture.FromCamera(CaptureDevice.Any)) { //VideoWriter videoWriter = new VideoWriter(Path.Combine(path, "Video.mp4"), FourCC.Prompt, 30, //new Size(camera.Get(CaptureProperty.FrameWidth), camera.Get(CaptureProperty.FrameHeight))); while (true) { var mat = camera.RetrieveMat(); if (mat.Empty()) { Thread.Sleep(100); continue; } if (++count > 30 * 30) break; Console.WriteLine("save .. " + count); //mat.PutText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), new Point(10, 10), HersheyFonts.HersheyPlain, 20, Scalar.Yellow, 2); Cv2.PutText(mat, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " GTM+8", timePoint, HersheyFonts.HersheyPlain, 1, Scalar.Red, 1); //OpenCvSharp.Cv2.ImShow("myWin", mat); //if (Cv2.WaitKey(40) == 13) //{ // break; //} Thread.Sleep(50); var newmat = mat.Resize(videoSize); videoWriter.Write(newmat); mat.Dispose(); newmat.Dispose(); } videoWriter.Dispose(); //GC.Collect(); } Console.WriteLine("Finish."); } } }
宋兴柱:转载内容,请标明出处,谢谢!源文来自 宝贝云知识分享:https://www.dearcloud.cn
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探
2016-09-22 异步一般处理程序的用法
2015-09-22 Windows服务安装与控制