Friday, April 29, 2011

How can I use lame to encode wav files within a shell script?

I'm trying to set artist information via variables with spaces in them. Lame craps out. Maybe I'm being retarded with bash?

#!/bin/bash
year=2008;
artist="New Kids On The Block";
album="The Block";
bitrate=320;
lame="lame -b $bitrate --ta \"$artist\"  --tl \"$album\" --ty $year"

function first_half
{
    for (( i=1;i<10;i++ )); do
        $lame "track_0$i.wav" "track_0$i.mp3";
    done;
}

function second_half
{
    for (( x=10;x<18;x++ )); do
        echo $lame "track_$x.wav" "track_$x.mp3";
    done;
}

first_half &
first_pid=$!

#second_half &
#second_pid=$

Here's the output of the script.

user@host:~/ogg/noartist/unknown_disc$ ./encode.sh 
user@host:~/ogg/noartist/unknown_disc$ lame: excess arg The
lame: excess arg The

Lame complains for each loop iteration... of course.

I changed the script to echo out one of the iterations of the loop and this is what is outputted.

lame -b 320 --ta "New Kids On The Block" --tl "The Block" --ty 2008 track_01.wav track_01.mp3

This works line fine on the shell... I'm confused. What am I doing wrong here? I know it has to do with the spaces in my variables, but I'm not sure how to fix it.

Thanks :)

From stackoverflow
  • I found a temporary solution that I used...

    It's a bit of a hack, but it does the job:

    #!/bin/bash
    year="2008";
    artist="\"New Kids On The Block\"";
    album="\"The Block\"";
    bitrate=320;
    genre="Pop";
    lame="lame -b $bitrate --ta $artist  --tl $album --ty $year --tg $genre"
    
    function first_half
    {
        echo "Encoding first half...";
        for (( i=1;i<10;i++ )); do
            echo $lame "track_0$i.wav" "track_0$i.mp3" > run1.sh;
            bash run1.sh >/dev/null 2>/dev/null;
        done;
        rm -f run1.sh;
    }
    
    function second_half
    {
        echo "Encoding second half too...";
        for (( x=10;x<18;x++ )); do
            echo $lame "track_$x.wav" "track_$x.mp3" >run2.sh;
            bash run2.sh >/dev/null 2>/dev/null;
        done;
        rm -f run2.sh;
    }
    
    first_half &
    echo $! > first_half.pid
    
    second_half
    echo $! > second_half.pid
    
    echo "Done!";
    rm *.pid -f
    
  • The problem is the line

    lame="lame -b $bitrate --ta \"$artist\"  --tl \"$album\" --ty $year"
    

    because $lame later is evaluated more than once. You can run

    bash -xv ./encode.sh
    

    to see commands executed and variables substituted (instead of running "bash -xv" you can add "set -xv" inside the script).

  • "lame" should be a function. Note: I ran "lame" in the same directory, "./lame", so I could use another script to test the results.

    #!/bin/bash
    year=2008
    artist="New Kids On The Block"
    album="The Block"
    bitrate=320
    
    function lame()
    {
    #local bitrate=$1
    #local artist=$2
    #local album=$3
    #local year=$4
    local in=$1
    local out=$2
    ./lame -b "$bitrate" --ta "$artist" --tl "$album" --ty "$year" "$in" "$out"
    }
    
    function first_half
    {
        for (( i=1;i<10;i++ )); do
            lame "track_0$i.wav" "track_0$i.mp3"
        done
    }
    
    first_half &
    first_pid=$!
    

    lame:

    #!/bin/bash
    
    echo ===============================================
    echo $0 $*
    echo "0 ==> \"$0\""
    
    CNT=1
    while true; do
      echo -n "$CNT "
      [ $CNT -lt 10 ] && echo -n " "
      echo "==> \"$1\""
      CNT=$(($CNT + 1))
      shift
      [ -z "$1" ] && break
    done
    
    echo ===============================================
    

    Sample output (partial):

    ===============================================
    ./lame -b 320 --ta New Kids On The Block --tl The Block --ty 2008 track_01.wav track_01.mp3
    0 ==> "./lame"
    1  ==> "-b"
    2  ==> "320"
    3  ==> "--ta"
    4  ==> "New Kids On The Block"
    5  ==> "--tl"
    6  ==> "The Block"
    7  ==> "--ty"
    8  ==> "2008"
    9  ==> "track_01.wav"
    10 ==> "track_01.mp3"
    ===============================================
    ===============================================
    ./lame -b 320 --ta New Kids On The Block --tl The Block --ty 2008 track_02.wav track_02.mp3
    0 ==> "./lame"
    1  ==> "-b"
    2  ==> "320"
    3  ==> "--ta"
    4  ==> "New Kids On The Block"
    5  ==> "--tl"
    6  ==> "The Block"
    7  ==> "--ty"
    8  ==> "2008"
    9  ==> "track_02.wav"
    10 ==> "track_02.mp3"
    ===============================================
    

0 comments:

Post a Comment