generate m3u in a directory

August 4, 2009 at 2:25 am 1 comment

| Gabriel |

This isn’t even remotely sociology, but it is code.

My daughter has a Playskool “Made for Me” child’s MP3 player that we bought for her back when I had a Windows machine. Now that I’m on a mac I’m having trouble updating the contents as the client is only for Windows (and doesn’t work with Wine). Fortunately it’s pretty transparent how it works and so I’m able to fix it so it works with Mac or Linux (or for that matter, Windows if you lose the installer disc). I’d imagine that this advice not only works for this particular product but many similar mp3 players or any other device that needs a file index in similar format.

Anyway, when you plug in the device it mounts as USB drive “Untitled.” Open it up and you see it has several folders: “Playtime,” “Soothing,” “Favorites,” and “Sounds.” Inside of each of these is a bunch of MP3s and an M3U.

The obvious thing is to just drag-and-drop MP3s into one of these folders but the device won’t recognize it because it uses the M3U file as a script. Fortunately it’s easy to edit an M3U. Even though your operating system likes to think of it as a media format (specifically, a playlist), an M3U is just a text file listing filepaths. This device uses old-school MSDOS FAT syntax (i.e., backslash for directories, allcaps, 8 letters for filename truncated with “~1”, 3 for extension, CRLF line breaks). For instance, here are the first few lines of the M3U for the “playtime” directory:

\PLAYTIME\3MONS~1.MP3
\PLAYTIME\2IFID~1.MP3
\PLAYTIME\PETERPAN.MP3

My solution was to drag and drop the files, then run this shell script to update the M3U. It goes by alphabetical order, but you can hand-edit it with a text editor, just move the lines around.

# make a backup of the old m3u file
cp Playtime.m3u Playtime.bak
# create a text file listing all the mp3 files
ls | grep -i mp3 > filelist.txt
# clean the text file by deleting whitespace and file extension, capitalizing everything
awk '{ gsub("\.mp3", ""); print $0;}' "filelist.txt" > tmp.txt ; mv tmp.txt filelist.txt
awk '{ gsub(" ", ""); print $0;}' "filelist.txt" > tmp.txt ; mv tmp.txt filelist.txt
awk '{print toupper($0)}' "filelist.txt" > tmp.txt ; mv tmp.txt filelist.txt
# if code is >8 chars, truncate to 6 chars and append "~1"
awk '{if (length($0)>8) {print substr($0, 1 , 6) "~1"} else {print $0}}' "filelist.txt" > tmp.txt ; mv tmp.txt filelist.txt
# add path and extension
awk '{print "\\PLAYTIME\\" $0}' "filelist.txt" > tmp.txt ; mv tmp.txt filelist.txt
awk '{print $0 "\.MP3"}' "filelist.txt" > tmp.txt ; mv tmp.txt filelist.txt
# use windows txt (crlf) instead of unix (lf)
awk '{ sub(/$/,"\r"); print }' "filelist.txt" > tmp.txt ; mv tmp.txt filelist.txt
mv filelist.txt Playtime.m3u

If you don’t know how to execute a shell script, just wrap it in the Automator action “run shell script” and execute the Automator workflow inside the appropriate directory.

Entry filed under: Uncategorized. Tags: .

If it’s genetic, why is it changing? Time consistency and reputation

1 Comment


The Culture Geeks


%d bloggers like this: