views:

6951

answers:

5

I have a collection of movies and TV shows in iTunes, and I'd like to rename them to an XBMC compatible naming convention without breaking the links in iTunes.

All the necessary metadata (season number, show name, episode number, etc) seems to be in an XML file that iTunes manages, and the episode name is the current file name. So programmatically renaming the files seems fairly straightforward but how do I keep the iTunes library straight at the same time? Is it enough to rewrite the XML file to point to the new file names?

I'd rather not get into applescript if I can avoid it (life is too short), however if it is easier to do it that way I may look at it. Otherwise I'd ideally like to do this in ruby.

+2  A: 

Not sure if you're on a Windows box but honestly if you can write even a little Javascript the iTunes COM API is very easy to use.

Below is a sample of what it will look like. (this is NOT tested, so only use it as a reference.)

var    ITTrackKindFile  = 1;
var iTunesApp = WScript.CreateObject("iTunes.Application");
var deletedTracks = 0;
var mainLibrary = iTunesApp.LibraryPlaylist;
var tracks = mainLibrary.Tracks;
var numTracks = tracks.Count;
var i;
var    RenameTarget;

while (i > 0)
{
    var currTrack = tracks.Item(numTracks);

    // is this a file track?
    if (currTrack.Kind == ITTrackKindFile)
    {
            RenameTarget = "Stuff from other properties"
     currTrack.Name = RenameTarget; //
    }
    numTracks++;
}

Good Luck.

Dan Williams
thanks - my library is on a mac though so I can't use this method
frankodwyer
+2  A: 

On Windows Apple provides the iTunes COM for Windows SDK that works quite well. It is created using a COM interface so it works with almost every language available for Windows. It provides methods for renaming Tracks and Playlists. I have used this to do all kinds of things with my Library.

I do not know what's available on a MAC, but I believe AppleScript is the best native way to access what's available. There is a project called "EyeTunes" that provides a Cocoa framework. There is a site devoted to Applescript ("Doug's AppleScripts for iTunes"). Here is a site showing how to access iTunes from perl.

If you want to rename both the file and the iTunes name then it's probably better to change the track name, remove the file from the library, rename the file, and then re-add the track. You would need to preserve information like last played, playcount, etc.

bruceatk
thanks, I will take a look at those links
frankodwyer
A: 

Check out EpNamer at http://www.epnamer.com/. It requires .NET but is exactly what you're looking for and XBMC is exactly what I use it for too.

You might also look into using Media Companion since you're most likely using the library functionality of XBMC, which you can read about here:

http://xbmc.org/forum/showthread.php?p=217769

It'll make .nfo and .tbn files for each movie and TV show that you've got which XBMC will scrape before going out to the internet for information.

Enjoy!

A: 

Another way to play with the iTunes xml file is to use the plist module.

Keltia
+1  A: 

Late but I needed exactly the same for Plex on OS X.

My solution was to write a Python script that queries iTunes for its TV shows via the AppleScript bridge, gets the file path from each track's location attribute and then creates a symlink to the original file, using iTunes metadata to give the link a name suitable for Plex/XBMC etc.

The advantage is you can painlessly create multiple sets of links with different naming schemes, e.g. My Name Is Earl S01E10.m4v or My Name Is Earl 01x10 White Lie Christmas.m4v

The core of the script is (where track is the reference provided by the appscript bridge):

def trackInfo(track):
"""returns tuple (filename, filepath, show)

track is iTunes playlist track from appscript bridge,

filename is of format "Showname 01x02 Episode Name.avi" where 
01 is the season number, 02 the episode number and the extension
will match that of filepath.

filepath is the UNIX path to the original file.
"""
try:
    path = track.location().path
except CommandError:
    return None, None, None
ext = os.path.splitext(path)[1]
name = "%s %02dx%02d %s%s" % (track.show(), track.season_number(), 
                                track.episode_number(), track.name(), ext)
return name, path, track.show()
wbg
nice solution - the symlink idea is great. I could see using this to create a separate directory structure alongside the itunes one for other media players, without having to duplicate everything. Though as itunes has a habit of renaming files it may be safer to keep the original elsewhere and have the itunes file be a symlink to that - not sure if itunes will handle symlinks like that as intended though.
frankodwyer
As long as you use proper symlinks and not OS X aliases, most applications have no problem with links. That said, I think it's much safer to feed the symlinks to the program that only reads the files. Less can go wrong.iTunes renaming files is not a problem. I just run the script regularly in the background (or as needed). It prunes any dead links first and then rebuilds the link tree. iTunes provides the file path anyway, so if it has renamed something, it will tell the script.
wbg