Convert flac to mp3 in one go

    Good day.

    I once pumped myself here a mountain of music, not knowing what format it is, in flac or in mp3. I thought, “Well, what, there’s a bunch of utilities that convert all this, then I’ll figure it out.” I downloaded, searched the network, how I can convert it, found the All2mp3 program (I’m sitting on a poppy) ... What was my surprise when I found out that I couldn’t just drop the Music folder into it and wait for it to go over all the subfolders, find everything flac files and converts them to mp3! He began to google, but did not find any sane solution.


    I wrote a small shell script that runs through folders, looks for flac, converts them to mp3, and can remove .flac if you ask. The script was written on the knee, so the new files are called * .flac.mp3. Also, it needs installed programs to work.flac
    and lame .

    Here is the script itself:
    #!/bin/sh
    #flac2mp3
    error(){
            echo $@ 1>&2;
            exit 1;
    }
    r=0
    rem(){
            if [ $r -eq 1 ]; then
                    rm "$1"
            fi
    }
    flac2mp3(){
            data=$1
            echo "          -----------------------Working on $data"
            if [ -d "$data" ]; then
                    for file in "$data"*; do
                            if [ -d "$file" ]; then
                                    file="$file"/
                            fi
                            flac2mp3 "$file"
                            wait
                    done
            elif [[ "$data" == *.flac ]]; then 
                    flac -d --stdout "$data" > temp.wav
                    wait
                    echo "          ---------------------------------$data to wav done"
                    lame temp.wav "$data".mp3
                    wait
                    echo "          ---------------------------------$data to $data.mp3 done"
                    rm temp.wav
                    rem "$data"             
            fi
    }
    if [ $# -eq 0 ]; then
            error "Wrong arguments"
    fi
    echo ============================================================
    if [ $# -eq 1 ]; then
            flac2mp3 "$1"
    elif [ $# -eq 2 ] && [ $1 == "-r" ]; then
            r=1
            flac2mp3 "$2"
    else
            error "Wrong arguments!"
    fi
    


    How to use it:
    • To convert WITHOUT deleting .flac files:
      flac2mp3 yourMusicFolder/
      

    • To convert C by deleting .flac files:
      flac2mp3 -r yourMusicFolder/
      



    Checked at home - everything works. At least there are mp3 files and nothing superfluous has been deleted, which is already not bad, I think. I hope someone comes in handy.

    PS I will be glad to comments on the code, since I write to the shell very rarely.

    UPD: The script does not save tags! I apologize to those who have already been blown up on this mine, now I will correct it.
    UPD2: Since my solution does not work as I would like, user mgyk kindly suggested his option in the comments:
    #!/bin/bash
    if [ "${1}" == '' ]; then
    shellDir="$PWD"
    else
    shellDir="${1}"
    fi
    prefix=/home/share/music/_mp3
    find "${shellDir}" -name '*.flac' -print | while read fn;
    do
      ARTIST=`metaflac "$fn" --show-tag=ARTIST | sed s/.*=//g`
      TITLE=`metaflac "$fn" --show-tag=TITLE | sed s/.*=//g`
      ALBUM=`metaflac "$fn" --show-tag=ALBUM | sed s/.*=//g`
      GENRE=`metaflac "$fn" --show-tag=GENRE | sed s/.*=//g`
      TRACKNUMBER=`metaflac "$fn" --show-tag=TRACKNUMBER | sed s/.*=//g`
      DATE=`metaflac "$fn" --show-tag=DATE | sed s/.*=//g`
    newpath="${prefix}/$ARTIST/$ALBUM"
    newfile=${TITLE}.mp3
    echo $newfile
    mkdir -p "${newpath}"
    flac -c -d "${fn}" | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "${newpath}/${newfile}"
    id3 -t "$TITLE" -T "${TRACKNUMBER:-0}" -a "$ARTIST" -A "$ALBUM" -y "$DATE" -g "${GENRE:-12}" "${newpath}/${newfile}"
    done
    


    Also, in the comments, several programs were suggested that I did not find, and which fulfill the task:
    Max
    X Lossless decoder

    Also popular now: