Pages

Saturday, April 4, 2009

M3U Playlist Copy Script

A couple of months ago I picked up a Garmin Nuvi 760 on the cheap. As it turns out this great GPS unit can also play music quite well, especially for riding on the motorcycle. The only problem was there was no good way to get playlists on the device that I could find. Enter the following bash script. This script will read an m3u file, copy all associated mp3 files, and generate a new m3u file. Now all I have to do is specifiy m3u files that I exported from Mozilla Songbird and the path to the Garmin's SD card.

 #!/bin/bash  
 # April 4, 2009  
 # m3u_cp.sh  
 #  
 # Take an m3u file and copy all associated mp3 files  
 # to a destination directory and generate a new m3u.  
 #  
 # Used to copy m3u playlists from computer to Garmin.  
 if [ $# -lt 2 ]; then  
 echo "Usage: m3u_cp.sh some.m3u /dst"  
 exit 0  
 fi  
 # Read the m3u file into an array  
 declare -a M3U  
 exec 10<"$1"  
 let count=0  
 while read LINE <&10; do  
 M3U[$count]=$LINE  
 ((count++))  
 done  
 exec 10>&-  
 # Determine the m3u's filename  
 if [[ $1 =~ [^/]*m3u ]]; then  
 m3u_path="$2/$BASH_REMATCH"  
 fi  
 # If playlist arleady exists, delete it  
 if [ -f "$m3u_path" ]; then  
 rm -f "$m3u_path"  
 fi  
 # Loop through the m3u lines  
 i=0  
 while [ $i -lt ${#M3U[@]} ]; do  
 # The current line is a comment, do nothing with it  
 if [ ${M3U[$i]:0:1} = "#" ]; then  
 echo ${M3U[$i]} >> "$m3u_path"  
 #Current line is a path to an mp3 file  
 else  
 # Get the current songs filename  
 if [[ ${M3U[$i]} =~ [^/]*mp3 ]]; then  
 song=$BASH_REMATCH  
 mpath=$( echo ${M3U[$i]} | tr -d '\r' )  
 # if the song doesn't exist, copy it to the desitnation folder  
 if [ -f "$2/$song" ]; then  
 echo File Exists -- $song  
 else  
 echo Copying -- $song  
 cp "$mpath" "$2/$song"  
 fi  
 # Write the song in the m3u file  
 echo $song >> "$m3u_path"  
 else  
 echo "The regex for finding the song's filename is fraked up."  
 fi  
 fi  
 let i=i+1  
 done  
 exit 0  

In the middle of writing this I really started wondering why I used bash. Perl would've been a lot easier.

11 comments:

  1. So today I found a cool program that will work with iTunes and has a nice GUI. However, the author stripped it from his site saying that some people were having issues with it deleted their entire target volume. He was not able to reproduce the error and neither was I. I managed to find a copy of it floating around and I'm posting it here if anyone else would like to try it. SyncTunes v2.0.0v2

    ReplyDelete
  2. thank you for writing this, I needed a script that did exactly this!

    ReplyDelete
  3. Thanks mate, I was just about to write my own little script to do this, but yours works perfectly!

    ReplyDelete
  4. Thanks for posting this, just what I was looking for.
    All the best, Brownster

    ReplyDelete
  5. Thanks dude! This is just what i needed!!

    ReplyDelete
  6. Seems not work with file like song.MP3

    ReplyDelete
  7. You're correct. It looks like I need to edit the script to be case-insensitve. It should be a quick fix. I should have time to post it this weekend.

    ReplyDelete
  8. Thanks... Exactly what I was looking for

    ReplyDelete
  9. Very useful, thanks for publishing this

    ReplyDelete
  10. It is very difficult to find a valid company that make your website. I also got a company which charged very lowest rate but made a very wonderful website download songs

    ReplyDelete
  11. Also, this is in reality great in light of the fact that an author composes all things considered. In any case, there is unquestionably more to screenplay composing than simply consolidating your words in a bit of content. screenwriter

    ReplyDelete