-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitunes_insert_artwork.pl
executable file
·206 lines (178 loc) · 5.54 KB
/
itunes_insert_artwork.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
###############################################################################
#
# itunes_insert_artwork.pl
#
# This script will tag your files using artwork downloaded using iTunes 7
#
# written by: Robert Jacobson (http://home.comcast.net/~teridon73/itunesscripts)
# Last Updated: 03 Jan 2007
# Version 1.0
#
# ImageMagick modification by: Russell Davis (https://github.com/russelldavis)
# This version will send all images through ImageMagick for better compression.
#
# This script is GPL v2. see http://www.gnu.org/copyleft/gpl.html
#
# Use option "-k" to keep the artwork files extracted
# (in the same location as the song file)
# (the default is to remove the files)
###############################################################################
use File::Basename;
my $PROGNAME = basename($0);
my $VERSION = "1.0";
my $AUTHOR = "Robert Jacobson";
my $HOMEPAGE = "http://home.comcast.net/~teridon73/";
my $YEAR = 2007;
my $GNU_URL = "http://www.gnu.org/copyleft/gpl.html";
{
print
"**************************************************************\n" .
"$PROGNAME version $VERSION, Copyright (C) $YEAR $AUTHOR\n" .
"Visit $HOMEPAGE for updates\n" .
"$PROGNAME comes with ABSOLUTELY NO WARRANTY;\n".
"This is free software, and you are welcome\n" .
"to redistribute it under certain conditions\n" .
"for details see $GNU_URL.\n" .
"**************************************************************\n" .
"\n"
;
}
use strict;
use Win32::OLE;
use Data::Dumper;
use File::Basename;
use Getopt::Std;
use Image::Magick;
# Create a signal handler to destroy the iTunes object
# in case our program quits before the end
use sigtrap 'handler', \&quit, 'normal-signals';
my %artformat = (
0 => 'Unknown',
1 => 'JPEG',
2 => 'PNG',
3 => 'BMP',
);
my %artformat_ext = (
0 => 'unk',
1 => 'jpg',
2 => 'png',
3 => 'bmp',
);
getopts('k');
## Create the OLE Object
my $iTunes = Win32::OLE->new('iTunes.Application') or die Win32::OLE->LastError();
# Check version first!
my $version = $iTunes->Version;
if ($version !~ /^7/) {
print "Sorry, this script requires iTunes 7\n";
quit();
}
# Get the possible sources
my $sources = $iTunes->Sources();
my $sourcesCount = $sources->Count();
my $source = '';
my $sourceKind = '';
my $n = 1;
my $remove_files = 1;
our $opt_k;
if ($opt_k) {
$remove_files = 0;
} else {
print "Keep extracted image files? [n] ";
chomp(my $answer = <STDIN>);
if ($answer =~ /^y/i) {
$remove_files = 0;
}
}
# For each source, figure out kind
for ($n = 1; $n <= $sourcesCount; $n++) {
$source = $sources->Item($n);
$sourceKind = $source->Kind();
# print "source no. " . $n . " is ";
# print $sourceKind . " -- ";
if ($sourceKind == 0) {
print "Unknown Source\n"; }
if ($sourceKind == 1) {
print "Library Source\n";
# Get the playlists in the Library
my $playlists = $source->Playlists();
my $num_playlists = $playlists->Count();
print "There are $num_playlists playlists\n";
# For each playlist, show the name and number of tracks
for (my $j = 1 ; $j <= $num_playlists; $j++) {
my $playlist = $playlists->Item($j);
my $playlist_name = $playlist->Name();
print "\t$j : $playlist_name\n";
}
print "Enter comma-separated playlist numbers: ";
chomp (my $nums = <STDIN>);
my @nums = split(/,/ , $nums);
for my $i (@nums) {
my $playlist = $playlists->Item($i);
my $playlist_name = $playlist->Name();
print "You selected $playlist_name\n";
my $tracks = $playlist->Tracks;
my $num_tracks = $tracks->Count();
print "\t$num_tracks tracks\n";
my %seen;
# Get all the tracks in the playlist
for (my $k = 1 ; $k <= $tracks->Count ; $k++ ) {
#print "num: " , $num_tracks , " Count: ", $tracks->Count , " k: ", $k , "\n";
my $track = $tracks->Item($k);
my $track_kind = $track->Kind();
if ($track_kind == 1) {
my $count = $track->Artwork->Count;
if ($count > 1) {
print "ERROR - found file with more than one artwork " . $track->Name . "\n";
}
for (my $c = 1 ; $c <= $count ; $c++) {
my $artwork = $track->Artwork->Item($c);
if ($artwork->IsDownloadedArtwork) {
my $name = $track->Name;
my $album = $track->Album;
print "name is \"$name\"\talbum: \"$album\"\n";
my $format = $artwork->Format;
my($fnFile, $fnDir, $fnExt) = fileparse($track->Location, qr/\.[^.]*/);
my $fnBase = $fnDir . $fnFile;
my $filename = $fnBase . ".art.orig." . $artformat_ext{$format};
my $compressedFilename = $fnBase . ".art." . $artformat_ext{$format};
$artwork->SaveArtworkToFile($filename);
if (not -s $filename) {
print "ERROR saving file $filename\n";
} else {
# Get the file down to a reasonable size (many of the jpegs from itunes images are over 500K)
my $image = new Image::Magick;
$image->Read($filename);
$image->Set(quality=>'85');
my $err = $image->Write($compressedFilename);
if ($err || not -s $compressedFilename) {
print "ERROR compressing $name: $err\n";
} else {
# insert into file
print "inserting artwork file $compressedFilename\n";
my $hr = $artwork->SetArtworkFromFile($compressedFilename);
if ($hr < 0) {
print "ERROR setting artwork: $hr\n";
}
}
}
if ($remove_files) {
unlink $filename;
unlink $compressedFilename;
}
}
}
}
}
}
}
}
# Destroy the object. Otherwise zombie object will come back
# to haunt you
quit();
sub quit
{
# This destroys the object
undef $iTunes;
exit;
}