how to trim audio file with specific time from text file by using SoX in Mac terminal?

亡梦爱人 提交于 2019-12-06 15:35:24

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!