c# - WAV 文件修剪和添加静音

c# - WAV file trimming and adding silence

我有两个相同事件的录音,不同的长度,开始于不同的时间。我想同步它们,时间偏移是已知的。我想实现以下目标:

  1. 按时间偏移对齐第二个。
  2. Trim 第二个匹配第一个的长度
  3. 当trim没有任何内容时,添加静音以匹配第一个的长度。

    我找到了 trim 音频的方法,但找不到添加静音的解决方案。有什么办法可以用 NAudio、ffmpeg 或 Aurio 做到这一点吗?

抱歉没有回复,但我使用 ffmpeg 解决了我的问题。以下是我的做法:

ffmpeg -f lavfi -i aevalsrc=0:d=8.375 -i inputfile.wav -filter_complex "[0:0] [1:0] concat=n=2:v=0:a=1" -ss 0 -to 63.787 outputfile.wav

-i aevalsrc=0:d=8.375 生成持续时间为 (d) 的静音8.375 秒。
-filter_complex "[0:0] [1:0] concat=n=2:v=0:a=1 合并静音和输入文件。 - Concatenation
-ss 0 -to 63.787 trims the output (from a start to the length of first file, that is 63.787, if input is shorter than that, output file has input file duration) - Seeking

如果偏移值为负数,则为:

ffmpeg -i inputfile.wav -ss 22.316 -t 54.213 outputfile.wav

所以输出文件是输入文件,从 22.316 秒开始持续 54.213

我使用 NReco.VideoConverter 作为 ffmpeg 包装器。

下面是使用NAudio实现毫秒级静音的方法:

public static void WriteSilence(WaveFormat waveFormat, 
   int silenceMilliSecondLength, WaveFileWriter waveFileWriter)
{
    int bytesPerMillisecond = waveFormat.AverageBytesPerSecond / 1000;
    //an new all zero byte array will play silence
    var silentBytes = new byte[silenceMilliSecondLength * bytesPerMillisecond];
    waveFileWriter.Write(silentBytes, 0, silentBytes.Length);
    waveFileWriter.Dispose();
}