NAudio 分割 mp3 文件

发布于 2024-11-09 06:57:30 字数 140 浏览 5 评论 0原文

我对音频或 mp3 内容非常陌生,正在寻找一种方法来在 C#、asp.net 中分割 mp3 文件。在谷歌搜索了三天但没有得到太多帮助之后,我希望这里有人能给我指出正确的方向。

我可以使用 NAudio 来完成此任务吗?有相关的示例代码吗?提前致谢。

I am very new to audio or mp3 stuff, was looking for a way to have a feature to split an mp3 file in C#, asp.net. After googling for a good 3-day without much of a great help, I am hoping that somebody here can point me to a right direction.

Can I use NAudio to accomplish this? Is there any sample code for that? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

醉生梦死 2024-11-16 06:57:30

我在 C# 中分割 mp3 文件的最终解决方案是使用 NAudio。这是一个示例脚本,希望它对社区中的某个人有所帮助:

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。

My final solution to split mp3 file in c# is to use NAudio. Here is a sample script for that, hope it helps someone in the community:

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();
}

Thanks to Mark Heath's suggestion for this.

The namespace required is NAudio.Wave.

寒江雪… 2024-11-16 06:57:30

MP3 文件由一系列 MP3 帧组成(通常在开头和结尾加上 ID3 标签)。分割 MP3 文件的最简洁方法是将一定数量的帧复制到新文件中(如果很重要,也可以选择带上 ID3 标签)。

NAudio 的 MP3FileReader 类具有一个 ReadNextFrame 方法。这将返回一个 MP3Frame 类,其中包含 RawData 属性中字节数组形式的原始数据。它还包括一个 SampleCount 属性,您可以使用它来准确测量每个 MP3 帧的持续时间。

An MP3 File is made up of a sequence of MP3 frames (plus often ID3 tags on the beginning and end). The cleanest way to split an MP3 file then is to copy a certain number of frames into a new file (and optionally bring the ID3 tags along too if that is important).

NAudio's MP3FileReader class features a ReadNextFrame method. This returns an MP3Frame class, which contains the raw data as a byte array in the RawData property. It also includes a SampleCount property which you can use to accurately measure the duration of each MP3 Frame.

迷荒 2024-11-16 06:57:30

前面的答案帮助我开始了。 NAudio 是必经之路。

对于我的 PodcastTool,我需要以 2 分钟的间隔分割播客,以便更快地搜索特定位置。

下面是每 N 秒分割一个 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();
    }

The previous answers helped me get started. NAudio is the way to go.

For my PodcastTool I needed to to split podcasts at 2 minute intervals to make seeking to a specific place faster.

Here's the code to split an mp3 every N seconds:

    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();
    }
温柔少女心 2024-11-16 06:57:30

这些会很有帮助Alvas Audio(商业)和ffmpeg

these would be helpful Alvas Audio (commercial) and ffmpeg

薄荷→糖丶微凉 2024-11-16 06:57:30

如果您想分割播客,请将曲目复制到音频设备(在我的例子中为游泳标头),并包含一个由 Google 的文本转语音服务制作的小音频标头,以识别曲目。 (例如“一百个物体中的世界历史。第 15 集。第 1 条,共 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

If you want to split podcasts, copy the tracks to an audio device (swimming headers in my case) and include a little audio header made from the Text To Speech service from Google to identify the tracks. (e.g. "History of the world in a hundred objects. Episode 15. Track 1 of 4") you could check a little bash script https://github.com/pulijon/cpodcast/blob/main/cutpodcast.bash

It is prepared to add the audio header in Spanish. For other languages you should change the option -l and the string of header

gtts-cli "Corte $((10#$ntrack)) de $((10#$numtracks)). $5 " -l es --output pre_$track
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文