#!/bin/bash # flac2mp3 script # Copyright (C) 2006-2007 Greg Methvin (greg@methvin.net) # This program is free software. You may distribute it under the terms of # the GNU General Public License as published by the Free Software # Foundation, version 2. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. # # This script transcodes FLAC files to MP3. function usage { echo "Usage: $(basename $0) [[-a|-c] -b | -p ] [-i ] [...]" echo "options:" echo " -b Set average or constant bitrate for mp3 (constant is default)" echo " : 32 40 48 56 64 80 96 112 128 160 192 224 256 320" echo " -a Encode mp3 at a variable bitrate averaging kbps" echo " -c Encode mp3 at a constant bitrate of kbps" echo " -p Use a preset quality value:" echo " \"-p medium\": ~150-180kbps" echo " \"-p standard\": ~170-210kbps" echo " \"-p extreme\": ~200-240kbps (default)" echo " \"-p insane\": 320kbps CBR" echo " -s Apply sed expression to output filename:" echo " \"-s 's/rip/\/media\/mp3player/'\":" echo " Replaces 'rip' with '/media/mp3player'" exit 1; } # Turn off input field separation to allow filenames with spaces IFS= bitrate= CBR_OPTS="--cbr -b" ABR_OPTS="--abr" br_opts= preset=extreme sedarg= while getopts ":p:acb:s:" options; do case $options in a) br_opts=$ABR_OPTS;; c) br_opts=$CBR_OPTS;; b) bitrate=$(echo "$OPTARG" | grep '^0*\(32\|40\|48\|56\|64\|80\|96\|112\|128\|160\|192\|224\|256\|320\)$') if [ -z "$bitrate" ]; then usage; fi;; p) preset=$(echo "$OPTARG" | grep '^\(medium\|standard\|extreme\|insane\)$') if [ -z "$preset" ]; then usage; fi;; s) sedarg=$OPTARG;; *) usage;; esac done shift $(($OPTIND - 1)) if [ -z "$*" ] || [ -n "$cbr" ] && [ -z "$bitrate" ]; then usage elif [ -z "$br_opts" ] && [ -n "$bitrate" ]; then br_opts=$CBR_OPTS fi if [ -n "$br_opts" ]; then lame_opts="$br_opts $bitrate" else lame_opts="--preset $preset" fi function flac2mp3_convert { inf="$1" outf="$2" # Encode to MP3 echo echo "input FLAC file: $inf" echo "output MP3 file: $outf" bash -c "flac -c -d \"$inf\" | lame $lame_opts - \"$outf\"" # check if we have an output file [ -f "$outf" ] || return 1 # Get ID3 info ARTIST=$(metaflac "$inf" --show-tag=ARTIST | sed s/ARTIST=//g) TITLE=$(metaflac "$inf" --show-tag=TITLE | sed s/TITLE=//g) ALBUM=$(metaflac "$inf" --show-tag=ALBUM | sed s/ALBUM=//g) GENRE=$(metaflac "$inf" --show-tag=GENRE | sed s/GENRE=//g) TRACKNUMBER=$(metaflac "$inf" --show-tag=TRACKNUMBER \ | sed s/TRACKNUMBER=//g) YEAR=$(metaflac "$inf" --show-tag=DATE \ | sed s/DATE=//g | cut -b -4) # Set ID3 tags (assume id3v2) echo echo "Setting ID3 tags" echo id3v2 -t "$TITLE" -T "$TRACKNUMBER" \ -a "$ARTIST" -A "$ALBUM" -y "$YEAR" -g "$GENRE" \ "$outf" > /dev/null id3v2 -l "$outf" return 0 } count=0 for inf in $*; do # Check if the file exists if [ ! -f "$inf" ]; then echo "$inf: No such file.">&2 continue fi # Set output filename outf=$(echo "$inf" | sed 's/\.flac$/.mp3/gi') if [ "$sedarg" != "" ]; then outf=$(echo "$outf" | sed "$sedarg") mkdir -p "$(dirname "$outf")" fi # Do the conversion on this file and increment if we created the file if [ "$outf" != "$inf" ]; then flac2mp3_convert "$inf" "$outf" && let count++ fi done echo echo "$count MP3 files created." echo