Trim audio files with Sox in milliseconds

heath sargent picture heath sargent · Sep 24, 2012 · Viewed 31k times · Source

Is there a way to trim the end of a file with SoX, in milliseconds, i have tried this:

sox tracks\5_7.mp3 ntracks\05_7.mp3 trim 2 2.195

But, it just says End position is after expected end of audio Even though the file is actually that long.

So im trying to make this song 2.000 seconds long, instead of 2.195, also, im doing this with 600+ files, so it needs to be done from the command line.

Heres my warnings:

sox WARN mp3: MAD lost sync"(this always happens, means nothing) 
sox WARN trim: Last 1 position(s) not reached (audio shorter than expected)
sox WARN sox: 'tracks\01_1.mp3' balancing clipped 3 samples; decrease volume?

This audio file goes for "00:00:02.19".

The line entered into the command line was:

sox -v3 tracks\01_1.mp3 ntracks\01_1.mp3 trim 2 0.19 pause

Here is my verbose start:

D:\Stuff\Habbo trax maker\trax maker\mp3support>sox -V3 tracks\01_1.mp3 ntracks\
01_1.mp3 trim 2 0.19
sox:      SoX v14.4.0

Input File     : 'tracks\01_1.mp3'
Channels       : 1
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:00:02.19 = 96755 samples = 164.549 CDDA sectors
File Size      : 17.6k
Bit Rate       : 64.0k
Sample Encoding: MPEG audio (layer I, II or III)

sox INFO sox: Overwriting `ntracks\01_1.mp3'
sox INFO mp3: using MP3 encoding defaults

Output File    : 'ntracks\01_1.mp3'
Channels       : 1
Sample Rate    : 44100
Precision      : 24-bit
Sample Encoding: MPEG audio (layer I, II or III)
Comment        : 'Processed by SoX'

sox INFO sox: effects chain: input        44100Hz  1 channels
sox INFO sox: effects chain: trim         44100Hz  1 channels
sox INFO sox: effects chain: output       44100Hz  1 channels
sox WARN mp3: MAD lost sync
sox WARN trim: Last 1 position(s) not reached (audio shorter than expected).

Answer

Thor picture Thor · Sep 24, 2012

If you want to trim with millisecond precision, specify it:

sox in.mp3 out.mp3 trim 2 0.195

Saves 195 milliseconds with an offset of 2 seconds of in.mp3 to out.mp3.

If you only want the first two seconds of the file use trim like this:

sox in.mp3 out.mp3 trim 0 2

If you want to skip the first two seconds of the file use trim like this:

sox in.mp3 out.mp3 trim 2

If you want to trim 195 milliseconds from the end of the file, use gerald's answer.

Excerpt from the manual:

trim start [length|=end]
   The optional length parameter gives the length of audio to output after the 
   start sample and is thus used to trim off the end of the audio.
   Alternatively, an absolute end location can be given by preceding it with 
   an equals sign. Using a value of 0 for the start parameter will allow trimming
   off the end only.