VideoTransformEffectDefinition ends with Error: Video could not be decoded

VideoTransformEffectDefinition ends with Error: Video could not be decoded

我正在尝试使用 VideoTransformEffectDefinition .

        var clip = await MediaClip.CreateFromFileAsync(pickedFile);//creating clip from storage file
        MediaComposition composition = new MediaComposition();
        composition.Clips.Add(clip);
        VideoTransformEffectDefinition videoEffect1 = new VideoTransformEffectDefinition();
        videoEffect1.PaddingColor = Colors.Blue;//this line is not necessary, does not matter how videoEffect is changed 
        clip.VideoEffectDefinitions.Add(videoEffect1);//adding videoEffect
        this.mediaElement.SetMediaStreamSource(composition.GenerateMediaStreamSource());//setting source to MediaElement

这不会抛出异常,但它会告诉我

"Error: Video could not be decoded"

在媒体元素上。

这很奇怪,因为代码在 VideoStabilizationEffectDefinition:

下运行良好
        var clip = await MediaClip.CreateFromFileAsync(pickedFile);//creating clip from storage file
        MediaComposition composition = new MediaComposition();
        composition.Clips.Add(clip);
        VideoStabilizationEffectDefinition videoEffect = new VideoStabilizationEffectDefinition();
        clip.VideoEffectDefinitions.Add(videoEffect);//adding videoEffect
        this.mediaElement.SetMediaStreamSource(composition.GenerateMediaStreamSource());//setting source to MediaElement

我做错了什么?

VideoTransformEffectDefinition only works with MediaTranscoder. It has no effect over MediaComposition。当您通过 MediaElement.SetMediaStreamSource(MediaComposition .GenerateMediaStreamSource()).

设置 MediaElement 时,它会抛出 NullReferenceException

有来自 GitHub that uses the MediaTranscoder. And in Scenario1, it creates the MediaTranscoder by private MediaTranscoder _Transcoder = new MediaTranscoder(); You can add the VideoTransformEffectDefinition to MediaTranscoder by AddAudioEffect(System.String activatableClassId, System.Boolean effectRequired, IPropertySet configuration) 的官方样本。

您可以将以下代码添加到 Scenario1_Default.xaml.cs 中的 TranscodePreset 事件。

例如:

VideoTransformEffectDefinition videoEffect = new VideoTransformEffectDefinition();
_Transcoder.ClearEffects();
var activatableClassId = videoEffect.ActivatableClassId;
_Transcoder.AddVideoEffect(activatableClassId, true, videoEffect.Properties);
videoEffect.PaddingColor = Colors.Blue;
videoEffect.Rotation = MediaRotation.Clockwise90Degrees;