C# 使用带C的MediaTranscoder将PCM音频转换为MP3#

C# 使用带C的MediaTranscoder将PCM音频转换为MP3#,c#,unity3d,audio,uwp,ms-media-foundation,C#,Unity3d,Audio,Uwp,Ms Media Foundation,我正在尝试对从WebRTC调用保存的PCM格式音频文件进行转码。WebRTC报告的音频流格式为16位深度、1个通道和48000 Hz采样率。我想将音频转换为MP3,这样我就可以在以后将音频作为背景曲目添加到Unity UWP应用程序的屏幕录制中(使用MediaComposition)。我在第一部分遇到了问题:试图将PCM音频文件转换为MP3文件preparedTranscode结果。当我尝试准备转码时,CanTranscode返回false。下面是我的代码 StorageFile remoteA

我正在尝试对从WebRTC调用保存的PCM格式音频文件进行转码。WebRTC报告的音频流格式为16位深度、1个通道和48000 Hz采样率。我想将音频转换为MP3,这样我就可以在以后将音频作为背景曲目添加到Unity UWP应用程序的屏幕录制中(使用MediaComposition)。我在第一部分遇到了问题:试图将PCM音频文件转换为MP3文件
preparedTranscode结果。当我尝试准备转码时,CanTranscode
返回
false
。下面是我的代码

StorageFile remoteAudioPCMFile=wait-StorageFile.getfilefrompathsync(Path.Combine(Application.temporaryCachePath,“remote.pcm”)。替换(“/”,“\\”);
StorageFolder tempFolder=await StorageFolder.GetFolderFromPathAsync(Application.temporaryCachePath.Replace(“/”,“\\”);
StorageFile remoteAudioMP3File=wait tempFolder.CreateFileAsync(“remote.mp3”,CreationCollisionOption.ReplaceExisting);
MediaEncodingProfile=MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Auto);
profile.Audio.BitsPerSample=16;
profile.Audio.ChannelCount=1;
profile.Audio.SampleRate=48000;
MediaTranscoder transcoder=新的MediaTranscoder();
var preparedTranscode结果=等待转码器。prepareFileTranscode异步(remoteAudioPCMFile、remoteAudioMP3File、profile);
if(准备好的Transcode结果。CanTranscode)
{
等待准备好的Transcode结果。Transcode异步();
}
其他的
{
如果(remoteAudioPCMFile!=null)
{
等待remoteAudioPCMFile.DeleteAsync();
}
如果(remoteAudioMP3File!=null)
{
等待remoteAudioMP3File.DeleteAsync();
}
开关(准备好的Transcode结果。故障原因)
{
案例代码转换失败原因.codeConnotfound:
LogError(“未找到编解码器”);
打破
案例代码转换失败原因.InvalidProfile:
LogError(“无效配置文件”);
打破
违约:
LogError(“未知故障”);
打破
}
}

因此,在开始将数据写入流之前,我要做的是将头写入我的
文件流中。我是从你那儿得到的

private void WriteWavHeader(文件流、bool isFloatingPoint、ushort channelCount、ushort bitDepth、int sampleRate、int totalSampleCount)
{
流位置=0;
//里夫头。
//区块ID。
stream.Write(Encoding.ASCII.GetBytes(“RIFF”),0,4);
//块大小。
stream.Write(BitConverter.GetBytes((bitDepth/8*totalSampleCount)+36),0,4;
//格式。
stream.Write(Encoding.ASCII.GetBytes(“WAVE”),0,4);
//子块1。
//子块1 ID。
stream.Write(Encoding.ASCII.GetBytes(“fmt”),0,4);
//子块1大小。
stream.Write(位转换器.GetBytes(16),0,4);
//音频格式(浮点(3)或PCM(1))。任何其他格式表示压缩。
stream.Write(BitConverter.GetBytes((ushort)(isFloatingPoint?3:1)),0,2);
//频道。
stream.Write(位转换器.GetBytes(channelCount),0,2);
//抽样率。
stream.Write(位转换器.GetBytes(sampleRate),0,4);
//字节速率。
stream.Write(BitConverter.GetBytes(sampleRate*channelCount*(bitDepth/8)),0,4);
//块对齐。
stream.Write(BitConverter.GetBytes(channelCount*(bitDepth/8)),0,2);
//每个样本的位数。
stream.Write(位转换器.GetBytes(位深度),0,2);
//分块2。
//子块2 ID。
stream.Write(Encoding.ASCII.GetBytes(“数据”),0,4);
//子块2大小。
stream.Write(BitConverter.GetBytes(bitDepth/8*totalSampleCount),0,4);
}

您的交换机机箱中是否有任何日志?你试过转码吗?嗨,我测试了一些不同类型的音频文件,只有pcm不能正确转码,并且不能提供有效的错误消息
MediaTranscode
现在可能无法直接对pcm音频文件进行转码。我正在寻求工程师的帮助,看看是否还有其他解决方案。@LouisGarczynski是的,它将转到默认情况并打印“未知故障”。@RichardZhang MSFT好的,谢谢!我真的很感激!我想先用ffmpeg,但你不能在UWPs中启动外部进程。如果你有权访问该文件,为什么不使用任何在线转换器来获取mp3呢?