I am trying to transcode a PCM formatted audio file that I saved from a WebRTC call. The format of the audio stream, reported by WebRTC, is 16 bit depth, 1 channel and 48000 Hz sample rate. I want to convert the audio to MP3 so I can add the audio as a background track to a screen recording of my Unity UWP app afterwards (using MediaComposition). I'm having trouble with the first part: trying to transcode my PCM audio file to a MP3 file. preparedTranscodeResult.CanTranscode
is returning false
when I try to prepare the transcode. The following is my code.
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;
}
}