The current project I am working on involves handling a lot of Audio files, MP3 files in particular, and FFMPEG makes it all to easy.

Now I have blogged in the past about Expression Encoder which is also a great piece of software for working with Audio and Video files but it’s beyond what I needed for this project and does not support output to .mp3 which is a must for this project.

What I needed was a lightweight, command line based tool for taking a sample of a full .mp3 file and FFMPEG was just the ticket.

Read on for some sample code,

First off you can get the source code for FFMPEG from here if you so desire but you will probably just want to grab the latest compiled .exe so just do a Bing/Google search for ffmpeg.exe or pop across to this site.

In my application I am using Uploadify (an excellent Flash based multiple file upload plugin) to upload multiple tracks and once uploaded I need a 30 second sample of the track for public listening. The full track is stored in a secure location and not available to the public unless purchased.

To do so I created the helper class below:

publicclass FFMPEG { string sampleArgs = "-i {0} -t 00:00:30 {1}";



publicvoid CreateSample(int trackId, string sourceFile) { string executable = string.Format(@"{0}tracks\secure\ffmpeg.exe", AppDomain.CurrentDomain.BaseDirectory); string targetFileName = string.Format(@"{0}tracks\samples{1}.mp3", AppDomain.CurrentDomain.BaseDirectory, trackId); string arguments = string.Format(sampleArgs, sourceFile, targetFileName); using (Process transcodeProcess = new Process()) { try { transcodeProcess.StartInfo.UseShellExecute = false; transcodeProcess.StartInfo.FileName = executable; transcodeProcess.StartInfo.Arguments = arguments; transcodeProcess.StartInfo.CreateNoWindow = true; transcodeProcess.Start(); } catch (Exception e) { ExceptionProcessor.Instance.LogException(e); } } } }

The ‘sampleArgs’ string is all that I require argument wise to take the 30 second sample.

In the CreateSample method I locate my ‘executable’, format the ‘targetFileName’ accordingly and then do a string.format on the ‘sampleArguments’.

The final bit to create the 30 second sample just fires up a new Process sets the FileName property (the path to ffmpeg,exe) and the Arguments (the formatted ‘sampleArgs’ field) and then just starts the process.

And that’s it. A simple 30 second mp3 sample file creator.

BondiGeek