Skip to content

Instantly share code, notes, and snippets.

@sfan5
Last active March 5, 2018 19:50
Show Gist options
  • Save sfan5/563a3172c3602f7a9aa2 to your computer and use it in GitHub Desktop.
Save sfan5/563a3172c3602f7a9aa2 to your computer and use it in GitHub Desktop.
Converts a /var/mobile/Media/Purchases (from iPhone, iPad or iPod touch) directory to media files with artwork & tags
#!/bin/bash -e
### config ###
numbered=1
force_artwork="./-52743546503191174.jpeg"
##############
getxml () {
grep -F -A1 "<key>$1<" -- "$file" | sed -rn 's/.*<(string|integer)>(.+)<.*/\2/p'
}
unescape () {
sed -e 's|&amp;|\&|g' -e 's|&lt;|<|g' -e 's|&gt;|>|g'
}
die () {
printf '%s\n' "$1" >&2
exit 1
}
plistutil &>/dev/null || die "Install libplist-utils"
AtomicParsley &>/dev/null || die "Install atomicparsley"
[ -f StorePurchasesInfo.plist ] || die "Doesn't look like a 'Purchases' directory here..."
for file in *.plist; do
plistutil -i "$file" -o "$file.xml"
rm -- "$file"
done
for file in *.xml; do
[ "`getxml kind`" == "song" ] || continue
artist=`getxml artistName | unescape`
title=`getxml itemName | unescape`
albumArtist=`getxml playlistArtistName | unescape`
albumName=`getxml playlistName | unescape`
copyright=`getxml copyright`
genre=`getxml genre`
year=`getxml year`
trackNum=`getxml trackNumber`
trackTotal=`getxml trackCount`
# goddamnit apple
fs=`getxml "file-size"`
mediaFile=`ls -l | sed -rn 's|^([^ ]+ ){4} *'$fs' +([^ ]+ +){3}([-0-9]+\.m4a)$|\3|p'`
if [[ -z "$mediaFile" || `wc -l <<<"$mediaFile"` -ne 1 ]]; then
printf 'error: ??? -> %s - %s\n' "$artist" "$title"
continue
fi
artworkFile=$force_artwork
out="$artist - $title.m4a"
[ $numbered -eq 1 ] && out="$(printf '%02d' $trackNum). $out"
tn=$trackNum
[ -n "$trackTotal" ] && tn="$tn/$trackTotal"
mv -- "$mediaFile" "$out"
AtomicParsley "$out" -W \
--artwork "$artworkFile" \
--copyright "$copyright" \
--artist "$artist" \
--title "$title" \
--albumArtist "$albumArtist" \
--album "$albumName" \
--genre "$genre" \
--year "$year" \
--tracknum "$tn" \
&>/dev/null
printf 'OK: "%s" -> %s - %s\n' "$mediaFile" "$artist" "$title"
rm -- "$file" #"$artworkFile"
done
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment