Trim MP3 to first 30 seconds

Original question

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

Now I know that when delivering the file, I could just "chop the stream" in n seconds (calculation from the bitrate and the size of the header), but this is a bit dirty and real PITA on the VBR track. I would like to create a proper MP3 file.

Any ideas?

The answers

Both mp3split and ffmpeg are good solutions. I chose ffmpeg as it is usually installed on Linux servers and is also easily accessible for Windows . Here are some more good command line options for generating previews with ffmpeg

  • -t <seconds> chop after the specified number of seconds
  • -y force overwrite the file.
  • -ab <bitrate> set the bitrate, for example. -ab 96k
  • -ar <rate Hz> set the sampling rate, for example. -ar 22050 for 22.05 kHz
  • -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
+96
ffmpeg mp3
Sep 04 '08 at 14:35
source share
11 answers

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

Here is the command line that will cut to 30 seconds without recoding:

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

The -acodec switch tells ffmpeg to use a special “copyable” codec that does not re-encode. It is lightning fast.

NOTE: the team has been updated based on a comment from Oben Sonne

+129
Sep 04 '08 at 15:42
source share

If you want to DELETE the first 30 seconds (and save the remainder), use this:

 ffmpeg -ss 30 -i inputfile.mp3 -acodec copy outputfile.mp3 
+52
May 27 '10 at 23:30
source share

to try:

 ffmpeg -t 30 -i inputfile.mp3 outputfile.mp3 
+17
Sep 04 '08 at 14:45
source share

you can use mp3cut:

 cutmp3 -i foo.mp3 -O 30s.mp3 -a 0:00.0 -b 0:30.0 

This is in the ubuntu repo, so it's simple: sudo apt-get install cutmp3 .

+11
Sep 19 '11 at 0:15
source share

This command also works great. I cut my music from 20 seconds to 40 seconds.

-y: force output file.

 ffmpeg -i test.mp3 -ss 00:00:20 -to 00:00:40 -c copy -y temp.mp3 
+10
Jun 14 '17 at 6:03
source share

You might want to try Mp3Splt .

I used it before in a C # service that just wrapped the get32 process of mp3splt.exe. I suppose something like this could be done in your Linux / PHP script.

+5
Sep 04 '08 at 14:45
source share

Just a thought: you can skip the beginning of the original song. Let's say you can use the 30 second part, starting with the third song.
In some songs, the first 30 seconds do not tell you that it's just part of the “scene creation,” like Pink Floyd Shine On You Crazy Diamond .

+5
Sep 04 '08 at 15:18
source share

I have not used it for this specific purpose, but I'm sure ffmpeg can do this.

+2
Sep 04 '08 at 14:38
source share
+1
Sep 04 '08 at 14:46
source share

Like the note, I had a very bad time with ffmpeg, mptsplit and mp3cutter on CentOS, and they all reported "Header Missing". So, I had to go with an MP3 class and some maths to cut files.

+1
Feb 28 '13 at 19:36
source share

I have an error while doing the same

 Invalid audio stream. Exactly one MP3 audio stream is required. Could not write header for output file #0 (incorrect codec parameters ?): Invalid argumentStream mapping: 

The fix for me was: ffmpeg -ss 00: 02: 43.00 -t 00:00:10 -i input.mp3 -codec: a libmp3lame out.mp3

0
Dec 02 '16 at 2:41
source share



All Articles