当前位置: 首页 > news >正文

opencvsharp usb摄像头录像 c# H264编码

1.首先创建ConsoleApp不要创建WinFormWInForm帧率和实际对不上,有延时

2.下载opencvsharp。

3.下载openh264-1.8.0-win32.dll  ,  openh264-1.8.0-win64.dll .放在根目录。

https://github.com/cisco/openh264

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace ConsoleApp1
{internal class Program{private const int CameraIndex = 0;private const double Fps = 30.0;// private static readonly OpenCvSharp.Size FrameSize = new OpenCvSharp.Size(1280, 720);private static readonly OpenCvSharp.Size FrameSize = new OpenCvSharp.Size(640, 480);private const int ReconnectInterval = 3000; // 重试间隔(ms)private const int MaxReconnectAttempts = 5; // 最大重试次数static void Main(string[] args){int reconnectAttempts = 0;bool isRecording = true;while (isRecording && reconnectAttempts < MaxReconnectAttempts){try{using (var capture = new VideoCapture(CameraIndex))using (var writer = new VideoWriter()){if (!InitializeCamera(capture)){reconnectAttempts++;Thread.Sleep(ReconnectInterval);continue;}string OutputFile = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + "_output.avi";if (!InitializeVideoWriter(writer, OutputFile, Fps, FrameSize)){Console.WriteLine("无法初始化视频写入器!");return;}Console.WriteLine("开始录制,按ESC键停止...");reconnectAttempts = 0; // 重置重试计数器using (var window = new Window("摄像头画面"))using (var frame = new Mat()){while (true){// 检查摄像头是否仍然连接if (!capture.IsOpened()){Console.WriteLine("检测到摄像头断开连接!");break;}// 捕获帧capture.Read(frame);DateTime now = DateTime.Now;Cv2.PutText(frame, now.ToString("yyyy-MM-dd HH:mm:ss"), new Point(10, 30),HersheyFonts.HersheySimplex, 0.6, Scalar.White, 1);if (frame.Empty()){Console.WriteLine("接收到空帧,可能摄像头已断开!");break;}// 显示帧window.ShowImage(frame);// 写入帧到文件writer.Write(frame);// 按ESC键退出if (Cv2.WaitKey(10) == 27){isRecording = false;break;}}}}}catch (Exception ex){Console.WriteLine($"发生错误: {ex.Message}");}if (isRecording && reconnectAttempts < MaxReconnectAttempts){Console.WriteLine($"尝试重新连接({reconnectAttempts + 1}/{MaxReconnectAttempts})...");reconnectAttempts++;Thread.Sleep(ReconnectInterval);}}if (reconnectAttempts >= MaxReconnectAttempts){Console.WriteLine($"已达到最大重试次数({MaxReconnectAttempts}),停止尝试。");}Console.WriteLine("程序结束");}static bool InitializeCamera(VideoCapture capture){if (!capture.IsOpened()){Console.WriteLine("无法打开摄像头!");return false;}// 设置摄像头参数capture.Set(VideoCaptureProperties.FrameWidth, FrameSize.Width);capture.Set(VideoCaptureProperties.FrameHeight, FrameSize.Height);capture.Set(VideoCaptureProperties.Fps, Fps);// 验证实际设置的值double actualWidth = capture.Get(VideoCaptureProperties.FrameWidth);double actualHeight = capture.Get(VideoCaptureProperties.FrameHeight);Console.WriteLine($"摄像头分辨率: {actualWidth}x{actualHeight}");return true;}static bool InitializeVideoWriter(VideoWriter writer, string filename, double fps, Size frameSize){// 尝试几种常见的编码格式Dictionary<string, FourCC> _codecs = new Dictionary<string, FourCC>(){{"X264", FourCC.X264},{"XVID", FourCC.XVID},{"MJPG", FourCC.MJPG},{"H264", FourCC.H264},{"MP4V", FourCC.MP4V},{"DIVX", FourCC.DIVX}};foreach (var codec in _codecs){writer.Open(filename, codec.Value, fps, frameSize, true);if (writer.IsOpened()){Console.WriteLine($"使用编码器: {codec.Key}");return true;}}Console.WriteLine("无法找到合适的视频编码器!");return false;}}
}

4.运行。

整个工程打包下载:

【免费】opencvsharpusb摄像头录像c#H264编码资源-CSDN文库

http://www.xdnf.cn/news/647857.html

相关文章:

  • ROS2学习(12)------ROS2 分布式通信说明
  • SpringBoot3集成Oauth2.1——6数据库存储客户端信息
  • 11.11 TypedDict与Pydantic实战:Python高效状态管理秘籍
  • k8s部署RocketMQ集群
  • 原生小程序与 UniApp 中传递循环数据(整个对象)的对比与实现
  • 答题pk小程序题库题型更新啦
  • AI智能混剪视频大模型开发方案:从文字到视频的自动化生成·优雅草卓伊凡
  • 视频问答功能播放器(视频问答)视频弹题功能实例
  • 【后端高阶面经:Elasticsearch篇】39、Elasticsearch 查询性能优化:分页、冷热分离与 JVM 调优
  • Android 中的 ViewModel详解
  • 远控安全进阶之战:TeamViewer/ToDesk/向日葵设备安全策略对比
  • Java基础(一):发展史、技术体系与JDK环境配置详解
  • 深度 |工业互联网的下一个十年:AI如何成“关键变量”
  • 类和对象(5)--《Hello C++ Wrold!》(7)--(C/C++)--构造函数的初始化列表,explicit关键词,友元,内部类和匿名对象
  • 【基于SpringBoot的图书管理系统】Redis在图书管理系统中的应用:加载和添加图书到Redis,从数据同步到缓存优化
  • spring实战第四版01
  • 【SpringBoot】从零开始全面解析Spring IocDI (二)
  • Windows系统如何查看ssh公钥
  • 第十一天 5G切片技术在车联网中的应用
  • ORM++ 封装实战指南:安全高效的 C++ MySQL 数据库操作
  • window 显示驱动开发-视频内存的直接交替(二)
  • 黑马点评Reids重点详解(Reids使用重点)
  • P2015 二叉苹果树
  • C#高级:Winform桌面开发中CheckedListBox的详解
  • 泰迪杯特等奖案例深度解析:基于三维点云与深度学习的复杂零件装配质量检测系统设计
  • 基于AOD-Net与GAN的深度学习去雾算法开发
  • 【Spring】Spring AI 核心知识(一)
  • LSTM三个门控机制详解
  • 电池预测 | 第28讲 基于CNN-GRU的锂电池剩余寿命预测
  • 对Spring IOC与AOP的理解