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

£可爱£侵袭症+ 提交于 2020-01-03 03:18:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/48100477/how-to-trim-audio-file-with-specific-time-from-text-file-by-using-sox-in-mac-ter

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