#!/bin/bash -u # copy_playlist.bsh # This script reads the contents of a m3u playlist, copies each file to specified folder # and optionally changes the album tag and track no tag of the new files # # Call without parameters to see usage. # # FUNCTION DEFINITIONS # function pause(){ read -p "$*" } # # ROUTINE STARTS HERE # # check and validate inputs # if [ $# -lt 2 ] then echo -e "\nThis script reads the contents of a m3u playlist, copies each file to specified folder and optionally changes the album tag and the track no tag of the new files. Usage: $0 sourcelist.m3u /homes/user/destinationpath [\"New Album Name\"] " exit; fi # check that sourcelist exists & is > 0 bytes # SOURCELIST=$1 # # does sourcelist exist and is it a regular file? if [ ! -f "$SOURCELIST" ] then echo -e "\n$SOURCELIST does not exist or is not a regular file\n" exit 1; fi # # is sourcelist > 0 bytes? if [ ! -s "$SOURCELIST" ] then echo -e "\n$SOURCELIST is zero bytes! Please check the file. \nTry cat $SOURCELIST.\n" exit 2; fi # set DESTPATH DESTPATH=$2 # check that destinationpath exists if [ ! -e "$DESTPATH" ] then echo -e "\n$DESTPATH does not exist!\n" exit 3; fi # check that destinationpath is a directory if [ ! -d "$DESTPATH" ] then echo -e "\n$DESTPATH is not a directory!\n" exit 4; fi # check that destinationpath is writable if [ ! -w "$DESTPATH" ] then echo -e "\n$DESTPATH is not writable!\n" exit 5; fi # reset error variable strErrorDesc="" # initilise/reset counter TrackNo=0 # open sourcelist # read a line of text # loop until EOF:- while read line do #echo $line # if it starts with "file:///" if [[ $line =~ ^file:// ]] then (( TrackNo += 1)) # increment the counter # extract string starting after "file:///" and ending at the end of the line strLen=`expr length $line` filenameLen=$(( strLen - 7 )) SourceFile=`expr substr $line 8 $filenameLen` # replace all %20s with " " etc SourceFile=${SourceFile//%20/ } # replace all %25 with "%" etc SourceFile=${SourceFile//%25/%} # check that file exists if [ -e "$SourceFile" ] then #echo -e "\nCopying $SourceFile to $DESTPATH" echo -e "Copying file $TrackNo" # copy file to destination path cp "$SourceFile" "$DESTPATH" if [ $? != 0 ]; then { strErrorDesc="$strErrorDesc\nUnable to copy the following file:\n $SourceFile" } fi # test to see whether there is an album name if [ ! -z "$3" ] then NewFile="$DESTPATH${SourceFile##*/}" #TagAlbum "$NewFile" "$3" $TrackNo FileExt=${NewFile#*.} #echo "File extension is $FileExt" case "$FileExt" in "mp3" | "MP3" | "Mp3" | "mP3" ) #check that suitable tagger exists if type -p id3tag &>/dev/null then #call it echo -e "Writing mp3/id3 tag to $NewFile" id3tag -A "$3" -t $TrackNo "$NewFile" >/dev/null if [ $? != 0 ]; then { strErrorDesc="$strErrorDesc\nProblem with id3tag tagging the following file:\n $NewFile" } fi else strErrorDesc="$strErrorDesc\nUnable to tag the following file because id3tag is not installed:\n $NewFile" fi ;; "m4a" | "M4A" | "M4a" | "m4A" ) #check that suitable tagger exists #call it if type -p AtomicParsley &>/dev/null then #call it echo -e "Writing m4a tag to $NewFile" AtomicParsley "$NewFile" --album "$3" --tracknum $TrackNo --overWrite >/dev/null if [ $? != 0 ]; then { strErrorDesc="$strErrorDesc\nProblem with AtomicParsley tagging the following file:\n $NewFile" } fi else strErrorDesc="$strErrorDesc\nUnable to tag the following file because AtomicParsley is not installed:\n $NewFile" fi ;; * ) # Default Option echo "\nError: $FileExt is an unrecognised format\n" ;; esac fi else echo -e "\nError: $SourceFile does not exist! ********\n" fi fi done <"$SOURCELIST" if [ ${#strErrorDesc} -gt 0 ] then echo -e $strErrorDesc exit 10 fi exit 0