我正在尝试对从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;
}
}