Skip to content

Instantly share code, notes, and snippets.

@vladkorotnev
Created October 2, 2018 12:18
Show Gist options
  • Save vladkorotnev/eae00eb89d3c7bdbed1d5727fec2c9bb to your computer and use it in GitHub Desktop.
Save vladkorotnev/eae00eb89d3c7bdbed1d5727fec2c9bb to your computer and use it in GitHub Desktop.
Convert iTunes Music folder into a simple directory, preserving structure, and converting the ALAC files into MP3 (leaving the rest untouched). Requires: ffmpeg with LAME, mediainfo
#!/usr/bin/env bash
IN="/media/Music/iTunes Media/Music/"
OUT="/home/akasaka/Music/From_iTunes"
FILES=$(find "$IN/" -type f ! -name '.DS_Store')
IFS=$'\n'
mkdir -p "$OUT"
cd "$OUT"
for file in $FILES
do
localname=$(dirname "$file" | sed -e "s?$IN/??")
if [ ! -d "./$localname" ]
then
echo "NEW DIR: $localname"
mkdir -p "$localname"
fi
fname=$(basename "$file")
# Ignore zip files!
if echo "$fname" | grep "zip$\|m4v$">/dev/null
then
echo "SKIP: $fname"
else
no_need_convert=1
if echo "$fname" | grep "wav$">/dev/null
then
# Always convert WAV
no_need_convert=0
fi
if echo "$fname" | grep "m4a$">/dev/null
then
# M4A can be lossless or lossy, determine which one it is
mediainfo "$file" | grep ALAC>/dev/null
no_need_convert=$?
fi
if ((no_need_convert))
then
# File is not lossless, copy direct
echo "COPY: $fname"
cp "$file" "$OUT/$localname/$fname"
else
echo -n "CONVERT: $fname ... "
outname="$OUT/$localname/${fname%.*}.mp3"
srcsz=$(stat --printf="%s" "$file")
echo -n "$srcsz"
ffmpeg -loglevel quiet -hide_banner -i "$file" -codec:a libmp3lame -qscale:a 0 "$outname"
dstsz=$(stat --printf="%s" "$outname")
echo " -> $dstsz"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment