获取时间并使用 SOX 削减

Get times and cut with SOX

我在使用 SoX 从简单的文本文件和剪辑音频文件中获取音频时间时遇到了一点问题。

我有一个时间表:

0
4.053
8.879
15.651
19.684
21.853

而且我需要用Sox做音频分区,如下:

sox NAME.wav NEW_NAME.wav trim "$time" "$duration"

为此,我需要初始时间和持续时间。对于 Duration 变量,我从讲座中跳转一行并获得下一个值 amb 进行减法:

cat FILE.txt | while read line; 
do
end_time=`grep -A 1 $line FILE.txt | sed 1d`
start_time=$line
if [ -z "$end_time" ];
then
    end_time='21.853'
fi
#echo "This is start: $start_time"
#echo "This is end: $end_time"

duration=$(echo "$end_time-$start_time" | bc) 
#echo "DURATION: $duration"
done

但是我在 Duration 变量中遇到了一些错误,有人可以帮我处理这个脚本吗?

非常感谢

不确定它是否符合您的需要,但这样做看起来更合乎逻辑:

FILE.txt

0       4.053
8.879   15.651
19.684  21.853

yourscript.sh

cat FILE.txt | while read start_time end_time; 
do
    if [ -z "$end_time" ];
    then
        end_time='21.853'
    fi
    #echo "This is start: $start_time"
    #echo "This is end: $end_time"

    duration=$(echo "$end_time-$start_time" | bc) 
    echo "DURATION: $duration"
done

输出

DURATION: 4.053                                                                                                                                                          
DURATION: 6.772                                                                                                                                                          
DURATION: 2.169 
awk '{ if ( != 0 )printf "sox NAME.wav NEW_NAME.wav trim \"%s\" \"%s\"\n",  LastTime,  - LastTime;LastTime = ;}' YourFile

在 awk 中还不简单吗?或批处理是强制性的(使用 printf 内容和任何其他信息调整您的输出)

sox NAME.wav NEW_NAME.wav trim "0" "4.053"
sox NAME.wav NEW_NAME.wav trim "4.053" "4.826"
sox NAME.wav NEW_NAME.wav trim "8.879" "6.772"
sox NAME.wav NEW_NAME.wav trim "15.651" "4.033"
sox NAME.wav NEW_NAME.wav trim "19.684" "2.169"

就用awk吧,这种任务就是它的设计目的:

$ awk '!(NR%2){print "sox NAME.wav NEW_NAME.wav trim", p, [=10=]-p} {p=[=10=]}' file
sox NAME.wav NEW_NAME.wav trim 0 4.053
sox NAME.wav NEW_NAME.wav trim 8.879 6.772
sox NAME.wav NEW_NAME.wav trim 19.684 2.169