itunes-rename-by-metadata/itunes-rename-by-metadata.sh
Git Commiter for 96-fromsofia.net 4512ec62d1 First commit
2023-02-27 01:16:56 +00:00

65 lines
2.6 KiB
Bash

#!/bin/bash
# NOTE:
# 1) This script works around the mediainfo package. It is a simple utility to get metadata info from various media types
# 2) This script relies on metadata in your media in order to rename your files. Depending on how you acquired your media in the first place, some metadata may be missing hence resulting in incomplete output!!
# 3) To mount your iphone on a Linux device, you will need libimodiledevice libraries + the ifuse package --> Ref: https://wiki.archlinux.org/title/IOS
# Set a random session number
session=$RANDOM
# Specify the particular delimiter for string devision by IFS (Internal Field Separator)
IFS='
'
# Specify the path where your iphone is mounted
mountpoint="/mnt/iphone"
iphone_music="$mountpoint/Music/F*/*"
# Specify the destination path where you want the new file names copied
destination="/home/user/Music"
# Create a temp file to store "failed items"
touch $PWD/failed_list$session.txt
fail_list=$PWD/failed_list$session.txt
# Loop through all the songs in $iphone_music
for song in $iphone_music
do
# Use the mediainfo utility to get the song name, it's performer and album
song_artist=$(mediainfo $song | grep "Album/Performer" | cut -c 44-)
song_name=$(mediainfo $song | grep -m 1 "Track name" | cut -c 44-)
song_album=$(mediainfo $song | grep -m 1 "Album" | cut -c 44-)
# If it doesn't have a song name add to failed list and continue with next song
if [ -z "$song_name" ]
then
echo $song >> $fail_list
continue
# Alternatively use next elif and else to create the $dest_name based on the metadata that has been found
elif [ -n "$song_artist" && -n "$song_album" ]
then
dest_name=$(printf "$song_name by $song_artist from $song_album")
elif [ -n "$song_artist" && -z "$song_album" ]
then
dest_name=$(printf "$song_name by $song_artist")
elif [ -z "$song_artist" && -n "$song_album" ]
dest_name=$(printf "$song_name from the album: $song_album")
else
dest_name=$(printf "$song_name")
fi
# Copy the original file to a new one in the specified $destination with it's new metadata generated name
cp $song "$destination/$dest_name"
done
# Update the user with the failed files
echo -e "WARNING \!\!\!\nThe following items have no song name in their metadata hence were not renamed and copied over:"
cat $fail_list
yes | rm $fail_list
# Confirm the successfully renamed and copied media
echo "The rest of your files have been saved into $destination"
ls -l $destination