Created
February 18, 2012 16:54
-
-
Save buty4649/1860167 to your computer and use it in GitHub Desktop.
iTunes Music Library to m3u Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage