我对音频或mp3等方面非常陌生,正在寻找一种在C#、asp.net中拆分mp3文件的方法。在谷歌搜索了三天后没有多少帮助,我希望这里有人能指引我正确的方向。
我可以使用NAudio来完成吗?是否有任何示例代码可供参考?先感谢您。
我在C#中用NAudio实现了分割mp3文件的最终解决方案。以下是一个示例脚本,希望能对社区中的某些人有所帮助:
string strMP3Folder = "<YOUR FOLDER PATH>";
string strMP3SourceFilename = "<YOUR SOURCE MP3 FILENAMe>";
string strMP3OutputFilename = "<YOUR OUTPUT MP3 FILENAME>";
using (Mp3FileReader reader = new Mp3FileReader(strMP3Folder + strMP3SourceFilename))
{
int count = 1;
Mp3Frame mp3Frame = reader.ReadNextFrame();
System.IO.FileStream _fs = new System.IO.FileStream(strMP3Folder + strMP3OutputFilename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
while (mp3Frame != null)
{
if (count > 500) //retrieve a sample of 500 frames
return;
_fs.Write(mp3Frame.RawData, 0, mp3Frame.RawData.Length);
count = count + 1;
mp3Frame = reader.ReadNextFrame();
}
_fs.Close();
}
感谢Mark Heath的建议。
所需的命名空间为NAudio.Wave。
一个MP3文件由一系列MP3帧组成(通常还有开头和结尾的ID3标签)。因此,最清晰的分割MP3文件的方法是将一定数量的帧复制到一个新文件中(如果重要的话,也可以将ID3标签一同复制)。
NAudio的MP3FileReader
类具有一个ReadNextFrame
方法。它返回一个MP3Frame
类,其中包含原始数据的字节数组在RawData
属性中。它还包括一个SampleCount
属性,您可以使用它来准确地测量每个MP3帧的持续时间。
var mp3Path = @"C:\Users\ronnie\Desktop\mp3\dotnetrocks_0717_alan_dahl_imagethink.mp3";
int splitLength = 120; // seconds
var mp3Dir = Path.GetDirectoryName(mp3Path);
var mp3File = Path.GetFileName(mp3Path);
var splitDir = Path.Combine(mp3Dir,Path.GetFileNameWithoutExtension(mp3Path));
Directory.CreateDirectory(splitDir);
int splitI = 0;
int secsOffset = 0;
using (var reader = new Mp3FileReader(mp3Path))
{
FileStream writer = null;
Action createWriter = new Action(() => {
writer = File.Create(Path.Combine(splitDir,Path.ChangeExtension(mp3File,(++splitI).ToString("D4") + ".mp3")));
});
Mp3Frame frame;
while ((frame = reader.ReadNextFrame()) != null)
{
if (writer == null) createWriter();
if ((int)reader.CurrentTime.TotalSeconds - secsOffset >= splitLength)
{
// time for a new file
writer.Dispose();
createWriter();
secsOffset = (int)reader.CurrentTime.TotalSeconds;
}
writer.Write(frame.RawData, 0, frame.RawData.Length);
}
if(writer != null) writer.Dispose();
}
如果你想要分割播客,可以将音轨复制到一个音频设备上(在我的情况下是游泳耳机),并使用 Google 的 Text To Speech 服务创建一个小的音频头来标识音轨。(例如,“一百件物品的世界历史。第15集。4个音轨之一。”)你可以查看一个小的 bash 脚本 https://github.com/pulijon/cpodcast/blob/main/cutpodcast.bash。
该脚本已准备好添加西班牙语的音频头。对于其他语言,您需要更改选项 -l 和头字符串。
gtts-cli "Corte $((10#$ntrack)) de $((10#$numtracks)). $5 " -l es --output pre_$track