tags:

views:

1993

answers:

7

Original Question

I want to be able to generate a new (fully valid) MP3 file from an existing MP3 file to be used as a preview -- try-before-you-buy style. The new file should only contain the first n seconds of the track.

Now, I know I could just "chop the stream" at n seconds (calculating from the bitrate and header size) when delivering the file, but this is a bit dirty and a real PITA on a VBR track. I'd like to be able to generate a proper MP3 file.

Anyone any ideas?

Answers

Both mp3split and ffmpeg are both good solutions. I chose ffmpeg as it is commonly installed on linux servers and is also easily available for windows. Here's some more good command line parameters for generating previews with ffmpeg

  • -t <seconds> chop after specified number of seconds
  • -y force file overwrite
  • -ab <bitrate> set bitrate e.g. -ab 96k
  • -ar <rate Hz> set sampling rate e.g. -ar 22050 for 22.05kHz
  • -map_meta_data <outfile>:<infile> copy track metadata from infile to outfile

instead of setting -ab and -ar, you can copy the original track settings, as Tim Farley suggests, with:

  • -acodec copy
+2  A: 

I haven't used it for this specific purpose, but I bet ffmpeg can do it.

grapefrukt
+11  A: 

try:

ffmpeg -t 30 -i inputfile.mp3 outputfile.mp3
John Boker
+2  A: 

You might want to try Mp3Splt.

I've used it before in a C# service that simply wrapped the mp3splt.exe win32 process. I assume something similar could be done in your Linux/PHP scenario.

Ryan Duffield
+1  A: 

I have used this: http://mp3splt.sourceforge.net/mp3splt_page/home.php before with good results

Unkwntech
+2  A: 

Just a thought: you may want to skip the beginning of the original song. Say, you can use 30 seconds piece starting at the third of the song.
In some songs, the first 30 seconds doesn't tell you much as it's just a "setting up the scene" part - for instance Pink Floyd's Shine On You Crazy Diamond.

Tomas Sedovic
+1 great song!
alex
+14  A: 

I also recommend ffmpeg, but the command line suggested by John Boker has an unintended side effect: it re-encodes the file to the default bitrate (which is 64 kb/s in the version I have here at least). This might give your customers a false impression of the quality of your sound files, and it also takes longer to do.

Here's a command line that will slice to 30 seconds without transcoding:

ffmpeg -t 30 -acodec copy -i inputfile.mp3 outputfile.mp3

The -acodec switch tells ffmpeg to use the special "copy" codec which does not transcode. It is lightning fast.

Tim Farley
+1  A: 

If you wish to REMOVE the first 30 seconds (and keep the remainder) then use this:

ffmpeg -ss 30 -acodec copy -i inputfile.mp3 outputfile.mp3
the.jxc