| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MP3::CreateInlayCard; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# $Id: CreateInlayCard.pm 535 2009-02-13 23:26:32Z davidp $ |
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
43481
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
41
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
32
|
|
|
9
|
1
|
|
|
1
|
|
1118
|
use File::Find::Rule; |
|
|
1
|
|
|
|
|
14303
|
|
|
|
1
|
|
|
|
|
11
|
|
|
10
|
1
|
|
|
1
|
|
632
|
use Music::Tag; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Cwd; |
|
12
|
|
|
|
|
|
|
use HTML::Template; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
MP3::CreateInlayCard - create a CD inlay label for a directory of MP3 files |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use MP3::CreateInlayCard; |
|
21
|
|
|
|
|
|
|
print MP3::CreateInlayCard::create_inlay( |
|
22
|
|
|
|
|
|
|
{dir => $dir, template => $template}); |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# $dir is the directory containing the MP3 files. If not specified, we'll |
|
25
|
|
|
|
|
|
|
# use the current directory. |
|
26
|
|
|
|
|
|
|
# $template is the filename of a template which HTML::Template should |
|
27
|
|
|
|
|
|
|
# read and populate. Alternatively, it can be a scalar reference, in which |
|
28
|
|
|
|
|
|
|
# case it will be taken to be the template contents to use. If it's not |
|
29
|
|
|
|
|
|
|
# supplied, a simple default built-in template will be used. |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# If you can't be bothered to write a script to call this module, use |
|
32
|
|
|
|
|
|
|
# 'makeinlay.pl' distributed with this package, or just do: |
|
33
|
|
|
|
|
|
|
perl -MMP3::CreateInlayCard -e \ |
|
34
|
|
|
|
|
|
|
"print MP3::CreateInlayCard(create_inlay({ (see example above) });" |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# If you're in the directory containing the MP3's, and you want to use the |
|
37
|
|
|
|
|
|
|
# built-in default template, just supply an empty hashref: |
|
38
|
|
|
|
|
|
|
perl -MMP3::CreateInlayCard \ |
|
39
|
|
|
|
|
|
|
-e "print MP3::CreateInlayCard(create_inlay({});" |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# or, more easily, use the eg/createinlay.pl script supplied with this |
|
42
|
|
|
|
|
|
|
# module. (Perhaps copy it to somewhere in your path and rename it to |
|
43
|
|
|
|
|
|
|
# 'createinlay', so you can just change directory into the appropriate |
|
44
|
|
|
|
|
|
|
# place and type 'createinlay'). |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Reads a directory of MP3s, and produces a HTML file you can print for a nice |
|
50
|
|
|
|
|
|
|
"inlay" for a CD case. Useful if you're burning a directory full of MP3s |
|
51
|
|
|
|
|
|
|
to a CD, and want an inlay label created for you. |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=cut |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=over 4 |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item create_inlay |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Go through the specified directory and produce the inlay. |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Takes a hashref of: |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=over 4 |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=item I |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
The directory containing the MP3 files |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item I |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
A scalar containing the filename of a template which HTML::Template should |
|
75
|
|
|
|
|
|
|
read and populate. Alternatively, it can be a scalar reference, in which |
|
76
|
|
|
|
|
|
|
case it will be taken to be the template contents to use. If it's not |
|
77
|
|
|
|
|
|
|
supplied, a simple default built-in template will be used. |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=back |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub create_inlay { |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $params = shift; |
|
86
|
|
|
|
|
|
|
if (!$params || ref $params ne 'HASH') { |
|
87
|
|
|
|
|
|
|
die "create_inlay() not passed hashref of params"; |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
my $startdir = $params->{dir} || getcwd; |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
if (!-d $startdir) { |
|
92
|
|
|
|
|
|
|
die "$startdir is not a directory"; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my @files = File::Find::Rule->file()->name('*.mp3')->in($startdir); |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
my $track = 1; |
|
99
|
|
|
|
|
|
|
my @tracks; |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# remember which artists and titles we saw, so we can work out whether this |
|
102
|
|
|
|
|
|
|
# is a compilation of various artists, a complete album by one artist, or |
|
103
|
|
|
|
|
|
|
# just a random assortment of tracks. |
|
104
|
|
|
|
|
|
|
my %artists; |
|
105
|
|
|
|
|
|
|
my %albums; |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
for my $file (@files) { |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
my $tags = Music::Tag->new($file, { quiet => 1 }); |
|
110
|
|
|
|
|
|
|
$tags->get_tag or warn "Error reading tags from $file" and next; |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my $length = sprintf('%02d',$tags->secs / 60) . ':' |
|
113
|
|
|
|
|
|
|
. sprintf('%02d',$tags->secs % 60); |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
push @tracks, { |
|
116
|
|
|
|
|
|
|
track => $track, |
|
117
|
|
|
|
|
|
|
title => $params->{prettify} ? |
|
118
|
|
|
|
|
|
|
_prettify($tags->title()) : $tags->title(), |
|
119
|
|
|
|
|
|
|
artist => $params->{pretify} ? |
|
120
|
|
|
|
|
|
|
_prettify($tags->artist()) : $tags->artist(), |
|
121
|
|
|
|
|
|
|
length => $length, |
|
122
|
|
|
|
|
|
|
}; |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
$artists{ $tags->artist() }++; |
|
125
|
|
|
|
|
|
|
$albums{ $tags->album() }++; |
|
126
|
|
|
|
|
|
|
$track++; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# open the html template (this code is rather crufty, refactor someday) |
|
130
|
|
|
|
|
|
|
my $template; |
|
131
|
|
|
|
|
|
|
my $template_opts = { die_on_bad_params => 0 }; |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
if ($params->{template}) { |
|
134
|
|
|
|
|
|
|
if (ref $params->{template} eq 'SCALAR') { |
|
135
|
|
|
|
|
|
|
# we've been given the template content: |
|
136
|
|
|
|
|
|
|
$template = HTML::Template->new( |
|
137
|
|
|
|
|
|
|
scalarref => $params->{template}, |
|
138
|
|
|
|
|
|
|
die_on_bad_params => 0 |
|
139
|
|
|
|
|
|
|
); |
|
140
|
|
|
|
|
|
|
} else { |
|
141
|
|
|
|
|
|
|
# it must be the filename of a template to use: |
|
142
|
|
|
|
|
|
|
if (-e $params->{template}) { |
|
143
|
|
|
|
|
|
|
$template = HTML::Template->new( |
|
144
|
|
|
|
|
|
|
filename => $params->{template}, |
|
145
|
|
|
|
|
|
|
die_on_bad_params => 0 |
|
146
|
|
|
|
|
|
|
); |
|
147
|
|
|
|
|
|
|
} else { |
|
148
|
|
|
|
|
|
|
die "Failed to open $params->{template} $!"; |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
} |
|
151
|
|
|
|
|
|
|
} else { |
|
152
|
|
|
|
|
|
|
# no template supplied, so use our default built-in one: |
|
153
|
|
|
|
|
|
|
$template = HTML::Template->new( |
|
154
|
|
|
|
|
|
|
scalarref => \$MP3::CreateInlayCard::default_template, |
|
155
|
|
|
|
|
|
|
die_on_bad_params => 0 |
|
156
|
|
|
|
|
|
|
); |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
if (!$template) { |
|
160
|
|
|
|
|
|
|
die "Uh-oh.. failed to create HTML::Template"; |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# fill in some parameters |
|
164
|
|
|
|
|
|
|
$template->param(tracks => sort { $a->{track} cmp $b->{track} } \@tracks); |
|
165
|
|
|
|
|
|
|
$template->param(artist => (scalar keys %artists == 1)? |
|
166
|
|
|
|
|
|
|
(keys %artists)[0] : 'Various Artists'); |
|
167
|
|
|
|
|
|
|
$template->param(album => (scalar keys %albums == 1)? |
|
168
|
|
|
|
|
|
|
(keys %albums)[0] : 'Compilation'); |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# send the obligatory Content-Type and print the template output |
|
171
|
|
|
|
|
|
|
return $template->output; |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
} # end of sub create_inlay |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=back |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=cut |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# return a "prettified" version of an artist/title - currently just converts |
|
180
|
|
|
|
|
|
|
# to title case, might support more options in future. Of course, this isn't |
|
181
|
|
|
|
|
|
|
# always a good thing, as there are some artist names / song names which are |
|
182
|
|
|
|
|
|
|
# supposed to be in all capitals, or "officially" should be all in lowercase |
|
183
|
|
|
|
|
|
|
# etc, and will choke on double-barreled names "Dave Two-Names" or Scottish |
|
184
|
|
|
|
|
|
|
# names ("Johnny McScottish" or "Old MacDonald"). |
|
185
|
|
|
|
|
|
|
sub _prettify { join ' ', map { ucfirst $_ } split /\s/, lc shift; } |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
our $default_template = <
|
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
- |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
- |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
TEMPLATE |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
1; |
|
231
|
|
|
|
|
|
|
__END__ |