gpt4 book ai didi

c# - 如何更改 .wav 格式音频文件比特率

转载 作者:太空宇宙 更新时间:2023-11-03 15:12:25 28 4
gpt4 key购买 nike

在我的应用程序中我有一些 .wav 格式的音频文件,这里我使用 Naudio dll 检查音频文件比特率,如果比特率低于 128kbps 那么我想将其更改为高于 128kpbs,所以我在下面写了检查比特率的代码,如果比特率低于 128kbps,那么它会转换为高于 128kbps。

int bitrate;
using (var reader = new WaveFileReader(textBox1.Text))
{
bitrate = reader.WaveFormat.AverageBytesPerSecond * 8;
reader.Dispose();
}

if (bitrate < 128000)
{
using (var reader = new WaveFileReader(textBox1.Text))
{
var newFormat = new WaveFormat(8000, 16, 1);
using (var conversionStream = new WaveFormatConversionStream(newFormat, reader))
{
WaveFileWriter.CreateWaveFile(@"C:\Docs\Files\", conversionStream);
}
}
}

对于某些文件,它工作正常,但对于某些文件,我遇到以下错误,

An unhandled exception of type 'NAudio.MmException' occurred in NAudio.dll Additional information: AcmNotPossible calling acmStreamOpen

我在这里附上错误快照。错误 Error Snap在这里,我该如何解决这个问题?

最佳答案

我建议你看看FFmpeg .这是我用于所有音频/视频转换任务的工具。

这是一个命令行工具,可以从几乎任何东西转换成任何东西,有很多选项。要做你想做的事,你可能需要运行类似的东西:

$ ffmpeg -i input.wav -ab 128 output.wav

在上面的行中,我们将文件转换为 128 比特率。

在代码中使用它的最简单方法是将 FFmpeg 可执行文件包含在您的项目中(或作为环境变量全局安装)并直接调用它,例如:

Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = "ffmpeg";

process.StartInfo.Arguments = $"-i \"{originalFile}\" -ab 128 \"{outputPath}\"";

process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.Start();
process.WaitForExit();

还有更优雅的解决方案 - FFmpeg 的包装器 - 但这应该可以解决问题。

关于c# - 如何更改 .wav 格式音频文件比特率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40522265/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com