欢迎来到我的的博客园,祝大家学有所成,早点实现自己的人生理想。

树莓派简单摄像头录像并保存视频文件

一、简介

  本文讲使用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.");
        }
    }
}
复制代码

 

posted @   宋兴柱  阅读(5369)  评论(1编辑  收藏  举报
编辑推荐:
· 从 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服务安装与控制
点击右上角即可分享
微信分享提示