C#uwp以每秒60帧的速度记录两个网络摄像头

时间:2017-01-23 13:36:54

标签: c# uwp windows-10-universal

我需要做的是在UWP c#中以60 fps 1280x720格式录制2个usb网络摄像头。
相机目前预览在 CaptureElement

这是它开始预览的方式:

    public async Task StartPreviewSideAsync(DeviceInformation deviceInformation)
    {
        if (deviceInformation != null)
        {
            var settings = new MediaCaptureInitializationSettings {VideoDeviceId = deviceInformation.Id};
            try
            {
                _mediaCaptureSide = new MediaCapture();

                var profiles = MediaCapture.FindAllVideoProfiles(deviceInformation.Id);

                Debug.WriteLine(MediaCapture.IsVideoProfileSupported(deviceInformation.Id) + " count: " + profiles.Count);

                var match = (from profile in profiles
                    from desc in profile.SupportedRecordMediaDescription
                    where desc.Width == 1280 && desc.Height == 720 && Math.Abs(Math.Round(desc.FrameRate) - 60) < 1
                    select new {profile, desc}).FirstOrDefault();

                if (match != null)
                {
                    settings.VideoProfile = match.profile;
                    settings.RecordMediaDescription = match.desc;
                }

                await _mediaCaptureSide.InitializeAsync(settings);
                SideCam.Source = _mediaCaptureSide;
                await _mediaCaptureSide.StartPreviewAsync();

                _displayRequestSide = new DisplayRequest();
                _displayRequestSide.RequestActive();

                DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;

                CameraManager.GetCameraManager.CurrentSideCamera = deviceInformation;
                IsPreviewingSide = true;
            }
            catch (UnauthorizedAccessException)
            {
                Debug.WriteLine("The app was denied access to the camera");
                IsPreviewingSide = false;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("MediaCapture initialization failed. {0}", ex.Message);
                IsPreviewingSide = false;
            }
        }
    }

这是开始录制的方法:

    public IAsyncOperation<LowLagMediaRecording> RecBackCam(StorageFile fileBack)
    {
        var mp4File = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p);
        if (mp4File.Video != null)
            mp4File.Video.Bitrate = 3000000;

        return _mediaCaptureBack.PrepareLowLagRecordToStorageFileAsync(mp4File, fileBack);
    }

但它没有记录60fps,因为它找不到它的配置文件(在预览方法中) 当我使用它时(在重新编码方法中):

mp4File.Video.FrameRate.Numerator = 3600;
mp4File.Video.FrameRate.Denominator = 60;

它每秒记录60帧,但帧1和2是相同的3和4,依此类推。但我需要每秒60帧 代码的所有基础知识都来自mdsn网站 link to code on msdn

1 个答案:

答案 0 :(得分:0)

要设置MediaCapture的帧速率,我们可以使用VideoDeviceController.SetMediaStreamPropertiesAsync方法,如下所示:

 await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, encodingProperties);

对于encodingProperties,我们可以从VideoDeviceController.GetAvailableMediaStreamProperties方法获取它,此方法返回视频设备支持的编码属性列表。如果您的相机支持60 fps,1280x720分辨率,那么您应该能够在此列表中找到相应的VideoEncodingProperties

有关详细信息,请参阅GitHub上的文章Set format, resolution, and frame rate for MediaCaptureCamera resolution sample