Trim inputs in SoX to avoid making intermediate files

79 Views Asked by At

I'm trying to write a single SoX command to concatenate 2 files while avoiding making intermediate files.

My 2 input files are both 1 second long.

I want to concatenate the first 0.5 seconds of input1.aif with all of input2.aif then pad with 1s silence at the end.

I can't work out the order of options and/or chaining methods. Can't find in docs either. What I've tried so far:

$ sox input1.aif input2.aif output.aif
> input1 then input2, as expected

$ sox input1.aif input2.aif output.aif pad 0 1
> input1 then input2 then 1s silence, as expected

$ sox input1.aif input2.aif output.aif trim 0 0.5 pad 0 1
> 0.5s of input1 (as desired) but none of input2 then 1s silence

$ sox input1.aif input2.aif output.aif trim 0 0.5 trim 0 1 pad 0 1
> "sox WARN trim: End position is after expected end of audio."
> "sox WARN trim: Last 1 position(s) not reached."

$ sox input1.aif input2.aif output.aif trim 0 0.5 : trim 0 1 pad 0 1
> input 1 then input2 then 1s silence

$ sox input1.aif input2.aif output.aif trim 0 0.5 : trim 0 0.5 pad 0 1
> input 1, none of input2 then 1s silence

$ sox input1.aif input2.aif output.aif trim 0 0.5 : trim 0 1 : pad 0 1
> input 1 then input2 then 1s silence

$ sox input1.aif trim 0 0.5 : input2.aif output.aif pad 0 1           
> sox FAIL sox: Not enough input filenames specified

What is the correct syntax for my required behavior?

1

There are 1 best solutions below

0
Dan Weaver On

Found an answer.

Use the pipe option (-p) to trim the first input then pipe it to the second command for the rest of the process.

$ sox input1.aif -p trim 0 0.5 | sox - input2.aif output.aif pad 0 1

The takes the first input and trims it to the first 0.5 seconds, then pipes it to the second command as the first argument (-) and concatenates it with the second input, then finally padding the result with 1 second of silence.

There may be other ways to do this without piping but this is simple enough for my needs.