Search code examples
bashsedgrepsoxbc

Get times and cut with SOX


I have a little problem taking audio times from a simple text file and cutting and audio file with SoX.

I have a time list like:

0
4.053
8.879
15.651
19.684
21.853

And I need make the audio partition with Sox, as follows:

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

To do this I need the Initial time and the Duration. For the Duration variable I jump one line from the lecture and get the next value amb make a substraction:

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

But I get some errors in the Duration variable, can anyone help me with this script?

Thank you so much


Solution

  • Not sure if it fits your needs but it looks more logical to do it this way:

    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
    

    Output

    DURATION: 4.053                                                                                                                                                          
    DURATION: 6.772                                                                                                                                                          
    DURATION: 2.169