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

C# WPF中720p 60fps摄像头录制时长异常求助

问题:720p+60fps录制视频时长不足预期的一半

我使用以下C# WPF代码打开摄像头并在Image控件显示画面:

public async void initializeCamara(int index, int fps, System.Drawing.Size resolution)
{
    videoCapture = new VideoCapture(index, VideoCaptureAPIs.DSHOW);
    videoCapture.Set(VideoCaptureProperties.Fps, this.fps);
    videoCapture.Set(VideoCaptureProperties.FrameHeight, this.resolution.Height);
    videoCapture.Set(VideoCaptureProperties.FrameWidth, this.resolution.Width);

    await Task.Run(() => displayCameraCallback());
}

随后通过VideoWriter截取当前帧保存为视频。多数参数组合运行正常,但设置720p分辨率+60fps时,录制的视频时长仅为预期的一半左右。60fps搭配其他分辨率、720p搭配30fps的组合均无此问题。我使用AForge获取摄像头支持的帧率和分辨率参数,恳请解决该问题。


解决方案

1. 验证摄像头实际输出帧率

AForge检测到的支持参数不代表硬件能稳定输出。在displayCameraCallback中添加帧率统计,确认实际捕获的帧速率:

private int frameCount = 0;
private Stopwatch stopwatch = Stopwatch.StartNew();

private void displayCameraCallback()
{
    while (videoCapture.IsOpened())
    {
        Mat frame = new Mat();
        if (videoCapture.Read(frame))
        {
            frameCount++;
            // 每2秒计算一次实际帧率
            if (stopwatch.Elapsed.TotalSeconds >= 2)
            {
                float actualFps = frameCount / (float)stopwatch.Elapsed.TotalSeconds;
                Debug.WriteLine($"实际帧率: {actualFps:F2}");
                frameCount = 0;
                stopwatch.Restart();
            }
            // 帧转换到WPF Image的显示逻辑...
        }
    }
}

如果实际帧率接近30fps,说明摄像头在720p下无法输出60帧,需调整录制参数。

2. 对齐VideoWriter与实际捕获帧率

时长缩短核心原因是VideoWriter编码帧率与实际捕获帧率不匹配:比如实际抓30帧,但编码器按60fps写入,播放时会快进一倍。初始化VideoWriter时用实际捕获的帧率替代预设值:

// 假设已通过统计得到actualFps
VideoWriter writer = new VideoWriter(
    "output.mp4",
    VideoWriter.Fourcc('M','P','4','V'),
    actualFps, // 用实际帧率代替60
    new Size(resolution.Width, resolution.Height)
);

3. 调整VideoCapture参数设置顺序

OpenCV的参数设置顺序会影响生效逻辑,建议先锁定分辨率,再设置帧率:

videoCapture = new VideoCapture(index, VideoCaptureAPIs.DSHOW);
// 先设置分辨率
videoCapture.Set(VideoCaptureProperties.FrameWidth, this.resolution.Width);
videoCapture.Set(VideoCaptureProperties.FrameHeight, this.resolution.Height);
// 再设置帧率
videoCapture.Set(VideoCaptureProperties.Fps, this.fps);

部分摄像头需要先确定分辨率档位,才能匹配对应的帧率选项。

4. 排查硬件与带宽限制

720p@60fps对USB带宽和摄像头硬件要求更高:

  • 检查是否使用USB 2.0接口,720p@60fps可能超出其带宽上限,更换USB 3.0接口尝试;
  • 在设备管理器中查看摄像头属性,确认其硬件实际支持的最高帧率档位。

5. 用AForge验证捕获能力

既然用AForge获取参数,可直接用AForge的捕获类对比OpenCV的结果:

// AForge捕获示例
VideoCaptureDevice videoSource = new VideoCaptureDevice(cameraMoniker);
videoSource.VideoResolution = videoSource.VideoResolutions.First(r => r.FrameSize == resolution && r.FrameRate == fps);
videoSource.NewFrame += (s, e) =>
{
    frameCount++;
    // 同样统计帧率
};
videoSource.Start();

如果AForge能稳定捕获60帧,说明OpenCV的DSHOW API适配有问题,尝试更换VideoCaptureAPIs.MSMF作为捕获接口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 18:45:14