Sync music via FTP from a Linux server


Last Updated:

  1. johnnychicago

    johnnychicago New Member

    Good evening,

    Part of why I bought a HTC Desire to replace my 2G iPhone was getting away from iTunes - piece of bloat required me to keep a Windows computer around, updating music was pesky, etc.

    The plan with the HTC is to sync it over Wifi when it connects to my home network. The system it syncs with is a media server running Linux and storing my music collection.

    I've gotten so far to have Tasker start FTPServer on connecting to Wifi, and have built a little script called every 10 minutes by cron that checks for presence of the phone. If it finds the phone, it connects and syncs a folder, then makes the ftp server on the phone shutdown, so it won't reconnect every 10 minutes.

    So far, so good (I'll gladly share the bash script, if anybody wants to use. Needs some more polishing, though)

    The bit that bugs me is playlists. I can create m3u playlists and copy them over fine, however once they are on the phone, they disappear from the Music folder and cannot be changed/deleted anymore - except by the built in music app. This is quite inconvenient, since I have a number of podcasts that get updated daily and that I'd like to take on the phone, with a playlist showing the newest episodes first. I can make the playlist easily on the computer, however the phone will end up with multiple copies of these playlists, which I can't get rid off.

    Can anybody point me someplace that explains how Android handles playlists?

    If there's no way, I'll work around by editing track/album MP3 tags and grouping my stuff together this way, but I'd prefer using simple playlists...

    Advertisement
  2. fratermus

    fratermus Well-Known Member

    You can longpress-delete playlists in the Music app.

    I was unaware the playlists get moved, though this explains a side effect I'm seeing. My script wipes out a /sdcard/audio directory and reloads music and timestamp-named playlists, but the old .m3u files continue to display in the playlist screen. I have to delete them as described above. If they are getting moved this explains why the wipe doesn't clear them.

    Does the auto-genned "recently added" faux playlist do anything for you?

    Looking forward to other podcast-related posts.
  3. fratermus

    fratermus Well-Known Member

    [connection burp]
  4. johnnychicago

    johnnychicago New Member

    Yes, I found out about that, too. But it's quite inconvenient to always have manually get rid of them.

    It does :)

    I don't wipe, but use lftp's reverse mirror to just copy what's changed.

    If I can't find how to get rid of the playlists, I'll use them server side to relabel ID3 tags and that way tie my stuff together. Make the album tag the playlist name, or something along these lines.

    I don't have that one.

    Ah, not much, really. Using podcatcher to download them, then creating a playlist of the last 50 of them with find, sort and head. My music lives in a directory structure suitable for Squeeze Server, I just want a comfortable way to sync a tiny subset of it to the phone.
  5. fratermus

    fratermus Well-Known Member

    I only sync after I've listened to all the 'casts so I'm deleting once a week or so. I can see how it would be a problem if syncing were more frequent.

    Related: I suspect the .m3u is being converted to some kind of internal db on the device. I did a find on /sdcard for *.m3u and only found my original. Did the same on / in adb shell and found nothing.

    Before I found the longpress deletion my workaround was to put a timestamp in the playlist name so I could tell them apart. :-o


    Hmmm. Maybe it's a cyanomod thing. I thought his version of the Music app was stock; perhaps not. Claims to be v1.7.5.2 and the credits include Cyano, so I guess they hacked it.



    I meant in general. Music listeners seem to be the majority in media discussions. There probably are more of us who aren't talking much.

    My setup looks like this:
    1. download - podracer does the downloads from a nightly cronjob, scripts do a bit of renaming and normalizing. I used to use hpodder but it disappeared from the ubuntu/debian repositories. I may have to find the source and roll my own. Weird-looking dependencies, though.
    2. script generates M3U and sync to G1

    Do you listen to podcasts on the workstation mainly, and carry the subset on the phone for opportunistic listening? I wouldn't be able to keep track of what I had and hadn't listened to if I didn't do the total-wipe-and-load after listening to everything.
  6. fratermus

    fratermus Well-Known Member

    Update: check this post, then this.

    At least we know how it works (and that it's been a problem for others).
    johnnychicago likes this.
  7. johnnychicago

    johnnychicago New Member

    fratermus, thanks for those links.

    I now have it running with basically using playlists to change ID3 tags on the computer, so I can group them on the phone (without actually transmitting the playlists to the phone), but I'll investigate in this.
  8. jwood79

    jwood79 New Member

    Really!? Great, could you possibly share with me? :) Out of interest, what flavour of linux are you running on your mediaserver?
  9. johnnychicago

    johnnychicago New Member


    Course, here you go:

    I'm running FTPServer on the phone, being started at connection to my home wifi - trust you get along to set this up yourself. In order to facilitate life, I've told my wireless router to always hand out the same IP to the phone.

    I have a music directory with mp3-files (and folders) that is being mirrored to the phone, as well as a sync directory in which I put M3U-Playlists (that I generate e.g. for my podcasts). Those playlists have the mp3-files with full pathnames, and the script copies those files to the music directory, and changes album information (on the copies) to reflect the playlist.

    Script runs from cron every half hour and checks if the phone's there. If so, it compiles the playlist albums, mirrors the music folder to the phone, and switches off the FTPServer on the phone (to prevent it from resyncing every half hour).

    Dependencies are netcat, id3v2 and lftp - probaby apt-gettable pretty much anywhere. My distribution is Debian.

    Code (Text):
    1.  
    2. #!/bin/bash
    3.  
    4. # settings
    5.  
    6. HOST="192.168.x.y"   #this is the phone's IP address
    7. PORT="2222"
    8. USER="abc"   #for FTPserver on phone
    9. PASSWD="XXXXXXX"
    10. SYNC_DIR="/mnt/sync/music"  # on the computer
    11. PL_DIR="/mnt/sync"
    12. PL_PREFIX="_Playlist-"
    13. PHONE_DIR="/sdcard/Music"  # where the phone keeps its music
    14. LOCKFILE="/var/lock/sync_android"
    15.  
    16. # first, scan the phone to see if it is there. If not, exit
    17.  
    18. nc -z "$HOST" "$PORT" || exit;
    19.  
    20.  
    21. # lockfile
    22.  
    23. if [ ! -e $LOCKFILE ]; then
    24.         trap "rm -f $LOCKFILE; exit" INT TERM EXIT
    25.         touch $LOCKFILE
    26. else
    27.         exit
    28. fi
    29.  
    30.  
    31. # create folders for playlists
    32.  
    33. for PLFILE in "$PL_DIR"/*.m3u ; do
    34.         PL_DIR_NAME="$SYNC_DIR/$PL_PREFIX$(basename "$PLFILE" .m3u)"
    35.         mkdir "$PL_DIR_NAME" || rm "$PL_DIR_NAME"/*
    36.         COUNTER="1"
    37.         egrep -v "^#" "$PLFILE" |  while read LINE
    38.         do
    39.                 cp "$LINE" "$PL_DIR_NAME"
    40.                 id3v2 -A "$PL_PREFIX-$(basename "$PLFILE" .m3u)" -T "$COUNTER" "$PL_DIR_NAME/$(basename "$LINE")"
    41.                 let "COUNTER+=1"
    42.         done
    43. done
    44.  
    45. # delete all m3u playlists now so they don't get to phone
    46.  
    47. rm -rf "$SYNC_DIR"/*.m3u
    48. rm -f "$PL_DIR"/*.m3u
    49.  
    50. # since this may have taken a while, check again if phone is there
    51.  
    52. nc -z "$HOST" "$PORT" || exit;
    53.  
    54. # access phone by ftp, mirror the phone directory to the sync directory
    55. # and shutdown the phone ftp server
    56.  
    57. lftp -p "$PORT" -u "$USER","$PASSWD" "$HOST" <<END_SCRIPT
    58. mirror --reverse --delete-first --only-newer --verbose "$SYNC_DIR"  "$PHONE_DIR"
    59. quote site shutdown
    60. quit
    61. END_SCRIPT
    62.  
    63. rm $LOCKFILE
    64. exit 0
    65.  
    jwood79 likes this.
  10. jwood79

    jwood79 New Member

    Ah, brilliant, thanks a lot. I'm running an 'unslung' NSLU2 with a custom disto for my music. Hoping I can get a set up similar to yours working!

Share This Page