如何在Mac终端中使用SoX从文本文件中修剪特定时间的音频文件? [英] how to trim audio file with specific time from text file by using SoX in Mac terminal?
问题描述
我有一个文本文件,看起来像这样 文本文件 ,并且我想使用sox根据文本文件中的时间来修剪音频文件,因此我可以使用从0.0到6.16、6.16到13.44、13.44到17.54等不同的音频剪辑.
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..
我知道修整袜子的基本脚本是*$ sox audio.wav newaudio.wav trim starttime duration*
但是如何从文本文件中获取持续时间并使用sox修整音频呢?
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?
推荐答案
更新后的答案
我认为以下代码可能更接近
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
我不使用 sox ,但这是方法的要点-它假定您的时间在名为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
示例输出
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
因此,您将所有代码保存在名为$HOME/trimmer
的文件中,然后启动 Terminal 并运行以下命令以使脚本可执行-只需一次即可:
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
如果您喜欢它的外观,请从脚本中删除单词echo
,然后再次运行它.如果您不熟悉脚本,请先进行备份.
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.
这篇关于如何在Mac终端中使用SoX从文本文件中修剪特定时间的音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!