0

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.

MatS2510
  • 21
  • 1
  • 5
  • I tested with official code sample with normal mp4 file , but I can't reproduce your problem, I suppose the .ts file cause problem, could you mind share [mcve] for us, I will test base on your sample . – Nico Zhu Sep 02 '21 at 02:34
  • @NicoZhu-MSFT Here is a simplified version of my application: https://github.com/mateuszskoczek/test . Examples of movies that cause the problem: https://www.twitch.tv/videos/1046683537 , https://www.twitch.tv/videos/1103074145 , https://www.twitch.tv/videos/947934250 (VODs from official Twitch's channels) – MatS2510 Sep 02 '21 at 18:49
  • This video (https://www.twitch.tv/videos/1124345239) throws an exception with selected quality "720p59", but works perfectly with "1080p56" – MatS2510 Sep 02 '21 at 19:10
  • well, I got it, I will test it, give me a second. – Nico Zhu Sep 06 '21 at 01:19

0 Answers0