樹莓派簡單攝像頭錄像並保存視頻文件
阿新 • • 發佈:2018-09-22
mep lin line poi reat path wait pen ini
一、簡介
本文講使用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."); } } }
樹莓派簡單攝像頭錄像並保存視頻文件