?
2011-03-20 21:56:22 UTC
#!/bin/bash
ncpu=`cat /proc/cpuinfo | grep "cpu MHz" | wc -l`
if [ "${2:0:2}" = "-n" ]; then
ncpu_=${2:2}
ncpu_=${ncpu_//[^0-9]/}
if [ -n "$ncpu_" ]; then
ncpu=$ncpu_
fi
fi
echo "Parallel encoding: using $ncpu CPUs"
parms=${1:--v0}
proclist=()
procin()
{
proclist=( ${proclist[@]} $1)
}
procout()
{
proclist=(${proclist[@]/$1/})
}
procref()
{
for pid in ${proclist[@]}; do
if ! kill -0 $pid 2>/dev/null; then
procout $pid
fi
done
}
nproc()
{
echo ${#proclist[@]}
}
queue()
{
local f="$1"
# wait until a cpu is available
while [ "`nproc`" -ge $ncpu ]; do
procref
sleep 0.5
done
echo "$f"
flac2mp3 "$f" "mp3$parms/${f%.flac}.mp3" $parms 2>/dev/null &
procin $!
}
while read f; do
mkdir -p "mp3$parms/`dirname "$f"`"
queue "$f"
done
# wait for remaining processes to exit
while [ "`nproc`" -gt 0 ]; do
procref
sleep 0.5
done