如何在 Mac 终端中使用 sox 来 trim 子文件夹中的声音文件?
How to trim sound files in subfolders by using sox in Mac terminal?
我有1000多个文件夹,每个文件夹包含一个.wav文件和一个.txt文件(example of the folders). The text file contains the time interval, and I need to trim the .wav file into clips based on the time interval that each text file given (note that text files are in different folders). I have got the following script from
#!/bin/bash
index=0
while read this; do
if [ $index -gt 0 ]; then
sox sound.wav clip-$index.wav trim $start $this-$start
fi
((index+=1))
start=$this
done < times.txt
但是,它是针对单个文件的,只能用于当前目录下的文件。我怎样才能让它适用于子文件夹并进入循环?
基本上,您想对所有子目录执行此循环。这意味着您需要遍历子目录:
#!/bin/bash
topdir=$(pwd)
find . -type d | while read dir ; do
cd "$dir"
if [ -f sound.wav -a -f times.txt ] ; then
index=0
while read this; do
if [ $index -gt 0 ]; then
sox sound.wav "clip-$index.wav" trim "$start" "$this-$start"
fi
((index+=1))
start="$this"
done < times.txt
fi
done
我假设你问题中的循环做了你想要它做的事情,我只是添加了一些引号。
我有1000多个文件夹,每个文件夹包含一个.wav文件和一个.txt文件(example of the folders). The text file contains the time interval, and I need to trim the .wav file into clips based on the time interval that each text file given (note that text files are in different folders). I have got the following script from
#!/bin/bash
index=0
while read this; do
if [ $index -gt 0 ]; then
sox sound.wav clip-$index.wav trim $start $this-$start
fi
((index+=1))
start=$this
done < times.txt
但是,它是针对单个文件的,只能用于当前目录下的文件。我怎样才能让它适用于子文件夹并进入循环?
基本上,您想对所有子目录执行此循环。这意味着您需要遍历子目录:
#!/bin/bash
topdir=$(pwd)
find . -type d | while read dir ; do
cd "$dir"
if [ -f sound.wav -a -f times.txt ] ; then
index=0
while read this; do
if [ $index -gt 0 ]; then
sox sound.wav "clip-$index.wav" trim "$start" "$this-$start"
fi
((index+=1))
start="$this"
done < times.txt
fi
done
我假设你问题中的循环做了你想要它做的事情,我只是添加了一些引号。