Initial commit

This commit is contained in:
Markus Koch 2015-12-21 15:43:57 +01:00
commit cefaa6525c
1 changed files with 115 additions and 0 deletions

115
LossifyMusicArchive.sh Executable file
View File

@ -0,0 +1,115 @@
#!/bin/bash
# --- Configuration --- #
MAXTHREADS=8
# --- Functions --- #
function isMusic() {
if [ "` file \"$1\"| sed \"s/.*: //g\" | tr '[:upper:]' '[:lower:]' | grep \"audio\"`" != "" ]; then
return 0
else
return 1
fi
}
function isFlac() {
if [ "`echo \"$1\" | sed \"s/.*\.//g\" | tr '[:upper:]' '[:lower:]'`" == "flac" ]; then
return 0
else
return 1
fi
}
function waitForFreeCore() {
sleep 0.1
cur=`pidof ffmpeg`
IFS=$(echo -en " ")
cnt=0
for proc in $cur; do
cnt=$(( $cnt + 1 ))
done;
if [ "$cnt" -lt "$1" ]; then
return
else
sleep 1
waitForFreeCore "$1";
fi
}
# --- APPLICATION ENTRY POINT --- #
# Init working environment
indir="/media/media/Music"
outdir="/home/markus/Music/lossy"
if [ "$3" != "" ]; then
cdir="$3"
else
cdir="."
fi
me=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)/`basename "${BASH_SOURCE[0]}"`
echo ""
cd "$indir"
mkdir -p "$outdir/$cdir" # ACTION: MKDIR
IFS=$(echo -en "\n\b")
firstFile=1
list=`ls "$cdir"`;
for fileX in $list; do
file="$cdir/$fileX"
#echo "TEST====$file"
if [ "$file" == "" ]; then
continue
fi
if [ -f "$file" ]; then
rawCopy=1 # Let's assume it's anything but flac
if isMusic "$file"; then
if [ $firstFile -eq 1 ]; then # Extract album art
firstFile=0
if [ ! -f "$outdir/cover.jpg" ]; then
ffmpeg -n -i "$indir/$file" "$outdir/$cdir/cover.jpg" # ACTION: EXTRACT COVER
fi
fi
if isFlac "$file"; then
echo "FLAC: $file"
rawCopy=0 # No copy!
newName="${file%.*}.ogg"
if [ -f "$outdir/$newName" ]; then
echo "SKIP: $file"
else
ffmpeg -n -threads auto -i "$indir/$file" -codec:a libvorbis -q:a 8 "$outdir/$newName" & # ACTION: CONVERT
waitForFreeCore $MAXTHREADS
fi
else
echo "COPY: $file"
fi
else
echo "OTHR: $file"
fi
if [ $rawCopy -eq 1 ] ; then
if [ -f "$outdir/$file" ]; then
echo "SKIP: $file"
else
cp $indir/$file $outdir/$file # ACTION: COPY
fi
fi
elif [ -d "$file" ]; then
echo -e "\e[1m---> CALL:" $me "$indir" "$outdir" "$file" "\e[21m"
# read foooooo # for debugging
$me "$indir" "$outdir" "$file"
echo -e "\e[1m<--- RETURN: $file" "\e[21m"
else
echo -e "\e[1m!!!! IGNORE: $file" "\e[21m"
fi
done;