17

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).
Thor
  • 45,082
  • 11
  • 119
  • 130
heath sargent
  • 213
  • 1
  • 3
  • 7
  • Ok, i did the V verbose or verbatim or whatever start :P – heath sargent Sep 24 '12 at 10:33
  • Theres still a problem... When i check the size of the audio, with a different program (because SoX only checks to the second decimal place) its about 0-99MilliSeconds off, is it possible to fix this? – heath sargent Sep 24 '12 at 12:07
  • Yeah, but, SoX doesn't show down to the millisecond, so i can't see if its accurate in SoX, thats why i checked it in another program :P and it was 2.036 seconds long. Also, how can i stop mixing from changing the volume? – heath sargent Sep 24 '12 at 12:38
  • wav files are massive and ogg arnt supported in what im doing, my program has over 640 sound files. I could, encode it to .wav, trim, then encode it back prehaps, would this work? – heath sargent Sep 24 '12 at 15:22
  • Looks like i won't be able to finnish my music program.. unless theres a C++ library that can join or trim sounds better :P Actually can you make SoX overlay another sound after you have mixed it, so sound1 goes for 2.036 seconds, and sound 2 starts a 00:00:02.000 even though the other part hasnt stopped yet? – heath sargent Sep 25 '12 at 00:50
  • The extra samples in the mp3 file are an artifact of the encoding and will be removed by the decoder if encoder/decoder are compatible. [A similar question has been asked before](http://superuser.com/questions/400790/covert-wav-to-mp3-without-adding-silence-at-the-beginning). – Thor Sep 25 '12 at 08:27
  • Ok, so how can i make the encoder and decode compatible, i am inputting mp3 and outputting mp3. i tried using wav files, but it seems the dll i use only allows up to 500 wav files in the program :/ – heath sargent Sep 25 '12 at 12:24
  • If its just data, and not actual audio, why does it increase the actual playtime, can't it just be separate data? :/ – heath sargent Sep 26 '12 at 14:38
  • I don't know. I suggest you do all your processing in PCM and encode to mp3 later. – Thor Sep 26 '12 at 14:53

3 Answers3

26

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.
Thor
  • 45,082
  • 11
  • 119
  • 130
  • Nope, neither worked for me :S The file ends up being 0 seconds long – heath sargent Sep 24 '12 at 09:35
  • There is no soxi.exe file in my extraction :S – heath sargent Sep 24 '12 at 09:43
  • I have 1.4.4.0 btw I tried: sox tracks\01_1.mp3 ntracks\01_1.mp3 trim 2.0 0.19 But it just set the file to 0 i even tried sox tracks\01_1.mp3 ntracks\01_1.mp3 trim 00:00:02.0 00:00:00.19 – heath sargent Sep 24 '12 at 09:54
  • I want to pass a list of [[start, end ]] times and an audio so that those portions are silenced out. what would be best way to achieve this using sox or ffmpeg? i.e – kRazzy R Jan 09 '18 at 05:02
  • @kRazzyR: Post this as a new question adhering to the [MCVE](https://stackoverflow.com/help/mcve) guidelines – Thor Jan 10 '18 at 00:13
  • @Thor Hi! Yes. I posted a new question too : https://stackoverflow.com/questions/48161567/silence-out-regions-of-audio-based-on-a-list-of-time-stamps-using-sox-and-pyth – kRazzy R Jan 10 '18 at 05:41
  • Specific thanks / upvote to whoever wrote "`If you want to trim 195 milliseconds from the end of the file, use gerald's answer.`" Perhaps it was @Thor? – Nathan majicvr.com Jun 18 '20 at 00:18
12

Have you tried using the reverse command like this:

sox tracks\5_7.mp3 ntracks\05_7.mp3 reverse trim 0.195 reverse

This should throw away the last 0.195 milliseconds of the file (it works for me).

gerald
  • 129
  • 1
  • 2
7

you should use like this to trim in milliseconds

trim 26.26 =32.3245678 

which means it will trim 26.26 seconds to exactly up to 32.3245678 seconds

this will give you the accurate result

with this = option we need to give end of the time instead of the duration with preceding of =

I hope this will resolve your problem

Luke
  • 20,878
  • 35
  • 119
  • 178
Ram ch
  • 1,633
  • 1
  • 13
  • 5