Search code examples
c#asp.netfilemp3crop

How to crop a mp3 in ASP.NET + C#?


I am in an ASP.NET project using C#. The users can upload their MP3 (I have no control over the encoding) and specify a sample size and a sample starting point. On save, the system needs to create a sample of this MP3 based on the information provided.

So the question is: How can Icrop a mp3 in ASP.NET + C#??


Solution

  • While you could possibly use a .NET audio library to do this, I think the easiest way to do this would be to run a command like FFMpeg or MPlayer to do the cropping for you, then send the file back down the line.

    For example, with FFMpeg, you can do something like this (from here crops up to the 90th second):

    ffmpeg -ss 90 -i input.mp3 output.mp3
    

    To start FFMpeg, use something like this:

    System.Diagnostics.Process p = new System.Diagnostics.Process();
    Response.Write("Cutting MP3...");
    Response.Flush();
    p.StartInfo = new System.Diagnostics.ProcessStartInfo("ffmpeg.exe", "-s 90 -i " + inputFile + " " + outputFile);
    p.Start();
    p.WaitForExit();
    Response.Write("Done");
    

    The only problem is that it takes quite a while and it's hard to report progress back to the user.