【发布时间】:2019-11-04 20:32:15
【问题描述】:
我正在尝试对从 WebRTC 调用中保存的 PCM 格式的音频文件进行转码。 WebRTC 报告的音频流格式为 16 位深度、1 通道和 48000 Hz 采样率。我想将音频转换为 MP3,以便之后可以将音频作为背景音轨添加到我的 Unity UWP 应用程序的屏幕录制中(使用 MediaComposition)。我在第一部分遇到问题:尝试将我的 PCM 音频文件转码为 MP3 文件。当我尝试准备转码时,preparedTranscodeResult.CanTranscode 正在返回 false。以下是我的代码。
StorageFile remoteAudioPCMFile = await StorageFile.GetFileFromPathAsync(Path.Combine(Application.temporaryCachePath, "remote.pcm").Replace("/", "\\"));
StorageFolder tempFolder = await StorageFolder.GetFolderFromPathAsync(Application.temporaryCachePath.Replace("/", "\\"));
StorageFile remoteAudioMP3File = await tempFolder.CreateFileAsync("remote.mp3", CreationCollisionOption.ReplaceExisting);
MediaEncodingProfile profile = MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Auto);
profile.Audio.BitsPerSample = 16;
profile.Audio.ChannelCount = 1;
profile.Audio.SampleRate = 48000;
MediaTranscoder transcoder = new MediaTranscoder();
var preparedTranscodeResult = await transcoder.PrepareFileTranscodeAsync(remoteAudioPCMFile, remoteAudioMP3File, profile);
if (preparedTranscodeResult.CanTranscode)
{
await preparedTranscodeResult.TranscodeAsync();
}
else
{
if (remoteAudioPCMFile != null)
{
await remoteAudioPCMFile.DeleteAsync();
}
if (remoteAudioMP3File != null)
{
await remoteAudioMP3File.DeleteAsync();
}
switch (preparedTranscodeResult.FailureReason)
{
case TranscodeFailureReason.CodecNotFound:
Debug.LogError("Codec not found.");
break;
case TranscodeFailureReason.InvalidProfile:
Debug.LogError("Invalid profile.");
break;
default:
Debug.LogError("Unknown failure.");
break;
}
}
【问题讨论】:
-
您的开关盒中是否有任何日志?您是否尝试过转码?
-
您好,我测试了一些不同类型的音频文件,只有 pcm 无法正确转码,并且无法提供有效的错误信息。
MediaTranscode现在可能无法直接转码 pcm 音频文件。我正在寻求工程师的帮助,看看是否有其他解决方案。 -
@LouisGarczynski 是的,它转到默认情况并打印“未知故障”。
-
@RichardZhang-MSFT 好的,谢谢!对此,我真的非常感激!我希望先使用 ffmpeg,但您无法在 UWP 中启动外部进程。
-
如果您可以访问该文件,为什么不使用任何类型的在线转换器来获取 mp3?
标签: c# unity3d audio uwp ms-media-foundation