2015-04-30 96 views
1

從使用SoX的簡單文本文件和剪切和音頻文件中獲取音頻時間,我遇到了一些問題。獲取時間並使用SOX切割

我有這樣一個時間列表:

0 
4.053 
8.879 
15.651 
19.684 
21.853 

我需要做出與紅襪音頻分區,如下所示:

sox NAME.wav NEW_NAME.wav trim "$time" "$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 

但我得到的時間變量的一些錯誤,任何人都可以幫我這個劇本?

謝謝你這麼多

+0

用'BC嘗試 - l'。 – Wintermute

回答

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 
+0

區別在於FILE.txt。我有一個專欄,但是感謝dekkard – Sergi

+0

是的,這就是主意 - 你把相關的價值放在同一條線上。至少它更明顯,並且可以防止忘記應該存在偶數個值。 – dekkard

+1

即使越過UUOC,任何時候你爲了操縱文本而在shell中編寫循環都會導致錯誤的處理方式。以上全部可以用'awk'{print'DURATION:',$ 2- $ 1''file'替換。 –

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