You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows11下C#+OpenCvSharp录制视频遇格式兼容问题求助

问题

在Windows 11笔记本上使用C#结合OpenCvSharp调用摄像头录制视频时,保存MP4格式出现以下错误:

gOpenCV: FFMPEG: tag 0x34363258/'X264' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1'

尝试保存为AVI格式后,VLC播放器和Windows默认播放器均无法打开生成的文件。相关代码如下:

using System;
using System.Threading;
using OpenCvSharp;

namespace VideoCapture
{
    public class Program
    {
        private static bool _isRecording = true;

        static void Main(string[] args)
        {
            Console.WriteLine("Press any key to start recording...");
            Console.ReadKey(); 

            string outputPath = @"C:\Users\me\Example\RecordedVideo.avi";

            Thread recordingThread = new Thread(() => RecordVideo(outputPath));
            recordingThread.Start();

            Console.WriteLine("Recording... Press any key to stop.");

            Console.ReadKey();
            _isRecording = false; 

            recordingThread.Join();

            Console.WriteLine($"Recording stopped. Video saved to: {outputPath}");
        }

        static void RecordVideo(string outputPath)
        {
            using var capture = new VideoCapture(0);
            capture.Set(VideoCaptureProperties.FrameWidth, 640);
            capture.Set(VideoCaptureProperties.FrameHeight, 480);
            capture.Set(VideoCaptureProperties.Fps, 30);

            if (!capture.IsOpened())
            {
                Console.WriteLine("Failed to open the camera.");
                return;
            }

            using var writer = new VideoWriter(outputPath, FourCC.MJPG, 30, new OpenCvSharp.Size(640, 480));

            if (!writer.IsOpened())
            {
                Console.WriteLine("Failed to open the video writer.");
                return;
            }

            while (_isRecording)
            {
                using var frame = new Mat();
                capture.Read(frame); // Capture a frame

                if (frame.Empty())
                {
                    Console.WriteLine("Empty frame encountered. Stopping recording.");
                    break;
                }

                writer.Write(frame);
            }
        }
    }
}
解决建议

修复MP4格式的编码标签问题

  • 直接指定avc1编码标签替代默认的X264,修改VideoWriter的初始化代码:
    // 输出路径改为.mp4后缀
    string outputPath = @"C:\Users\me\Example\RecordedVideo.mp4";
    // 使用avc1编码适配MP4格式
    using var writer = new VideoWriter(outputPath, FourCC.FromString("avc1"), 30, new OpenCvSharp.Size(640, 480));
    
    这个修改会让FFmpeg直接使用MP4兼容的avc1标签,避免 fallback 警告。

解决AVI文件无法播放的问题

  • 更换为播放器兼容的AVI编码,比如DIVX:
    using var writer = new VideoWriter(outputPath, FourCC.DIVX, 30, new OpenCvSharp.Size(640, 480));
    
  • 匹配摄像头实际帧率,避免强制设置的帧率与硬件输出不匹配导致文件损坏:
    // 获取摄像头实际支持的帧率
    double actualFps = capture.Get(VideoCaptureProperties.Fps);
    // 使用实际帧率初始化Writer
    using var writer = new VideoWriter(outputPath, FourCC.DIVX, actualFps, new OpenCvSharp.Size(640, 480));
    
  • 确保录制结束时Writer完成数据写入,可在循环结束后显式调用writer.Release()(虽然using语句会自动释放,但显式调用能避免异常场景下的资源泄漏)。

通用排查要点

  • 确认安装的OpenCvSharp包含完整的FFmpeg运行时,比如安装OpenCvSharp4.runtime.win包,避免缺少编码解码组件。
  • 录制过程中留意控制台输出,若有Failed to open the video writer提示,检查编码格式、分辨率、帧率是否与输出容器兼容。

内容的提问来源于stack exchange,提问作者UnSure

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 23:36:18