public
Last active

This takes my playlists, managed by mpd, fixes them up, and places them in a staging folder, ready to sync with my Android phone. The script also asks if you'd like to sync the music after staging. Who needs iTunes anyway.

  • Download Gist
plsync.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#!/usr/bin/env zsh
 
# Takes all my playlists from ~/.mpd/playlists, fixes them up, and creates a
# folder for each, along with the music they reference.
 
# The sync stage requires an sshd server to run on your phone, as well as the rsync executable.
# - http://linux.wxs.ro/2011/08/05/rsync-your-android/
 
MPD_MUSIC_ROOT="${HOME}/Music" # Root of your MPD library
MPD_PLAYLIST_ROOT="${HOME}/.mpd/playlists" # MPD playlist folder
SYNC_MUSIC_ROOT="${HOME}/Android/Sync/Music" # Staging folder (receives all files prior to sync)
PLAYLIST_ROOT="${SYNC_MUSIC_ROOT}/playlists" # Where to place playlist files (m3u) for sync
PHONE_MUSIC_ROOT="/sdcard/Music" # Where the music will live on your phone
PHONE_ADDRESS="Nexus" # Address to access your phone over ssh for sync
 
# Output stuff to stdout if $DEBUG evaluates to anything
# $ DEBUG=1 plsync.sh
function debug()
{
[[ ! -z "$DEBUG" ]] && echo $*
}
 
# Sync stuff with phone over rsync+ssh
function sync() {
rsync -vzrL --delete-after --no-perms --no-t ${SYNC_MUSIC_ROOT}/ ${PHONE_ADDRESS}:${PHONE_MUSIC_ROOT}/
}
 
# Ask for confirmation on something
function confirm()
{
echo -n "$@ [y/N] "
read answer
for response in y Y yes YES Yes Sure sure SURE OK ok Ok kk; do
[[ "_$answer" == "_$response" ]] && return 0
done
 
return 1
}
 
[[ ! -d "$PLAYLIST_ROOT" ]] && mkdir -p "$PLAYLIST_ROOT"
 
for pl in ~/.mpd/playlists/*.m3u; do
plname="$(basename ${pl})"
plname="${plname%%.*}"
plfolder="${SYNC_MUSIC_ROOT}/${plname}"
plfile="${PLAYLIST_ROOT}/${plname}.m3u"
 
[[ -f "$plfile" ]] && rm -f "$plfile"
[[ -d "$plfolder" ]] && rm -rf "$plfolder"
[[ ! -d "$plfolder" ]] && mkdir -p "$plfolder" && touch "$plfile"
 
while read line; do
f="${MPD_MUSIC_ROOT}/${line}"
bn=$(basename "$f")
 
if [[ -f "$f" && ! -f "${plfolder}/${bn}" ]]; then
ln -s "$f" "$plfolder"
echo "${PHONE_MUSIC_ROOT}/${plname}/${bn}" >> "$plfile"
fi
 
done < "$pl"
done
 
confirm "Would you like to sync with your phone? (Make sure sshd runs on the device)" && sync

How could I add a list of playlists to sync? Not every playlist should get synced. for pl in ~/.mpd/playlists/*.m3u; do?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.