// <SnippetAddVideoEffect> private void AddVideoEffect() { var currentClip = composition.Clips.FirstOrDefault( mc => mc.StartTimeInComposition <= mediaPlayerElement.MediaPlayer.PlaybackSession.Position && mc.EndTimeInComposition >= mediaPlayerElement.MediaPlayer.PlaybackSession.Position); VideoStabilizationEffectDefinition videoEffect = new VideoStabilizationEffectDefinition(); currentClip.VideoEffectDefinitions.Add(videoEffect); }
//</SnippetDeclareVideoStabilizationEffect> public async Task CreateVideoStabilizationEffectAsync() { //<SnippetCreateVideoStabilizationEffect> // Create the effect definition VideoStabilizationEffectDefinition stabilizerDefinition = new VideoStabilizationEffectDefinition(); // Add the video stabilization effect to media capture _videoStabilizationEffect = (VideoStabilizationEffect)await _mediaCapture.AddVideoEffectAsync(stabilizerDefinition, MediaStreamType.VideoRecord); _videoStabilizationEffect.EnabledChanged += VideoStabilizationEffect_EnabledChanged; await SetUpVideoStabilizationRecommendationAsync(); _videoStabilizationEffect.Enabled = true; //</SnippetCreateVideoStabilizationEffect> }
/// <summary> /// Adds video stabilization to the video record stream, registers for its event, enables it, and gets the effect instance /// </summary> /// <returns></returns> private async Task CreateVideoStabilizationEffectAsync() { // No work to be done if there already is an effect if (_videoStabilizationEffect != null) { return; } // Create the definition, which will contain some initialization settings var definition = new VideoStabilizationEffectDefinition(); // Add the effect to the video record stream _videoStabilizationEffect = (VideoStabilizationEffect)await _mediaCapture.AddVideoEffectAsync(definition, MediaStreamType.VideoRecord); Debug.WriteLine("VS effect added to pipeline"); // Subscribe to notifications for changes in the enabled state for the effect _videoStabilizationEffect.EnabledChanged += VideoStabilizationEffect_EnabledChanged; // Enable the effect _videoStabilizationEffect.Enabled = true; #if (USE_VS_RECOMMENDATION) // Configure the pipeline to use the optimal settings for VS await SetUpVideoStabilizationRecommendationAsync(); // At this point, one of two things has happened: // // a) If a more suitable capture resolution was available: // 1. Such resolution will have been set up for video capture ("input") // 2. The MediaEncodingProfile ("output") will have been changed to specify dimensions reflecting the amount of cropping // done on said capture resolution (possibly even none if the new resolution offers enough padding) // b) If no better suited capture resolution was available: // 1. The video capture resolution will not have changed // 2. The MediaEncodingProfile will have been changed to specify smaller dimensions than the capture resolution // so that the video isn't scaled back up to the capture resolution after cropping, which could cause a loss in quality #else Debug.WriteLine("Not setting up VS recommendation"); // Not setting up VS recommendation means that the video will be captured at the desired resolution, // then cropped by the VS effect as part of the stabilization process, and then scaled back up to the // original capture resolution #endif }
/// <summary> /// Adds video stabilization to the video record stream, registers for its event, enables it, and gets the effect instance /// </summary> /// <returns></returns> private async Task CreateVideoStabilizationEffectAsync() { // No work to be done if there already is an effect if (_videoStabilizationEffect != null) return; // Create the definition, which will contain some initialization settings var definition = new VideoStabilizationEffectDefinition(); // Add the effect to the video record stream _videoStabilizationEffect = (VideoStabilizationEffect)await _mediaCapture.AddVideoEffectAsync(definition, MediaStreamType.VideoRecord); Debug.WriteLine("VS effect added to pipeline"); // Subscribe to notifications for changes in the enabled state for the effect _videoStabilizationEffect.EnabledChanged += VideoStabilizationEffect_EnabledChanged; // Enable the effect _videoStabilizationEffect.Enabled = true; #if (USE_VS_RECOMMENDATION) // Configure the pipeline to use the optimal settings for VS await SetUpVideoStabilizationRecommendationAsync(); // At this point, one of two things has happened: // // a) If a more suitable capture resolution was available: // 1. Such resolution will have been set up for video capture ("input") // 2. The MediaEncodingProfile ("output") will have been changed to specify dimensions reflecting the amount of cropping // done on said capture resolution (possibly even none if the new resolution offers enough padding) // b) If no better suited capture resolution was available: // 1. The video capture resolution will not have changed // 2. The MediaEncodingProfile will have been changed to specify smaller dimensions than the capture resolution // so that the video isn't scaled back up to the capture resolution after cropping, which could cause a loss in quality #else Debug.WriteLine("Not setting up VS recommendation"); // Not setting up VS recommendation means that the video will be captured at the desired resolution, // then cropped by the VS effect as part of the stabilization process, and then scaled back up to the // original capture resolution #endif }
async Task <bool> InitializeMediaCaptureAsync() { if (this.CameraComboBox.SelectedValue == null) { return(false); } this.ErrorMessageGrid.Visibility = Visibility.Collapsed; if (_mediaCapture != null) { await CleanupCameraAsync(); } _mediaCapture = new MediaCapture(); _mediaCapture.Failed += MediaCapture_Failed; var setting = new MediaCaptureInitializationSettings() { VideoDeviceId = this.CameraComboBox.SelectedValue as string, StreamingCaptureMode = StreamingCaptureMode.Video, }; try { await _mediaCapture.InitializeAsync(setting); this.CameraPreview.Source = _mediaCapture; await _mediaCapture.StartPreviewAsync(); setBrightnessControl(); await setVideoStabilizationEffectAsync(); } catch (UnauthorizedAccessException) { HideCameraUI(); await CleanupCameraAsync(); return(false); } catch (Exception ex) { switch (ex.HResult) { case -1072875854: // C00D 36B2 // 現在の状態では、要求は無効です。\n deviceActivateCount case -1072873822: // C00D 3EA2 // ビデオ録画デバイスは存在しません。 // (休止からのリジューム時に出ることがある) case -1072875772: // C00D 3704 // ハードウェア リソースがないため、ハードウェア MFT はストリーミングを開始できませんでした。 // Failedハンドラでメッセージ表示済み。何もせず抜ける break; default: ShowErrorMessage((uint)ex.HResult, ex.Message, false); await CleanupCameraAsync(); #if DEBUG await(new Windows.UI.Popups.MessageDialog($"{ex.Message} [{ex.HResult:X}]", "catch (Exception ex)")) .ShowAsync(); #endif return(false); } } _initializeRetryCount = 0; return(true); void setBrightnessControl() { _mediaCapture.VideoDeviceController.Brightness.TrySetAuto(true); _mediaCapture.VideoDeviceController.Contrast.TrySetAuto(true); _mediaCapture.VideoDeviceController.Focus.TrySetAuto(true); _mediaCapture.VideoDeviceController.WhiteBalance.TrySetAuto(true); // [明るさ] スライダーを設定 var brightCtl = _mediaCapture.VideoDeviceController.Brightness; this.BrightSlider.Minimum = brightCtl.Capabilities.Min; this.BrightSlider.Maximum = brightCtl.Capabilities.Max; if (brightCtl.TryGetValue(out double bright)) { this.BrightSlider.Value = bright; } // [コントラスト] スライダーを設定 var contrastCtl = _mediaCapture.VideoDeviceController.Contrast; this.ContrastSlider.Minimum = contrastCtl.Capabilities.Min; this.ContrastSlider.Maximum = contrastCtl.Capabilities.Max; if (contrastCtl.TryGetValue(out double contrast)) { this.ContrastSlider.Value = contrast; } } async Task setVideoStabilizationEffectAsync() { // https://docs.microsoft.com/ja-jp/windows/uwp/audio-video-camera/effects-for-video-capture var stabilizerDefinition = new VideoStabilizationEffectDefinition(); var effect = (VideoStabilizationEffect)await _mediaCapture.AddVideoEffectAsync(stabilizerDefinition, MediaStreamType.Photo); effect.Enabled = true; } } // InitializeMediaCaptureAsync