DistrOggize

A small shell script which allows recursive parallel distributed recoding of directories containing mp3 files. It uses ssh, oggenc and mp3info to do the work.

#!/bin/bash
 
set -e
 
echo "distributed oggifying directory $1 ... please be patient"
 
oggdir="$1.oggized"
 
mkdir -p "$oggdir"
 
tmpdir="/tmp/distoggize_"`date +%s%N`$RANDOM
echo $tmpdir
 
trap cleanup 0
 
cleanup() {
    echo "cleaning up..."
    for host in $peers; do 
        ssh $host "rm -rf $tmpdir"
    done
}
 
makeogg() {
    peerhost="$1"
    echo "** $peerhost"
 
    dir=`dirname "$mp3"`
    file=`basename "$mp3"`
    ogg=`basename "$mp3" .mp3`.ogg
 
    tracknum=`mp3info -p "%n" "$mp3"`
    artist=`mp3info -p "%a" "$mp3"`
    album=`mp3info -p "%l" "$mp3"`
    title=`mp3info -p "%t" "$mp3"`
 
    pushd "$oggdir" > /dev/null
    mkdir -p "$dir"
 
    tmpoggfile="$tmpdir/$dir/$ogg"
    tmpoggdir=`dirname "$oggfile"`
 
    mpg123 -q -w- ../"$mp3" | ssh "$peerhost" "mkdir -p \"$tmpoggdir\" ; oggenc --tracknum \"$tracknum\" --artist \"$artist\" --album \"$album\" --title \"$title\" -b 64 -o \"$tmpoggfile\" - ; cat \"$tmpoggfile\" ; rm -f \"$tmpoggfile\"" > "$dir/$ogg" ; 
 
    popd > /dev/null
 
}
 
DIST_INIT() {
    export C_RESET="\033[0m"
 
    export C_GRAY="\033[1;30m"
    export C_GREY="\033[1;30m"
    export C_RED="\033[1;31m"
    export C_GREEN="\033[1;32m"
    export C_YELLOW="\033[1;33m"
    export C_BLUE="\033[1;34m"
    export C_PURPLE="\033[1;35m"
    export C_MAGENTA="\033[1;35m"
    export C_CYAN="\033[1;36m"
    export C_WHITE="\033[1;37m"
 
 
    DIST_flagdir=`mktemp -d`
    #mkdir -p $DIST_flagdir
 
    DIST_peers="$*"
 
    for DIST_peer in $DIST_peers; do
	touch "$DIST_flagdir/$DIST_peer"
    done
 
    echo -e "\n${C_RESET}${C_GREEN}Initializing distributed running environment...${C_RESET}"
 
}
 
DIST_RUN() {
    # run task in background when a worker host is available
    while true; do
	DIST_peerhost=`ls -1 "$DIST_flagdir" | head -n1`
        if [ "$DIST_peerhost" ]; then
	    echo -e "\n${C_RESET}${C_GREEN}Starting task ${C_MAGENTA}$1${C_GREEN} on ${C_YELLOW}$DIST_peerhost${C_GREEN}...${C_RESET}"
	    (
		rm -fv "$DIST_flagdir/$DIST_peerhost" ; 
		"$1" "$DIST_peerhost"
		touch "$DIST_flagdir/$DIST_peerhost" ;
	    )&
	    break;
	fi
        sleep 1
    done
}
 
DIST_WAIT_FINISH() {
    # wait for the tasks to finish
    for DIST_peerhost in $DIST_peers; do
	echo -e "\n${C_RESET}${C_GREEN}Waiting for task on ${C_YELLOW}$DIST_peerhost${C_GREEN} to finish...${C_RESET}"
	while true; do
    	    if [ -f "$DIST_flagdir/$DIST_peerhost" ]; then
		break
	    fi
    	    sleep 1
	done    
	echo -e "\n${C_RESET}${C_GREEN}Task on ${C_YELLOW}$DIST_peerhost${C_GREEN} finished.${C_RESET}"
    done
    rm -rf "$DIST_flagdir"
    rm -rf "$DIST_logdir"
}
 
DIST_INIT "localhost mcree.myftp.org tricon.hu bofors.ikk.sztaki.hu dobrogi.ikk.sztaki.hu nat.sztaki.hu leeloo.kiskapu.hu leona.kiskapu.hu"
 
find "$1" -type f -iregex ".*.mp3$" | while read mp3; do 
 
    echo "... processing $mp3"
 
    mp3gain -r -k ../"$mp3"
 
    DIST_RUN makeogg
 
done
 
DIST_WAIT_FINISH
 
cleanup