My question is this: If I’m transcoding a video using Windows.Media.Transcoding.MediaTranscoder (rather than capturing with Windows.Media.Capture.MediaCapture) and wish to add a video stabilization effect, how do I set the properties of the Windows.Media.Core.VideoStabilizationEffect object?
According to some Microsoft documentation (detailed here: Effects for video capture), because of the cropping done during video stabilization, it is desirable to set some properties that will ensure that the resulting output appears with the same apparent quality as the original. The link given above details how to set these properties, but only if capturing video, rather than transcoding.
Looking at the MSDN page for the VideoStabilizationEffect class, I’m instructed to create an instance of VideoStabilizationEffectDefinition, and then get an instance of VideoStabilizationEffect by passing that to MediaCapture.AddVideoEffectAsync or MediaTranscoder.AddVideoEffect. However, that is incorrect. You cannot obtain an instance of VideoStabilizationEffect by calling MediaTranscoder.AddVideoEffect, because all signatures for that method (there are 2) return void. I’m sure that internally, MediaTranscoder has an instance of VideoStabilizationEffect, but it does not appear to be exposed.
Here’s a small working example of my code:
private async void MyButton_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new()
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
FileTypeFilter = { ".avi" }
};
picker.ViewMode = PickerViewMode.List;
nint windowHandle = WindowNative.GetWindowHandle(this);
InitializeWithWindow.Initialize(picker, windowHandle);
StorageFile file = await picker.PickSingleFileAsync();
FileSavePicker savePicker = new()
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
DefaultFileExtension = ".avi",
SuggestedFileName = "NewVideo"
};
savePicker.FileTypeChoices.Add("AVI", new string[] { ".avi" });
InitializeWithWindow.Initialize(savePicker, windowHandle);
StorageFile destination = await savePicker.PickSaveFileAsync();
MediaEncodingProfile profile = MediaEncodingProfile.CreateAvi(VideoEncodingQuality.Auto);
VideoStabilizationEffectDefinition vidStabDef = new();
MediaTranscoder transcoder = new MediaTranscoder();
transcoder.AddVideoEffect(vidStabDef.ActivatableClassId);
PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(file, destination, profile);
if (prepareOp.CanTranscode)
{
var transcodeOp = prepareOp.TranscodeAsync();
transcodeOp.Progress += new AsyncActionProgressHandler<double>(TranscodeProgress);
transcodeOp.Completed += new AsyncActionWithProgressCompletedHandler<double>(TranscodeComplete);
}
}
public void TranscodeProgress(IAsyncActionWithProgress<double> asyncInfo, double percent)
{
}
void TranscodeComplete(IAsyncActionWithProgress<double> asyncInfo, AsyncStatus status)
{
asyncInfo.GetResults();
if (asyncInfo.Status == AsyncStatus.Completed)
{
// Display or handle complete info.
}
else if (asyncInfo.Status == AsyncStatus.Canceled)
{
// Display or handle cancel info.
}
else
{
// Display or handle error info.
}
}
The good news is that this works! The transcoding begins, it reports progress, it reports successful completion, and the resulting video is nicely stabilized. However, I can notice a very slight reduction in quality due to the zoom-in performed by the effect.
I’d like to be able to preconfigure the stabilization effect so that I have more control over the amount of cropping, etc. I’ll also note that the MSDN documention nowhere, as far as I can tell, details exactly what properties are available for this video effect.
So to summarize again: Does anyone know how I might configure the VideoStabilizationEffect before transcoding?