Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sox 14.3.1: How to trim specified amount of time off the end of a wav file?

2,007 views
Skip to first unread message

Jonathan

unread,
Jun 16, 2011, 11:45:08 AM6/16/11
to
Hi,

Sorry for this basic question, but is there a simple way to use the
trim function in Sox to remove a specified amount of time off the
*end* of a WAV file?
Let's say 2 seconds, for example.

Is it necessary to know exactly how far into the file you want to
start trimming, or is there a way to just plug in 2 seconds as a
parameter?
I have read the documentation, but I found it a bit confusing.

Thanks!
Jonathan

Scott Dorsey

unread,
Jun 16, 2011, 11:57:48 AM6/16/11
to
In article <a219505b-01bd-4238...@e35g2000yqc.googlegroups.com>,

sox infile outfile trim 0 0:0:2
--scott


--
"C'est un Nagra. C'est suisse, et tres, tres precis."

Jonathan

unread,
Jun 16, 2011, 11:58:59 AM6/16/11
to

To clarify...
I realize you can accomplish this if you use zero as the first
parameter, and then specify a start time to start trimming...
Just wondering if there's a way to supply only the amount of trim
time, which would be a little easier for me.

Jonathan

unread,
Jun 17, 2011, 3:44:59 PM6/17/11
to
On Jun 16, 11:57 am, klu...@panix.com (Scott Dorsey) wrote:
> In article <a219505b-01bd-4238-a66a-04aaaf1e3...@e35g2000yqc.googlegroups.com>,

>
> Jonathan  <gosto.do.vio...@gmail.com> wrote:
> >Hi,
>
> >Sorry for this basic question, but is there a simple way to use the
> >trim function in Sox to remove a specified amount of time off the
> >*end* of a WAV file?
> >Let's say 2 seconds, for example.
>
> >Is it necessary to know exactly how far into the file you want to
> >start trimming, or is there a way to just plug in 2 seconds as a
> >parameter?
> >I have read the documentation, but I found it a bit confusing.
>
> sox infile outfile trim 0 0:0:2
> --scott
>
> --
> "C'est un Nagra. C'est suisse, et tres, tres precis."

Thanks!

Nil

unread,
Jun 21, 2011, 6:36:25 PM6/21/11
to
On 16 Jun 2011, klu...@panix.com (Scott Dorsey) wrote in
rec.audio.pro:

> In article
> <a219505b-01bd-4238...@e35g2000yqc.googlegroups.com>
> , Jonathan <gosto.d...@gmail.com> wrote:
>>Sorry for this basic question, but is there a simple way to use
>>the trim function in Sox to remove a specified amount of time off
>>the *end* of a WAV file?
>>Let's say 2 seconds, for example.
>>
>>Is it necessary to know exactly how far into the file you want to
>>start trimming, or is there a way to just plug in 2 seconds as a
>>parameter?
>>I have read the documentation, but I found it a bit confusing.
>
> sox infile outfile trim 0 0:0:2

This doesn't appear to do what the OP wanted. Your command will trim
from the start of the file to 2 seconds in. I think what the OP wants
is to trim, say, 2 seconds off the tail.

As far as I can tell, you need to tell sox where to start the trim, so
in this case you'd have to specify the time length of the tile minus 2
seconds. Like, for a 3 minute song:

sox infile outfile 0 0:02:58

anahata

unread,
Jun 22, 2011, 4:16:19 AM6/22/11
to
On Tue, 21 Jun 2011 18:36:25 -0400, Nil wrote:

> On 16 Jun 2011, klu...@panix.com (Scott Dorsey) wrote in rec.audio.pro:
>> sox infile outfile trim 0 0:0:2
>
> This doesn't appear to do what the OP wanted. Your command will trim
> from the start of the file to 2 seconds in. I think what the OP wants is
> to trim, say, 2 seconds off the tail.

man soxeffect

"trim start [length]
Trim can trim off unwanted audio from the beginning and end of
the audio. Audio is not sent to the output stream until the
start location is reached.

The optional length parameter tells the number of samples to
output after the start sample and is used to trim off the back
side of the audio. ***Using a value of 0 for the start parameter
will allow trimming off the back side only.***"

I have to RTFM *every* time I use sox. It's very powerful, but impossible
to remember all the commands and variations thereof, and there's nearly
always some trial and error before I get it to do what I want.

--
Anahata
ana...@treewind.co.uk --/-- http://www.treewind.co.uk
+44 (0)1638 720444

alex

unread,
Jun 22, 2011, 6:40:06 AM6/22/11
to
Il 16/06/2011 17.58, Jonathan ha scritto:
> To clarify...
> I realize you can accomplish this if you use zero as the first
> parameter, and then specify a start time to start trimming...
> Just wondering if there's a way to supply only the amount of trim
> time, which would be a little easier for me.

maybe you can use the "reverse" command twice...

alex

unread,
Jun 22, 2011, 8:46:58 AM6/22/11
to

like this:

sox infile.wav outfile.wav reverse trim 30 reverse

this will trim out the last 30 seconds of audio. A bit slow since the
program need to reverse the file twice but works lossly.

anahata

unread,
Jun 22, 2011, 8:48:27 AM6/22/11
to
On Thu, 16 Jun 2011 08:58:59 -0700, Jonathan wrote:

>
> To clarify...
> I realize you can accomplish this if you use zero as the first
> parameter, and then specify a start time to start trimming... Just
> wondering if there's a way to supply only the amount of trim time, which
> would be a little easier for me.

My understanding is that Scott's answer does this.
Looking back though, I have to agree the manual isn't too clear...

alex

unread,
Jun 22, 2011, 9:26:09 AM6/22/11
to
Il 22/06/2011 14.46, alex ha scritto:
> like this:
>
> sox infile.wav outfile.wav reverse trim 30 reverse
>
> this will trim out the last 30 seconds of audio. A bit slow since the
> program need to reverse the file twice but works lossly.

hehe, sorry... LOSSLESSLY...

Nil

unread,
Jun 22, 2011, 12:31:56 PM6/22/11
to
On 22 Jun 2011, anahata <ana...@treewind.co.uk> wrote in
rec.audio.pro:

> man soxeffect
>
> "trim start [length]
> Trim can trim off unwanted audio from the beginning and end of
> the audio. Audio is not sent to the output stream until the
> start location is reached.
>
> The optional length parameter tells the number of samples to
> output after the start sample and is used to trim off the back
> side of the audio. ***Using a value of 0 for the start
> parameter will allow trimming off the back side only.***"
>
> I have to RTFM *every* time I use sox. It's very powerful, but
> impossible to remember all the commands and variations thereof,
> and there's nearly always some trial and error before I get it to
> do what I want.

Yes, that's what the manual says, but it's language is a little
misleading. What it does is remove everything BUT the tail. At least
that's what's happening for me, with version 14.3.0 for Windows. The
command

sox test.wav test1.wav trim 0 5

...leaves me with only the last 5 seconds of the file, rather than
removing the last 5 seconds.

Nil

unread,
Jun 22, 2011, 12:39:36 PM6/22/11
to
On 22 Jun 2011, alex <gra...@yahoo.com> wrote in rec.audio.pro:

> like this:
>
> sox infile.wav outfile.wav reverse trim 30 reverse
>
> this will trim out the last 30 seconds of audio. A bit slow since
> the program need to reverse the file twice but works lossly.

You're right - I find that very recommendation in the manual:

"Unfortunately, you must know the length of the silence at the end of
your audio file to trim off silence reliably. A work around is to
use the silence effect in combination with the reverse effect. By
first reversing the audio, you can use the above-periods to reliably
trim all audio from what looks like the front of the file. Then
reverse the file again to get back to normal."

I just tried it and it seems to work perfectly.

Jonathan

unread,
Jun 22, 2011, 2:55:25 PM6/22/11
to
On Jun 22, 12:31 pm, Nil <redno...@REMOVETHIScomcast.net> wrote:
> On 22 Jun 2011, anahata <anah...@treewind.co.uk> wrote in

Yeah...I discovered that :)

alex

unread,
Jun 22, 2011, 4:33:39 PM6/22/11
to

really? i don't readed the manual, i just searched for a "reverse"
command...
i don't expected this situation to be explained in the man.
In scripting terms the most reliable workaround will be allowing some
"algebra" in the parameters like this: "trim (end-30)"...

regards
alex

Nil

unread,
Jun 22, 2011, 6:57:04 PM6/22/11
to

Yes, it's in there, although the description is for a slightly
different scenario. It describes using the "silence" command, which
supposedly will delete all silence at the end of the track (which may
be what the OP wants, anyway.) "Reverse" works with the "trim" command,
too - I tried it. The command line:

sox test.wav test1.wav reverse trim 10 reverse

...removes exactly the last 10 seconds of the file. I was worried that
the "reverse" command might do something negative to the file, but I
tested it by comparing a double-reversed version with the original, and
they are byte-for-byte identical.

alex

unread,
Jun 22, 2011, 7:03:43 PM6/22/11
to
Il 23/06/2011 0.57, Nil ha scritto:
> ...removes exactly the last 10 seconds of the file. I was worried that
> the "reverse" command might do something negative to the file, but I
> tested it by comparing a double-reversed version with the original, and
> they are byte-for-byte identical.
yes because sox, after the initial decoding (if the file is encoded),
will save temp pcm files and does not re-encode it on every passage.
This make the "double reverse" method completely lossless against the
single processing step of the trim command. Sadly, expecially on large
files, this will lead to a slower processing task because the temp files
will be written 3 times instead of 1.
0 new messages