views:

315

answers:

2

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#??

+2  A: 

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.

Lucas Jones
Thanks a lot this is really helpful! I guess I could run it as a thread somewhere on the server since the user doesn't need to know when it's over. How complicated is it to set up FFMpeg ?
marcgg
The main thing you need to do is download `ffmpeg.exe` and put it in a convenient folder somewhere. Then just replace the `ffmpeg.exe` in the example with the path to the where you put the EXE. It's basically one EXE and, IIRC, a DLL.The biggest problem is actually getting the right command-line arguments as FFMpeg is a *huge* program - when I've used it I've spent quite a lot of time looking through the man pages.
Lucas Jones
Thanks, I'll try this out. If it works, I'll accept this answer
marcgg
It worked, thanks. I ran into some issues, addressed here: http://stackoverflow.com/questions/1390559/how-to-get-the-output-of-a-system-diagnostics-process http://stackoverflow.com/questions/1390731/how-to-crop-a-mp3-from-x-to-xn-using-ffmpeg
marcgg
Neat. I thought that that question seemed a bit familiar... :D
Lucas Jones
A: 

You can split the file using the MP3 frame headers. Using a simple brute force search you can split the file into separate frames and create a new MP3 that is as long as you wish.

MP3 Format

ChaosPandion