Created
February 18, 2022 01:43
-
-
Save C4K3/07852a88acb2d2933a813ac9b9aa0fa8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
## This is a script made for the purpose of embedding metadata into media files | |
## from an iTunes library, allowing the media files to be used in any player. | |
## | |
## Given a media file extracted from iTunes, search the current directory for | |
## iTunes plist files, and if a matching file is found metadata is extracted | |
## from the plist file and embedded into the media file. The media file is then | |
## written into the directory 'out'. | |
## | |
## Can also set the environment variable COVERART to point to a file containing | |
## cover art to embed. | |
if [[ "$#" -gt 1 ]]; then | |
for ARG in "$@"; do | |
"$0" "$ARG" | |
echo | |
done | |
exit | |
fi | |
rdom () { local IFS=\> ; read -d \< E C ;} | |
MEDIA="$1" | |
EXTENSION="${MEDIA##*.}" | |
echo "Processing file $MEDIA" | |
ID=$(exiftool -S -'ItemID' -- "$MEDIA" | awk '{print $2}') | |
ID=$(printf '%d' "$ID") | |
echo "ID is $ID" | |
PLIST="" | |
for FILE in *.plist; do | |
PLIST=$(plistutil -i "$FILE") | |
# xq doesn't work for parsing this | |
if [[ "$PLIST" == *"$ID"* ]]; then | |
echo "Found plist file $FILE" | |
break | |
fi | |
done | |
if [ -z "${PLIST:+x}" ]; then | |
echo "Did not find plist file" | |
exit 1 | |
fi | |
# Key is key in plist, value is mp4 metadata tag | |
declare -A SEARCH_VARS | |
SEARCH_VARS[artistName]=Artist | |
SEARCH_VARS[itemName]=Title | |
SEARCH_VARS[trackNumber]=Tracknumber | |
SEARCH_VARS[playlistName]=Album | |
SEARCH_VARS[year]=Year | |
NEXT="" | |
declare -A VARS | |
while rdom; do | |
# Ignore terminators | |
if [[ "${E:0:1}" == "/" ]]; then | |
continue | |
fi | |
# Ignore empty values | |
if [ -z "${C:+x}" ]; then | |
continue | |
fi | |
if [[ "$E" == "key" ]]; then | |
NEXT="$C" | |
else | |
KEY="$NEXT" | |
NEXT="" | |
FOUND=0 | |
for S in "${!SEARCH_VARS[@]}"; do | |
if [[ "$S" == "$KEY" ]]; then | |
FOUND=1 | |
break | |
fi | |
done | |
if [ "$FOUND" -eq 0 ]; then | |
continue | |
fi | |
VARS["$KEY"]="$C" | |
fi | |
done <<< "$PLIST" | |
cp -- "$MEDIA" tmp | |
EXIF_ARGS=(-overwrite_original_in_place) | |
for KEY in "${!VARS[@]}"; do | |
echo "$KEY = ${VARS[$KEY]}" | |
VALUE="${VARS[$KEY]}" | |
TAG="${SEARCH_VARS["$KEY"]}" | |
EXIF_ARGS+=(-"$TAG"="$VALUE") | |
done | |
exiftool "${EXIF_ARGS[@]}" ./tmp | |
if [ -n "${COVERART+x}" ]; then | |
AtomicParsley ./tmp --artwork "$COVERART" --output atomictmp | |
mv atomictmp tmp | |
fi | |
OUTDIR="${VARS[playlistName]}" | |
mkdir -p "$OUTDIR" | |
OUT="$OUTDIR/${VARS[trackNumber]} - ${VARS[itemName]}.$EXTENSION" | |
mv tmp "$OUT" | |
echo "Wrote $OUT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment