3

I have a text file looks like this text file ,and I want to use sox to trim the audio file based on the time in the text file, so I can have different audio clips from 0.0 to 6.16, 6.16 to 13.44, 13.44 to 17.54 etc..

I understand the basic script for sox trim is *$ sox audio.wav newaudio.wav trim starttime duration*
But how can I get the duration from the text file and use sox to trim the audio?

Bruce Wang
  • 33
  • 3
  • Please don't add code or formatted data to comments - it is unintelligible. Instead, click `edit` under your question and update it so it accurately reflects your question. Select code and use `{}` button to format it. Thank you. – Mark Setchell Jan 04 '18 at 17:55
  • Sorry, Mark.. This is my first time post things here. I will be careful with it next time.. I am changing it now.. – Bruce Wang Jan 04 '18 at 17:57
  • No problems, you are welcome. Good luck with your project and remember questions, and answers, are free - so come back and ask more if you get stuck. – Mark Setchell Jan 08 '18 at 13:03
  • Thanks a lot Mark! Let me know if you get a chance to visit Newcastle, I owe you a pint! – Bruce Wang Jan 12 '18 at 23:24

1 Answers1

0

Updated Answer

I think the following code may be closer:

#!/bin/bash
index=0
while read this; do
   if [ $index -gt 0 ]; then
      d=$(bc <<< "$this - $start")
      echo sox audio.wav new-$index.wav trim \"$start\" \"$d\"
   fi
   ((index+=1))
   start=$this
done < times.txt

I don't use sox, but this is the gist of the approach - it assumes your times are in a file called times.txt:

#!/bin/bash
index=0
while read this; do
   if [ $index -gt 0 ]; then
      echo sox audio.wav new-$index.wav trim $start $this
   fi
   ((index+=1))
   start=$this
done < times.txt

Sample Output

sox audio.wav new-1.wav trim 0.0 6.16
sox audio.wav new-2.wav trim 6.16 13.44
sox audio.wav new-3.wav trim 13.44 17.54
sox audio.wav new-4.wav trim 17.54 23.3
sox audio.wav new-5.wav trim 23.3 26.5

So you would save all that code in file called $HOME/trimmer then start Terminal and run the following to make the script executable - just once is sufficient:

chmod +x $HOME/trimmer

Then change directory to where your audio files are, e.g.:

cd path/to/audio/files

then run the script with:

$HOME/trimmer

If you like how it looks, remove the word echo from the script and run it again. Please make a backup first if you are unfamiliar with scripting.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks a lot Mark! But I want my Sample output to look like this, {sox audio.wav new-1.wav trim "0.0" "6.16" sox audio.wav new-2.wav trim "6.16" "7.28 (13.44 minus 6.16)" sox audio.wav new-3.wav trim "13.44" "4.1" sox audio.wav new4.wav trim "17.54" "5.76"}. So the second time in sox trim is actually the duration of the clip you want it to be, not to the second. I just changed {$this} into {$this - $start}. – Bruce Wang Jan 04 '18 at 20:30
  • Oh, ok, it's late in the UK, so I'll have a look tomorrow if I get a moment. – Mark Setchell Jan 04 '18 at 20:49