| 1 | package MP3::CreateInlayCard; | |||||||||||||||||||||||||||||||
| 2 | ||||||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '0.06'; | ||||||||||||||||||||||||||||||
| 4 | ||||||||||||||||||||||||||||||||
| 5 | # $Id: CreateInlayCard.pm 535 2009-02-13 23:26:32Z davidp $ | |||||||||||||||||||||||||||||||
| 6 | ||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | |||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | |||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use File::Find::Rule; | |||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Music::Tag; | |||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Cwd; | |||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use HTML::Template; | |||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||
| 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<dir> | |||||||||||||||||||||||||||||||
| 69 | ||||||||||||||||||||||||||||||||
| 70 | The directory containing the MP3 files | |||||||||||||||||||||||||||||||
| 71 | ||||||||||||||||||||||||||||||||
| 72 | =item I<template> | |||||||||||||||||||||||||||||||
| 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 | 8 | ▶ | sub create_inlay { | |||||||||||||||||||||||||||||
|
Subroutine
create_inlay: called CC=16 SCAR=33.2
Pod
documented
| ||||||||||||||||||||||||||||||||
| 84 | ||||||||||||||||||||||||||||||||
| 85 | 8 | my $params = shift; | ||||||||||||||||||||||||||||||
| 86 | 8 | ▶ | if (!$params || ref $params ne 'HASH') { | |||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $params or ref $params ne "HASH"
MC/DC0%0/2
not $params or ref $params ne "HASH"
not $params
ref $params ne "HASH"
Truth table33%1/3
not(A) or B
A= not $paramsB= ref $params ne "HASH"
| ||||||||||||||||||||||||||||||||
| 87 | 0 | die "create_inlay() not passed hashref of params"; | ||||||||||||||||||||||||||||||
| 88 | } | |||||||||||||||||||||||||||||||
| 89 | 8 | ▶ | my $startdir = $params->{dir} || getcwd; | |||||||||||||||||||||||||||||
|
Condition33%1/3
$params->{'dir'} || getcwd()
MC/DC0%0/2
$params->{'dir'} || getcwd()
$params->{'dir'}
getcwd()
Truth table33%1/3
A || B
A= $params->{'dir'}B= getcwd()
| ||||||||||||||||||||||||||||||||
| 90 | ||||||||||||||||||||||||||||||||
| 91 | 8 | ▶ | if (!-d $startdir) { | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 92 | 0 | die "$startdir is not a directory"; | ||||||||||||||||||||||||||||||
| 93 | } | |||||||||||||||||||||||||||||||
| 94 | ||||||||||||||||||||||||||||||||
| 95 | ||||||||||||||||||||||||||||||||
| 96 | 8 | my @files = File::Find::Rule->file()->name('*.mp3')->in($startdir); | ||||||||||||||||||||||||||||||
| 97 | ||||||||||||||||||||||||||||||||
| 98 | 8 | my $track = 1; | ||||||||||||||||||||||||||||||
| 99 | 8 | 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 | 8 | my %albums; | ||||||||||||||||||||||||||||||
| 106 | ||||||||||||||||||||||||||||||||
| 107 | 8 | for my $file (@files) { | ||||||||||||||||||||||||||||||
| 108 | ||||||||||||||||||||||||||||||||
| 109 | 24 | my $tags = Music::Tag->new($file, { quiet => 1 }); | ||||||||||||||||||||||||||||||
| 110 | 24 | ▶ | $tags->get_tag or warn "Error reading tags from $file" and next; | |||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
$tags->get_tag || warn("Error reading tags from $file") && (next)
warn "Error reading tags from $file" and next
MC/DC0%0/2
$tags->get_tag || warn("Error reading tags from $file") && (next)
$tags->get_tag
warn "Error reading tags from $file"
Truth table33%1/3
A || B
A= $tags->get_tagB= warn "Error reading tags from $file"
| ||||||||||||||||||||||||||||||||
| 111 | ||||||||||||||||||||||||||||||||
| 112 | 24 | 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 | 24 | ▶ | _prettify($tags->artist()) : $tags->artist(), | |||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||
| 121 | length => $length, | |||||||||||||||||||||||||||||||
| 122 | }; | |||||||||||||||||||||||||||||||
| 123 | ||||||||||||||||||||||||||||||||
| 124 | 24 | $artists{ $tags->artist() }++; | ||||||||||||||||||||||||||||||
| 125 | 24 | $albums{ $tags->album() }++; | ||||||||||||||||||||||||||||||
| 126 | 24 | $track++; | ||||||||||||||||||||||||||||||
| 127 | } | |||||||||||||||||||||||||||||||
| 128 | ||||||||||||||||||||||||||||||||
| 129 | # open the html template (this code is rather crufty, refactor someday) | |||||||||||||||||||||||||||||||
| 130 | 8 | my $template; | ||||||||||||||||||||||||||||||
| 131 | 8 | my $template_opts = { die_on_bad_params => 0 }; | ||||||||||||||||||||||||||||||
| 132 | ||||||||||||||||||||||||||||||||
| 133 | 8 | ▶ | if ($params->{template}) { | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 134 | 8 | ▶ | if (ref $params->{template} eq 'SCALAR') { | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 135 | # we've been given the template content: | |||||||||||||||||||||||||||||||
| 136 | $template = HTML::Template->new( | |||||||||||||||||||||||||||||||
| 137 | scalarref => $params->{template}, | |||||||||||||||||||||||||||||||
| 138 | 0 | die_on_bad_params => 0 | ||||||||||||||||||||||||||||||
| 139 | ); | |||||||||||||||||||||||||||||||
| 140 | } else { | |||||||||||||||||||||||||||||||
| 141 | # it must be the filename of a template to use: | |||||||||||||||||||||||||||||||
| 142 | 8 | ▶ | if (-e $params->{template}) { | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 143 | $template = HTML::Template->new( | |||||||||||||||||||||||||||||||
| 144 | filename => $params->{template}, | |||||||||||||||||||||||||||||||
| 145 | 8 | die_on_bad_params => 0 | ||||||||||||||||||||||||||||||
| 146 | ); | |||||||||||||||||||||||||||||||
| 147 | } else { | |||||||||||||||||||||||||||||||
| 148 | 0 | die "Failed to open $params->{template} $!"; | ||||||||||||||||||||||||||||||
| 149 | } | |||||||||||||||||||||||||||||||
| 150 | } | |||||||||||||||||||||||||||||||
| 151 | } else { | |||||||||||||||||||||||||||||||
| 152 | # no template supplied, so use our default built-in one: | |||||||||||||||||||||||||||||||
| 153 | 0 | $template = HTML::Template->new( | ||||||||||||||||||||||||||||||
| 154 | scalarref => \$MP3::CreateInlayCard::default_template, | |||||||||||||||||||||||||||||||
| 155 | die_on_bad_params => 0 | |||||||||||||||||||||||||||||||
| 156 | ); | |||||||||||||||||||||||||||||||
| 157 | } | |||||||||||||||||||||||||||||||
| 158 | ||||||||||||||||||||||||||||||||
| 159 | 8 | ▶ | if (!$template) { | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 160 | 0 | die "Uh-oh.. failed to create HTML::Template"; | ||||||||||||||||||||||||||||||
| 161 | } | |||||||||||||||||||||||||||||||
| 162 | ||||||||||||||||||||||||||||||||
| 163 | # fill in some parameters | |||||||||||||||||||||||||||||||
| 164 | 0 | $template->param(tracks => sort { $a->{track} cmp $b->{track} } \@tracks); | ||||||||||||||||||||||||||||||
| 165 | 8 | ▶ | $template->param(artist => (scalar keys %artists == 1)? | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 166 | (keys %artists)[0] : 'Various Artists'); | |||||||||||||||||||||||||||||||
| 167 | 8 | ▶ | $template->param(album => (scalar keys %albums == 1)? | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 168 | (keys %albums)[0] : 'Compilation'); | |||||||||||||||||||||||||||||||
| 169 | ||||||||||||||||||||||||||||||||
| 170 | # send the obligatory Content-Type and print the template output | |||||||||||||||||||||||||||||||
| 171 | 8 | 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 | 0 | ▶ | sub _prettify { join ' ', map { ucfirst $_ } split /\s/, lc shift; } | |||||||||||||||||||||||||||||
|
Subroutine
_prettify: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||
| 186 | ||||||||||||||||||||||||||||||||
| 187 | ||||||||||||||||||||||||||||||||
| 188 | 2 | our $default_template = <<TEMPLATE; | ||||||||||||||||||||||||||||||
| 189 | <!-- Created by MP3::CreateInlayCard $VERSION (using default template) --> | |||||||||||||||||||||||||||||||
| 190 | <html> | |||||||||||||||||||||||||||||||
| 191 | <head> | |||||||||||||||||||||||||||||||
| 192 | <style> | |||||||||||||||||||||||||||||||
| 193 | BODY, P, TD { | |||||||||||||||||||||||||||||||
| 194 | font-family: Verdana, Helvetica, Arial, sans-serif; font-size: 10px; | |||||||||||||||||||||||||||||||
| 195 | } | |||||||||||||||||||||||||||||||
| 196 | ||||||||||||||||||||||||||||||||
| 197 | #holder { | |||||||||||||||||||||||||||||||
| 198 | position: absolute; width: 12cm; height: 12cm; | |||||||||||||||||||||||||||||||
| 199 | border: 1px dashed gray; | |||||||||||||||||||||||||||||||
| 200 | } | |||||||||||||||||||||||||||||||
| 201 | ||||||||||||||||||||||||||||||||
| 202 | H1 { | |||||||||||||||||||||||||||||||
| 203 | font-size: 16px; font-weight: bold; | |||||||||||||||||||||||||||||||
| 204 | } | |||||||||||||||||||||||||||||||
| 205 | </style> | |||||||||||||||||||||||||||||||
| 206 | <title><tmpl_var name="artist"> - <tmpl_var name="album"></title> | |||||||||||||||||||||||||||||||
| 207 | </head> | |||||||||||||||||||||||||||||||
| 208 | <body> | |||||||||||||||||||||||||||||||
| 209 | <div id="holder"> | |||||||||||||||||||||||||||||||
| 210 | ||||||||||||||||||||||||||||||||
| 211 | <h1><tmpl_var name="artist"> - <tmpl_var name="album"></h1> | |||||||||||||||||||||||||||||||
| 212 | ||||||||||||||||||||||||||||||||
| 213 | <table width="100%" cellspacing="2"> | |||||||||||||||||||||||||||||||
| 214 | ||||||||||||||||||||||||||||||||
| 215 | <tmpl_loop name="tracks"> | |||||||||||||||||||||||||||||||
| 216 | <tr> | |||||||||||||||||||||||||||||||
| 217 | <td><tmpl_var name="track"></td> | |||||||||||||||||||||||||||||||
| 218 | <td><tmpl_var name="title"></td> | |||||||||||||||||||||||||||||||
| 219 | <td><tmpl_var name="artist"></td> | |||||||||||||||||||||||||||||||
| 220 | <td><tmpl_var name="length"></td> | |||||||||||||||||||||||||||||||
| 221 | </tr> | |||||||||||||||||||||||||||||||
| 222 | </tmpl_loop> | |||||||||||||||||||||||||||||||
| 223 | ||||||||||||||||||||||||||||||||
| 224 | </table> | |||||||||||||||||||||||||||||||
| 225 | ||||||||||||||||||||||||||||||||
| 226 | </body> | |||||||||||||||||||||||||||||||
| 227 | </html> | |||||||||||||||||||||||||||||||
| 228 | TEMPLATE | |||||||||||||||||||||||||||||||
| 229 | ||||||||||||||||||||||||||||||||
| 230 | 2 | 1; | ||||||||||||||||||||||||||||||
| 231 | __END__ | |||||||||||||||||||||||||||||||