48

Using Sox, how do I shorten an audio file by 5 seconds, trimming from the end?

For example, this is how to trim a file from the beginning:

    sox input output trim 5000

This is how to add 5 seconds of silence to the end:

    sox input output pad 0 5000
Edward Ocampo-Gooding
  • 2,782
  • 1
  • 23
  • 29
get8p
  • 709
  • 2
  • 8
  • 12
  • I asked a similar question about SoX and it got cancelled "This question is off-topic. It is closed and not accepting answers. Question: SoX Join WAV files Win Feedback: This question is off-topic because it’s about general computing hardware and software. Edit the question so it’s on-topic." – LuaStart Feb 28 '22 at 07:12

3 Answers3

68

The syntax is sox input output trim <start> <duration>

e.g. sox input.wav output.wav trim 0 00:35 will output the first 35 seconds into output.wav.

(you can know what the length is using sox input -n stat)

From the SoX documentation on the trim command:

Cuts portions out of the audio. Any number of positions may be given; audio is not sent to the output until the first position is reached. The effect then alternates between copying and discarding audio at each position. Using a value of 0 for the first position parameter allows copying from the beginning of the audio.

For example,

sox infile outfile trim 0 10

will copy the first ten seconds, while

play infile trim 12:34 =15:00 -2:00

and

play infile trim 12:34 2:26 -2:00

will both play from 12 minutes 34 seconds into the audio up to 15 minutes into the audio (i.e. 2 minutes and 26 seconds long), then resume playing two minutes before the end of audio.

Per dpwe's comment, the position values are interpreted as being relative to the previous position, unless they start with = (in which case they are relative to the start of the file) or - (in which case they are relative to the end of the file).

So, trimming five seconds off the end would be sox input output trim 0 -5

Community
  • 1
  • 1
yonix
  • 11,665
  • 7
  • 34
  • 52
  • This cuts the file to the good length but this adds an annoying click noise at the end. – John Smith Optional Mar 19 '15 at 17:15
  • 5
    You can use `sox input output trim `, or `sox input output trim =`. The full `trim` syntax accepts any number of pairs of `` arguments, which are interpreted as relative to the preceding position by default, or relative to the start of the file if preceded by '=', or relative to the end of the file (if known) if preceded by '-'. Only the sound between each pair of positions is output. – dpwe May 17 '15 at 01:37
  • 1
    See my solution below for a way to avoid a click at the end, using a tiny fade. – dingles Dec 24 '16 at 18:31
  • @yonix Would love your thoughts on : 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 23:51
12

Above command is wrong, it will get you last 5 seconds only. You actually need to use:

sox input output reverse trim 5 reverse 

which will cut 5 seconds from end of the file.

tamerzg
  • 160
  • 1
  • 3
  • 16
    I figured that there is an easier way to do it. Adding the minus will do the rest. Following will cut the last 5 sec `sox input output trim 0 -5` – get8p Feb 15 '13 at 14:09
  • 2
    Let's not reverse twice an audio stream, just to get it's end. – gb. Mar 18 '16 at 04:40
7

I'm new to SoX but have noticed this page frequently shows up in search results for audio trimming and will be seen by many trying to do similar things.

As such I wanted to provide what I have found to be the best solution personally.

I experienced the same 'click' at file end which John Smith Optional had mentioned. This suggested a brief fade out could remove any glitching artefacts as the audio finishes and sure enough it works. It's acceptance of a negative value for the fadeout position parameter to indicate the time before the end of audio is the key.

So I can see no better way to achieve the OP's aim than this:

sox full_length.wav trimmed.wav fade 0 -5 0.01

Parameter 1 is '0' so there is no fade in. Parameter 2 removes the last 5 seconds Parameter 3 uses a 10ms fade

Community
  • 1
  • 1
dingles
  • 1,548
  • 1
  • 14
  • 11
  • Providing negative value to the stoptime of `fade` is not working for me on SoX v14.4.1. – joelostblom May 19 '17 at 11:35
  • @dingles are you able to help with 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 23:52