Skip to content

Instantly share code, notes, and snippets.

@MicahElliott
Created November 29, 2010 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MicahElliott/719597 to your computer and use it in GitHub Desktop.
Save MicahElliott/719597 to your computer and use it in GitHub Desktop.
FLAC-to-MP3 Mass Converter
#! /bin/bash
#
# flac2mp3 — Convert FLAC files to MP3.
#
# Author: Micah Elliott <mde MicahElliott com>
# Date: Feb 2008 (not sure if any util has obviated this)
#
# Many people store their music collections in FLAC format these days
# since it’s nice to have lossless compressed copies, and disks are
# cheap. But people also need to convert their FLAC collections into
# MP3 whenever they load music onto their MP3 players (for
# FLAC-unequipped players or any time you want to maximize your
# space). So this tool is needed for generation of (typically)
# temporary MP3 files.
#
# I wanted to use soundconverter to perform conversion operations, but
# it is lacking the abililty to retain the tag information.
#
# Ideas from thread
# (http://www.linuxtutorialblog.com/post/solution-converting-flac-to-mp3)
# and soundconverter (http://soundconverter.berlios.de/)
#
# Features to consider:
# * Detection of already-existing output files
# * Resume from interrupted conversion
# * Envar to locate the mp3 storage area
# * Maintain dir structure of source
# * Creation of a link-farm
# * Timing of each file conversion
# * Spinner/percentage indicator
# * Speed up the conversion process
# * Multiple output formats (mp3 or ogg)
# * Some sort of interactive selection facility
#
# Options:
# -w overWrite (default to skip existing .mp3)
# -o Outdir
# -b Base-depth[=0]
usage() {
echo "ERROR: $1" 1>&2
echo
exit 1
}
msg() {
test -n "$quiet" ||
echo "$1"
}
prog=$(basename $0)
version="0.1"
usagetxt="\
usage: $prog [OPTS] DIR …
or: $prog [OPTS] FILE …
Convert FLAC files to MP3.
-o DIR Output DIR to hold MP3s
-w overWrite any pre-existing files
-b Base depth
-q try to be Quiet
-h print this Help message
-v print Version
Report bugs to <mde@micahelliott.com>"
# Parse optional behavior
outdir="./mp3"
overwrite=
while getopts ":hwo:b:qv" opt; do
case $opt in
w ) overwrite=1;;
o ) outdir="$OPTARG" ;;
b ) basedepth=1 ;;
q ) quiet=1 ;;
h ) echo "$usagetxt"; exit 0; ;;
v ) echo "$version"; exit 0; ;;
\? ) usage "Illegal option — $OPTARG";;
esac
done
shift $(($OPTIND - 1))
test ! -d $outdir && {
mkdir -p $outdir ||
usage "Cannot create output dir $output."
}
requirements="flac metaflac id3v2 lame"
for r in $requirements ; do
which $r &>/dev/null ||
usage "This script requires: $requirements"
done
# Modify the lame options to your preference
lameopts=" --vbr-new -B 256 "
infiles="" indirs=""
for a in $@ ; do
test -f $a && infiles="$infiles $a"
test -d $a && indirs="$indirs $a"
done
test -n "$infiles" -a -n "$indirs" &&
usage "Can’t specify dirs AND files."
test -n "$indirs" &&
flacfiles="$(find -L $indirs -type f -name '*.flac')" ||
flacfiles="$infiles"
test -z "$flacfiles" &&
usage "Did not find any .flac files."
tomp3() {
unset ARTIST ALBUM TITLE COMMENT GENRE DATE TRACKNUMBER
# Use metaflac to extract tags, then use sed to add double-quoting
# to field values to make them usable as bash vars.
eval $(metaflac --export-tags-to=- "$flacfile" | sed 's/=\(.*\)/="\1"/')
msg "Converting to MP3 format: $flacfile"
flac --silent -c -d "$flacfile" | lame --quiet $lameopts - $outdir/"$mp3file"
id3v2 \
-a "$ARTIST" \
-A "$ALBUM" \
-t "$TITLE" \
-c "$COMMENT" \
-g "$GENRE" \
-y "$DATE" \
-T "$TRACKNUMBER" \
"$outdir/$mp3file"
}
for flacfile in $flacfiles ; do
# Convert the filename suffix.
mp3file=$(basename "${flacfile%.flac}.mp3")
if [ ! -f "$outdir/$mp3file" ] ; then
tomp3 "$flacfile"
elif [ -z "$overwrite" ]; then
tomp3 "$flacfile"
else
echo "Skipping already existing $mp3file" ||
tomp3 "$flacfile"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment