Linux Script to convert flac to mp3
#1
I wrote my first bash script and thought I would share it with you guys. I know it's been done a hundred times already, but mine does the following;

1) Converts flac to mp3 from samba shares
2) Can run script from any location
3) Converts your entire library with one click
4) Copies folder art work
5) Incrementally converts files as you add more music with one click
6) Ability to easily schedule

Code:
#!/bin/bash
###############################################################################################################
# Author :    RhysGM
# Title  :     flac2mp3
# Date   :    25-Mar-2013
# Version:    1.02
# Description;
# Incrementally convert all flac files in a directory including all subdirectories to mp3 and copy to a
#  mirrored directory. Also copies folder.jpg
# Prerequisites;
# sudo apt-get install lame flac id3
###############################################################################################################

# input and output locations for flac and mp3 files
username=rhys
flac_loc="/home/$username/flac_music"
mp3_loc="/home/$username/mp3_music"

###############################################################################################################
#
#  Samba only //server/share
#
###############################################################################################################
flac_smb="//berry/flac_music"
mp3_smb="//berry/mp3_music"

if [ ! -d "$flac_loc" ]
then
    mkdir "$flac_loc"
fi

flac_smb_count=$(ls "$flac_loc" | wc -l)
if [ $flac_smb_count = 0 ]
then
    mount -t smbfs "$flac_smb" "$flac_loc"
fi

if [ ! -d "$mp3_loc" ]
then
    mkdir "$mp3_loc"
fi

mp3_smb_count=$(ls "$mp3_loc" | wc -l)
if [ $mp3_smb_count = 0 ]
then
    mount -t smbfs "$mp3_smb" "$mp3_loc" -o uid=$username
fi

chmod -R 777 "$mp3_loc"
###############################################################################################################

# Creates counters to display progress
flac_count=$(find "$flac_loc" -path "/trashbox" -prune -o -iname "*.flac" | wc -l)
mp3_count=$(find "$mp3_loc" -path "/trashbox" -prune -o -iname "*.mp3" | wc -l)
process=$((flac_count - mp3_count))
count=0    
echo $flac_count "flac files found," $mp3_count "mp3 files found, estimated" $process "files to be processed"

# mp3 loop, check if mp3 exists, convert flac to mp3 in new location, copy id3 tags
while read -r -d $'\0' flac_file; do

    temp="${flac_file%.flac}.mp3"
    mp3_file="$mp3_loc${temp#$flac_loc}"

    if [ ! -f "$mp3_file" ]
    then
        count=$((count + 1))
        artist=$(metaflac "$flac_file" --show-tag=artist | sed s/ARTIST=//g)
        title=$(metaflac "$flac_file" --show-tag=title | sed s/TITLE=//g)
        album=$(metaflac "$flac_file" --show-tag=album | sed s/ALBUM=//g)
        genre=$(metaflac "$flac_file" --show-tag=genre | sed s/GENRE=//g)
        track=$(metaflac "$flac_file" --show-tag=tracknumber | sed s/TRACKNUMBER=//g)
        moddate=$(metaflac "$flac_file" --show-tag=date | sed s/DATE=//g)
        comment=$(metaflac "$flac_file" --show-tag=comment | sed s/COMMENT=//g)

        mp3_album="${mp3_file%/*}"
        mp3_artist="${mp3_album%/*}"
        if [ ! -d "$mp3_artist" ]
        then
            mkdir "$mp3_artist"
        fi

        if [ ! -d "$mp3_album" ]
        then
            mkdir "$mp3_album"
        fi

        flac -c -d "$flac_file" | lame -m j -q 0 --vbr-new -V 0 -s 44.1 - "$mp3_file"
        id3 -t "$title" -T "${track:-0}" -a "$artist" -A "$album" -y "$moddate" -g "$genre" -c "$comment" "$mp3_file"

        echo $count "file(s) of" $process "completed"
    fi
done < <(find "$flac_loc" -path "/trashbox" -prune -o -iname "*.flac" -print0)
echo "Finished processing flac 2 mp3"

flac_count=$(find "$flac_loc" -name 'folder.jpg' | wc -l)
mp3_count=$(find "$mp3_loc" -name 'folder.jpg' | wc -l)
process=$((flac_count - mp3_count))
count=0    
echo $flac_count "original jpg's found," $mp3_count "sync'd jpg's found, estimated" $process "files to be processed"

# jpg loop, check if jpg exists, copy jpg
while read -r -d $'\0' jpg_flac; do
    jpg_mp3="$mp3_loc${jpg_flac#$flac_loc}"

    if [ ! -f "$jpg_mp3" ]
    then
        cp "$jpg_flac" "$jpg_mp3"
    fi
done < <(find "$flac_loc" -path "/trashbox" -prune -o -iname "folder.jpg" -print0)

###############################################################################################################
#
#  SAMBA ONLY MUST REMOVE IF MUSIC LIBRARY IS LOCAL IF UNSURE DELETE THE NEXT FOUR LINES OF CODE
#
###############################################################################################################

umount $flac_smb
rmdir $flac_loc
umount $mp3_smb
rmdir $mp3_loc

###############################################################################################################

echo "flac2mp3 completed"

If you want to use the script;

Copy and paste the code into your favourite editor
Change username (assumes samba and home user names are the same)
Change locations (delete both parts of code between samba comments if flac and mp3 is local)
Save in your home directory or anyway else
In terminal cd to script location
chmod 755 ./name_of_script
sudo apt-get install lame flac id3

Once this is done you can run the script whenever you want using the terminal or opening the file by clicking on it.

Hope someone finds it useful. Smile
Linux 3.5.0-22-generic
Ubuntu 12.10 - XBMCbuntu quantal
Skin: Aeon Nox
CPU: Intel Celeron G1610 2.60GHz
GPU: Enhanced Intel HD Graphics 2000/3000
RAM: 2GB
SSD: 32GB
Reply
#2
Be careful with the -s flag and the forced sample rate - better keep what you had for maximum quality.

Are you really sure you want joint stereo?
First decide what functions / features you expect from a system. Then decide for the hardware. Don't waste your money on crap.
Reply
#3
(2013-03-30, 00:17)fritsch Wrote: Be careful with the -s flag and the forced sample rate - better keep what you had for maximum quality.

Are you really sure you want joint stereo?
Do you think flac -c -d "$flac_file" | lame -m j -q 0 --vbr-new -V 0 -s - "$mp3_file" will be better quality?
Linux 3.5.0-22-generic
Ubuntu 12.10 - XBMCbuntu quantal
Skin: Aeon Nox
CPU: Intel Celeron G1610 2.60GHz
GPU: Enhanced Intel HD Graphics 2000/3000
RAM: 2GB
SSD: 32GB
Reply
#4
You resample and you set joint stereo. That is great for some headphones, when you want to share with somebody and listening together and everybody has one of the two plugs in one ear - but for stereo systems, it should stay real stereo. Concerning the resampling, that will make quality worse of course, cause you loose samples.

I would keep the current sample rate (if possible) and if it is not possible, i would downsample to 48.0 as this is the max lame supports.

That means for real life: Keep 44.1 samples at 44.1 and 48.0 at 48.0 khz.

From the man file:
Quote:LAME will automatically resample the input file to one of the supported MP3 samplerates if necessary.

Try some files, e.g. 96khz and 192khz flacs and see how it sounds - perhaps you can ommit it completely.
First decide what functions / features you expect from a system. Then decide for the hardware. Don't waste your money on crap.
Reply

Logout Mark Read Team Forum Stats Members Help
Script to convert flac to mp30