获取时间并使用SOX切割

时间:2015-04-30 12:02:24

标签: bash sed grep sox bc

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

我有一个时间列表,如:

0
4.053
8.879
15.651
19.684
21.853

我需要使用Sox进行音频分区,如下所示:

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

要做到这一点,我需要初始时间和持续时间。对于持续时间变量,我从讲座中跳了一行,然后得到下一个值做减法:

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变量中遇到一些错误,有人可以帮我解决这个脚本吗?

非常感谢

3 个答案:

答案 0 :(得分:3)

awk '{ if ($1 != 0 )printf "sox NAME.wav NEW_NAME.wav trim \"%s\" \"%s\"\n",  LastTime, $1 - LastTime;LastTime = $1;}' 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"

答案 1 :(得分:1)

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

<强> 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 

答案 2 :(得分:0)

只需使用awk,这类任务就是它的目的:

$ awk '!(NR%2){print "sox NAME.wav NEW_NAME.wav trim", p, $0-p} {p=$0}' 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