Remotely updating iTunes and MPD at the same time

Like many other people, I enjoy listening to music. I have aggregated a reasonably large (~90 GB) library of digital music over the years, and, of course, my favorite way to listen to it is MPD.

Right now, I'm running MPD on my "media center" computer hellboy2, which (surprisingly?) is a MacBook running OSX Yosemite connected to an HDTV. Before I switched to MPD again (this is actually my second switch to MPD; I used it around 8 years ago on my first Linux systems), I used iTunes to manage my music library. One advantage of iTunes (and the main reason why I cannot stop using it) is that iTunes on a Mac is probably the most dependable way of syncing music to an iPod. This is the sole reason why I still want all of my music to be indexed by iTunes.

Up until recently, if I wanted to import music into my library, I would drag-and-drop the files into iTunes to make sure they end up in the right place. After that, I would run a quick mpc update to update my MPD library (which, of course, points to my "iTunes Media" folder).

This was a rather dirty approach to adding music to my library, and I cannot recommend it to anyone (Especially since it involves using GUI programs…). Fortunately, I stumbled upon this interesting post on StackExchange today. The user patrix explains that there exists a "magic" directory for iTunes that automatically imports its contents to the iTunes library. This is perfect! Using this "magic" folder, I can add add files to my iTunes by simply moving them into the folder ~/Music/iTunes/iTunes\ Media/Automatically\ Add\ to\ iTunes!

Naturally, I had to write a script that automates the process of adding music to my library. I usually write scripts in bash, but since this is really just a small wrapper around scp, I figured that I could whip up a fish-shell function instead. This is it:

function add-music --description 'Copy music files to a remote Mac, add them to iTunes, update mpd DB'
  # the remote computer
  set -l REMOTEHOST "hellboy2"
  set -l REMOTEUSER "trx"
  set -l REMOTEDIR "/Volumes/Data/Users/trx/Music/iTunes/iTunes\ Media/Automatically\ Add\ to\ iTunes"
  set -l REMOTE "$REMOTEUSER@$REMOTEHOST"
  # only do this if we have files to send
  if test (count $argv) = "0"
    echo "No files specified. Aborting."
    return -1
  end
  # now, test whether we can even speak to the remote
  if not ping $REMOTEHOST -c 1 -W 1 >/dev/null ^/dev/null
    echo "Can't reach remote host $REMOTEHOST. Aborting."
    return -1
  end
  # check whether SSH login works
  if not ssh $REMOTE "true" >/dev/null ^/dev/null
    echo "Can't establish SSH connection to remote host $REMOTEHOST (ip: "(dig +short $REMOTEHOST)"). Aborting"
    return -1
  end
  # now check whether iTunes is running on the remote
  if not ssh "$REMOTE" "ps aux | grep -q iTunes"
    echo "iTunes is not running on $REMOTE, you have to start iTunes first!"
    return -1
  end
  # if we reach this point, we're ready to send the files
  echo "Transferring music to $REMOTE..."
  scp $argv "$REMOTE":"$REMOTEDIR"
  echo "Updating music database..."
  ssh "$REMOTE" "mpc update"
end

This function can be used from any computer on the same network as the media center computer. So, for example, I can now take some MP3s on my main machine, ava, edit the ID3 tags to my liking using puddletag, and then place them in my library on hellboy2 using the above script. In the script, I am first making sure that scp can be used, then I copy all of the music files to the "magic" iTunes directory, and then I update the MPD library.

Quite elementary, but still cool. I did not expect iTunes to have a feature like this.

Hope everyone is enjoying the great music this summer,

Dennis

"
\n

RSS License: CC BY-SA 4.0

"