c# - 在 UWP 中渲染视频时出现异常
问题描述
我正在用 C# 创建一个 UWP 应用程序,它下载视频并允许用户修剪视频持续时间并以三种不同的模式保存电影:视频和音频 (.mp4)、仅视频 (.mp4) 和仅音频 ( .mp3)。目前它只支持 Twitch VOD,所以输入视频是 .ts 文件。
下载工作正常,但渲染有问题。我正在使用 Windows.Media.Editing 命名空间来编辑视频。我可以创建合成、创建媒体剪辑、设置修剪时间和音量、将剪辑添加到合成并开始渲染。
一些视频在处理过程中抛出错误:
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
其他在开始时抛出错误(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
只有一小部分视频完全呈现。
下面是一段负责视频处理的代码:
// 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) { }
我不知道问题出在哪里。
解决方案
推荐阅读
- mongodb - 如何在 Mongoose.js 中创建“对象数组的数组”模式
- angular - angular ComponentFactoryResolver 继承父 css
- java - Java:用于存储 900,000 个客户编号的集合
- javascript - 如何在 HTML 代码中的 Javascript 与 Style.css 之间建立链接?
- android - EditText 中的方法 setText() 改变光标的位置
- node.js - 在 express.js 中设置服务器超时
- linux - linux 中带有 ssl 的 libcurl(curle_not_built_in 错误)
- python - 如何在虚拟环境中使用 pip 安装较低版本的 Django
- spring-boot - Spring Boot Oracle Exadata | 连接重置错误
- azure-data-lake - 从内部连接子查询中选择前 1