kyoto dreams
Saturday, May 14, 2005
  It's just like magic it all starts with a simple idea. export playlists from itunes to my dlink media thingy so i can listen to my current batch of favorites.

seems rational to me. the dlink thingy supports playlists and itunes supports exporting playlists. let's go... several minutes later. bad news, flappy, the dlink thingy only supports playlists that are created on the dlink thingy via the lame ass, finger contorting, obviously designed for aliens remote control. that sucks.

well, me mate from work (mate because he's british) told me about a neato sounding media server called twonkyvision which claims to be compatible with the dlink. so i download the free version and get it running pretty quickly. it works great. sorry though, flappy, old chum, more bad news. the playlists exported from itunes aren't m3u playlists, which is what twonkyvision supports. ok, how bad could it be. i take a look at the file created by itunes.

G:\media-server>cat fourteen.txt
ÿ_N a m e A r t i s t C o
p i n g G e n r e S i
b e r D i s c C o u n t T r
u n t Y e a r D a t e M
t R a t e S a m p l e R a t

seems like a simple tab-delimited file.

i google for the m3u file format. The format is straightforward and clear and the page gives great explanations of the fields.

#EXTM3U
#EXTINF:111,3rd Bass - Al z A-B-Cee z
mp3/3rd Bass/3rd bass - Al z A-B-Cee z.mp3
#EXTINF:462,Apoptygma Berzerk - Kathy´s song (VNV Nation rmx)
mp3/Apoptygma Berzerk/Apoptygma Berzerk - Kathy's Song (Victoria Mix by VNV Nation).mp3
#EXTINF:394,Apoptygma Berzerk - Kathy's Song
mp3/Apoptygma Berzerk/Apoptygma Berzerk - Kathy's Song.mp3
#EXTINF:307,Apoptygma Bezerk - Starsign
mp3/Apoptygma Berzerk/Apoptygma Berzerk - Starsign.mp3
#EXTINF:282,Various_Artists - Butthole Surfers: They Came In
mp3/Butthole_Surfers-They_Came_In.mp3


both in and out look simple, so i vim a perl script to parse the itunes file and print the info to stdout as m3u. one test run and i can see this won't be as simple as i thought. seems the itunes file is unicode (as evidenced by the spaces between every character, doh!).

holy bleeding piehole, how's this gonna work. i know jack about unicode in perl. but wait! java is unicode from the inside out, right? one quick file parsing class later and the answer is quite obviously, no. same exact problem as before. oh no, i'm going to have to read some more. i take a quick look at the file in binary:

FF FE 4E 00 61 00 6D 00 - 65 00 09 00 41 00 72 00 ..N.a.m. - e...A.r.
74 00 69 00 73 00 74 00 - 09 00 43 00 6F 00 6D 00 t.i.s.t. - ..C.o.m.
70 00 6F 00 73 00 65 00 - 72 00 09 00 41 00 6C 00 p.o.s.e. - r...A.l.
62 00 75 00 6D 00 09 00 - 47 00 72 00 6F 00 75 00 b.u.m... - G.r.o.u.
70 00 69 00 6E 00 67 00 - 09 00 47 00 65 00 6E 00 p.i.n.g. - ..G.e.n.


it looks like the 2 leading bytes are significant because they don't seem to have anything to do with playlists. i google "FF FE unicode". bahm! some page on msdn reports that these 2 bytes are known as a byte-order mark:

EF BB BF UTF-8
FE FF UTF-16/UCS-2, little endian
FF FE UTF-16/UCS-2, big endian
FF FE 00 00 UTF-32/UCS-4, little endian.
00 00 FE FF UTF-32/UCS-4, big-endian.

a little more reading and i now know that the InputStreamReader class has a constructor that allows you to pass in the character set used by the stream being read. taking my cue from the above table i use:

new InputStreamReeader(fileName, "UTF-16");

everything goes well. a couple of tweaks later and wallah, all hail the rudimentary itunes2m3u utility. it works well enough. twonkyvision likes the m3u files and the dlink loves twonkyvision. i listen to music for 5 minutes and then go back to playing with virtualdub.

the whole adventure took about 45 minutes.

...24 hours later...

weeeehhhhhhhhhhhhh!!!!, ala pwned

some of the exports are not coming out as utf-16 any longer, so i changed the util to take the character set as an argument and default to us-ascii. i hate computers, i hate itunes, i hate programming, and i know it's really all my fault.


warning! crude code ahead
here's the utility for educational purposes :

import java.io.*;

public class Ipod2M3u {
public static void main(String []args) throws Exception {
String file = args[0];
String charset = args.length == 1 ? "US-ASCII" : args[1].trim();


BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
String line = null;

// throw away first line, it's a header
line = in.readLine();

System.out.println("#EXTM3U");

while ( (line = in.readLine()) != null ) {
//System.out.println(line);
String []fields = line.split("\t");

// if we're missing fields, it's probably
// because the info is split between 2 lines
if ( fields.length < 24 ) {
line = line + in.readLine();
fields = line.split("\t");
}

String name = fields[0];
String playSeconds = fields[7];
String location = fields[24];

System.out.println("#EXTINF:" + playSeconds + "," + name);
System.out.println(location);
}

System.out.println();

in.close();

}
Comments: Post a Comment

<< Home

ARCHIVES
May 2005 / June 2005 / August 2005 / January 2006 / March 2006 /


Powered by Blogger