Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NAudio to split mp3 file

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.

like image 233
joe kirk Avatar asked May 23 '11 07:05

joe kirk


People also ask

How do I split audio automatically?

Open an audio file in the Audio Editor. Select the Process tab. In the Split section, click Auto Split. In the Auto Split dialog, select Audio File in Active Window and click Next.


2 Answers

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.

like image 183
joe kirk Avatar answered Sep 29 '22 21:09

joe kirk


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();
    }
like image 41
Ronnie Overby Avatar answered Sep 29 '22 21:09

Ronnie Overby