Skip to content

Instantly share code, notes, and snippets.

@buty4649
Created February 18, 2012 16:54
Show Gist options
  • Save buty4649/1860167 to your computer and use it in GitHub Desktop.
Save buty4649/1860167 to your computer and use it in GitHub Desktop.
iTunes Music Library to m3u Converter
#!/usr/bin/env perl
use strict;
use utf8;
use Mac::iTunes::Library::XML;
use Encode;
binmode STDOUT, ":utf8";
my $itunes = $ARGV[0] || die "Usage: itl2m3u <iTunes Directory>";
my $library = $ARGV[0] . '/iTunes Music Library.xml';
my $mediafolder = $itunes . "/iTunes Media/";
my $xml = Mac::iTunes::Library::XML->parse($library);
my $oldmusicfolder = $xml->{'Music Folder'};
my $playlists = $xml->{'Playlists'};
foreach my $playlist (values %{$playlists})
{
my $playlist_name = $playlist->{'Name'};
print qq/+ $playlist_name\n/;
open my $fh, '>', $playlist_name . ".m3u";
binmode $fh, ":utf8";
print $fh qq/#EXTM3U\n/;
foreach my $item (@{$playlist->{'items'}})
{
my $time = int($item->{'Total Time'} / 1000);
my $name = $item->{'Name'};
my $location = $item->{'Location'};
$location =~ s#^$oldmusicfolder#$mediafolder#o;
$location =~ s/%([0-9a-zA-Z]{2})/pack('H2',$1)/ge;
$location = decode('utf-8', $location);
print $fh qq/#EXTINF:$time,$name\n/;
print $fh $location,"\n";
}
close $fh;
}
# vim: ft=perl
@buty4649
Copy link
Author

Usage

> itl2m3u.pl <iTunes library path>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment