我有一个.ogg文件,我需要将其转换为.wav。我正在尝试使用Opus.Net(使用NAudio),但是我在OpusDecoder.Decode()中遇到此异常:
“ System.Exception:'解码失败-InvalidPacket'”
这是代码:
byte[] audioBynary = File.ReadAllBytes($"{filePath}{fileOgg}");
_decoder = OpusDecoder.Create(48000, 1);
var pcmBinary = _decoder.Decode(audioBynary, audioBynary.Length, out int decodedLenght);
WaveFileWriter.CreateWaveFile($"{filePath}{fileWav}", new WaveFileReader(new MemoryStream(pcmBinary)));
答案 0 :(得分:1)
这对我有用,我用Concentus读取了ogg / opus文件,将其字节复制到memoryStream中,然后使用它来创建RawSourceWaveStream。 我可以从RawSourceWaveStream获取一个ISampleProvider,这是您需要提供的 WaveFileWriter.CreateWaveFile16。
Voilá
var filePath = $@"C:\Users\blabla\foo\bar\";
var fileOgg = "testAudio.ogg";
var fileWav = "testAudio.wav";
using (FileStream fileIn = new FileStream($"{filePath}{fileOgg}", FileMode.Open))
using (MemoryStream pcmStream = new MemoryStream())
{
OpusDecoder decoder = OpusDecoder.Create(48000, 1);
OpusOggReadStream oggIn = new OpusOggReadStream(decoder, fileIn);
while (oggIn.HasNextPacket)
{
short[] packet = oggIn.DecodeNextPacket();
if (packet != null)
{
for (int i = 0; i < packet.Length; i++)
{
var bytes = BitConverter.GetBytes(packet[i]);
pcmStream.Write(bytes, 0, bytes.Length);
}
}
}
pcmStream.Position = 0;
var wavStream = new RawSourceWaveStream(pcmStream, new WaveFormat(48000, 1));
var sampleProvider = wavStream.ToSampleProvider();
WaveFileWriter.CreateWaveFile16($"{filePath}{fileWav}", sampleProvider);