Announcement

Collapse
No announcement yet.

iTunes Playlist Converter

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • iTunes Playlist Converter

    YOU NEED .NET 1.1 for the Converter
    Tested with iTunes 6.0.1.3 - Let me know if it doesnt work.

    I couldnt work out how to properly make Centrafuse playlists, so i wrote this to convert my iTunes playlists to Centrafuse playlists.

    Usage:

    Right click on your Playlist in iTunes, and select Export. Export the file to XML format.

    Run convert.exe with your file that your exported.

    Eg: convert.exe myplaylist.xml

    this will produce myplaylist.mpl which you can insert in your Centrafuse playlist folder. (C:\Program Files\Flux Media\Centrafuse\playlists)

    I've included the code so you can see it contains no viruses :P

    I hope its useful, it was made just for me, so ignore the quirks

    Code:
    using System;
    using System.Xml;
    using System.Collections;
    using System.Reflection;
    using System.IO;
    
    namespace iTunesPlaylistTOCentrafuse
    {
    	class Class1
    	{
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			Console.WriteLine("----------------------------------------");
    			Console.WriteLine("iTunes to Centrafuse Playlist Converter.");
    			Console.WriteLine("(C) Konnichiwa (JCarter) 2005");
    			Console.WriteLine("----------------------------------------");
    			Console.WriteLine(string.Empty);
    
    			if(args.Length != 1 || args[0] == "/?")
    			{
    				Console.WriteLine("Usage: convert.exe InputFilename [inputfile.xml]");
    				return;
    			}
    
    			Console.WriteLine("Opening iTunes Playlist...");
    			string inputFilename = args[0];
    			XmlTextReader reader = new XmlTextReader(inputFilename);
    			ArrayList songList = new ArrayList();
    			Console.WriteLine("Reading songs...");
    			try
    			{
    				while(reader.Read())
    				{
    					XmlNodeType t = reader.NodeType;
    					if(reader.NodeType == XmlNodeType.Text)
    					{
    						if(reader.Value == "Track ID")
    						{
    							songList.Add(ExtractSong(reader));
    						}
    						else if(reader.Value == "Playlists")
    						{
    							break;
    						}
    					}
    				}
    			}
    			catch
    			{
    				Console.WriteLine("Failed reading iTunes playlist.");
    				return;
    			}
    			Console.WriteLine("Read {0} songs.", songList.Count);
    
    			string outputFilename = System.IO.Path.GetFileNameWithoutExtension(inputFilename);
    			outputFilename += ".mpl";
    
    			Console.WriteLine("Writing songs...");
    			try
    			{
    			using(FileStream writer = new FileStream(outputFilename, FileMode.Create))
    			{
    				writer.WriteByte(0xFF);
    				writer.WriteByte(0xFE);
    				for(int i = 0; i < songList.Count; i++)
    				{
    					Song song = (Song)songList[i];
    					string location = song.Location;
    					location = location.Replace("%20", " ");
    					location = location.Replace("file://localhost/", "");
    					string line = song.Artist + " - " + song.Name + "|" + location;
    					byte[] unicodeLine = System.Text.UnicodeEncoding.Unicode.GetBytes(line);
    					writer.Write(unicodeLine, 0, unicodeLine.Length);
    					if(i != songList.Count - 1)
    					{
    						byte[] returnChars = System.Text.UnicodeEncoding.Unicode.GetBytes("\r\n");
    						writer.Write(returnChars, 0, returnChars.Length);
    					}
    				}
    			}
    			}
    			catch
    			{
    				Console.WriteLine("Failed writing Centrafuse playlist.");
    				return;
    			}
    			Console.WriteLine("Wrote {0} songs.", songList.Count);
    			Console.WriteLine("Finished.");
    		}
    
    		private static Song ExtractSong(XmlTextReader reader)
    		{
    			FieldInfo[] fieldInfo = typeof(Song).GetFields();
    			Song song = new Song();
    			while(reader.Read())
    			{
    				if(reader.NodeType == XmlNodeType.EndElement)
    				{
    					if(reader.Name == "dict")
    					{
    						Console.WriteLine("Read: " + song.Artist + " - " + song.Name);
    						return song;
    					}
    				}
    				if(reader.NodeType == XmlNodeType.Text)
    				{
    					string key = reader.Value.Replace(" ", "");
    					Skip(reader);
    					string val = reader.Value;
    					for(int i = 0; i < fieldInfo.Length; i++)
    					{
    						if(fieldInfo[i].Name == key)
    						{
    							fieldInfo[i].SetValue(song, val);
    							break;
    						}
    					}
    				}
    			}
    			return song;
    		}
    
    		private static void Skip(XmlTextReader reader)
    		{
    			reader.Read();
    			reader.Read();
    			reader.Read();
    		}
    
    		private class Song
    		{
    			public string TrackID;
    			public string Name;
    			public string Artist;
    			public string Album;
    			public string Genre;
    			public string Kind;
    			public string Size;
    			public string TotalTime;
    			public string TrackNumber;
    			public string TrackCount;
    			public string Year;
    			public string DateModified;
    			public string DateAdded;
    			public string BitRate;
    			public string SampleRate;
    			public string Season;
    			public string PersistentID;
    			public string TrackType;
    			public string Location;
    			public string FileFolderCount;
    			public string LibraryFolderCount;
    		}
    	}
    }
    Attached Files

  • #2
    At the top of my head question: This would require the mp3 files will be physically stored in the same folder as iTunes' library and Centrafuse's? Great idea! Will definitely be useful to me!

    Next version would be nice if it could automatically read existing iTunes playlists and then automatically generate a centrafuse version, all within centrafuse... That's just me though
    carpc blog

    Comment


    • #3
      I quickly glances through the iTunes COM documentation and didnt see any mention of 'export' functionality therefore doing this from code may be tricky.

      The iTunes APIs are missing a lot of stuff.

      I wish they exposed some kind of advanced querying (i.e. I want to find all albums with more than 5 songs).. but beyond that they are missing basic functionality such as exporting, and finding songs by genre.
      B Smoov
      Project Status: 90% Complete

      Next Step: TIVO for Radio

      Ampie Case :: MII10000 Mobo :: M1-ATX PSU :: 512MB RAM :: 2.5" HD, 60GB, 5400rpm, 16MB Buffer :: DWW-700H :: Centrafuse

      Comment


      • #4
        yeah, i did want to just have an option that would keep my iTunes playlists in sync with Centrafuse, but i cant get to iTunes playlists via the API.

        Comment


        • #5
          I doubt anyone is still watching this thread but I created a plugin for Centrafuse to play iTunes, Winamp playlists. You can get it from here:

          http://forums.fluxmedia.net/plugin-d...tunes-etc.html
          My Install - 2003 Lexus IS300

          Comment

          Working...
          X