I'm creating an UWP app in C#, which downloads a video and allows the user to trim a video duration and save the movie in three different modes: Video & Audio (.mp4), Only video (.mp4) and Only audio (.mp3). At the moment it only supports Twitch VOD, so input video is .ts file.
Downloading works fine, but there is a problem with rendering. I'm using Windows.Media.Editing namespace to edit the video. I can create the composition, create media clip, set trim time and volume, add clip to composition and start rendering.
Some videos throw an error in the middle of processing:
System.Exception
HResult=0xC00DA7FC
Message=Stream is not in a state to handle the request.
Stream is not in a state to handle the request.
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ProgramName.Core.Sources.Video.<DownloadTaskAsync>d__96.MoveNext() in D:\Project\Core\Sources\Video.cs:line 645
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at ProgramName.Core.Sources.Video.<Download>d__92.MoveNext() in D:\Project\Core\Sources\Video.cs:line 509
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at ProgramName.Core.Sources.Video.<DownloadOrCancelButtonClicked>d__95.MoveNext() in D:\Project\Core\Sources\Video.cs:line 563
Other throws an error at the beginning (0%):
System.Exception
HResult=0xC00D6D60
Message=A valid type has not been set for this stream or a stream that it depends on. (Exception from HRESULT: 0xC00D6D60)
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ProgramName.Core.Sources.Video.<DownloadTaskAsync>d__96.MoveNext() in D:\Project\Core\Sources\Video.cs:line 645
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at ProgramName.Core.Sources.Video.<Download>d__92.MoveNext() in D:\Project\Core\Sources\Video.cs:line 509
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at ProgramName.Core.Sources.Video.<DownloadStopButtonClicked>d__95.MoveNext() in D:\Project\Core\Sources\Video.cs:line 563
Only a small percentage of videos render completely.
Here is a fragment of code responsible for video processing:
// Init media composition and add clip
MediaComposition composition = new MediaComposition();
StorageFile inputVideoFile = await TempDownloadFolder.GetFileAsync(downloadTempFilename);
MediaClip video = await MediaClip.CreateFromFileAsync(inputVideoFile);
// TrimStart and TrimEnd are TimeSpan variables
// Trim at start
if (0 < TrimStart.TotalMilliseconds && TrimStart.TotalMilliseconds < Duration.TotalMilliseconds)
{
video.TrimTimeFromStart = TrimStart;
}
// Trim at end
if (0 < TrimEnd.TotalMilliseconds && TrimEnd.TotalMilliseconds < Duration.TotalMilliseconds)
{
video.TrimTimeFromEnd = TrimEnd;
}
// MediaType is string variable. V - Only video mode, A - Only audio mode, AV - Audio & Video mode
// Only video mode
if (MediaType == "V")
{
video.Volume = 0;
}
// Add video to composition
composition.Clips.Add(video);
// Render
IAsyncOperationWithProgress<TranscodeFailureReason, double> saveOperation;
if (MediaType == "A") // Only audio mode
{
saveOperation = composition.RenderToFileAsync(outputVideoFile, MediaTrimmingPreference.Precise, MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Medium));
}
else // Only video and Audio & Video mode
{
saveOperation = composition.RenderToFileAsync(outputVideoFile, MediaTrimmingPreference.Precise);
}
saveOperation.Progress = new AsyncOperationProgressHandler<TranscodeFailureReason, double>(RenderVideoProgressEvent);
try
{
await saveOperation.AsTask(token); // This line throws an exceptions
}
catch (TaskCanceledException) { }
I have no idea where the problem is.