]>
Commit | Line | Data |
---|---|---|
1 | package App::MusicExpo; | |
2 | use 5.014000; | |
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | our $VERSION = '1.001_001'; | |
7 | ||
8 | use Audio::FLAC::Header qw//; | |
9 | use HTML::Template::Compiled qw//; | |
10 | use Memoize qw/memoize/; | |
11 | use MP3::Info qw/get_mp3tag/; | |
12 | use Ogg::Vorbis::Header::PurePerl; | |
13 | use MP4::Info qw/get_mp4tag get_mp4info/; | |
14 | use Audio::Opusfile qw//; | |
15 | ||
16 | use DB_File qw//; | |
17 | use Encode qw/encode/; | |
18 | use File::Basename qw/fileparse/; | |
19 | use Fcntl qw/O_RDWR O_CREAT/; | |
20 | use Getopt::Long; | |
21 | use Storable qw/thaw freeze/; | |
22 | use sort 'stable'; | |
23 | ||
24 | ################################################## | |
25 | ||
26 | my $default_template; | |
27 | ||
28 | our $prefix='/music/'; | |
29 | our $cache=''; | |
30 | our $template=''; | |
31 | ||
32 | GetOptions ( | |
33 | 'template:s' => \$template, | |
34 | 'prefix:s' => \$prefix, | |
35 | 'cache:s' => \$cache, | |
36 | ); | |
37 | ||
38 | sub flacinfo{ | |
39 | my $file=$_[0]; | |
40 | my $flac=Audio::FLAC::Header->new($file); | |
41 | ||
42 | freeze +{ | |
43 | format => 'FLAC', | |
44 | title => $flac->tags('TITLE'), | |
45 | artist => $flac->tags('ARTIST'), | |
46 | year => $flac->tags('DATE'), | |
47 | album => $flac->tags('ALBUM'), | |
48 | tracknumber => $flac->tags('TRACKNUMBER'), | |
49 | tracktotal => $flac->tags('TRACKTOTAL'), | |
50 | genre => $flac->tags('GENRE'), | |
51 | file => scalar fileparse $file, | |
52 | } | |
53 | } | |
54 | ||
55 | sub mp3info{ | |
56 | my $file=$_[0]; | |
57 | my %tag = map { encode 'UTF-8', $_ } %{get_mp3tag $file}; | |
58 | my @trkn = split m#/#s, $tag{TRACKNUM} // ''; | |
59 | ||
60 | freeze +{ | |
61 | format => 'MP3', | |
62 | title => $tag{TITLE}, | |
63 | artist => $tag{ARTIST}, | |
64 | year => $tag{YEAR}, | |
65 | album => $tag{ALBUM}, | |
66 | tracknumber => $trkn[0], | |
67 | tracktotal => $trkn[1], | |
68 | genre => $tag{GENRE}, | |
69 | file => scalar fileparse $file, | |
70 | } | |
71 | } | |
72 | ||
73 | sub vorbisinfo{ | |
74 | my $file=$_[0]; | |
75 | my $ogg=Ogg::Vorbis::Header::PurePerl->new($file); | |
76 | ||
77 | freeze +{ | |
78 | format => 'Vorbis', | |
79 | title => scalar $ogg->comment('TITLE'), | |
80 | artist => scalar $ogg->comment('artist'), | |
81 | year => scalar $ogg->comment('DATE'), | |
82 | album => scalar $ogg->comment('ALBUM'), | |
83 | tracknumber => scalar $ogg->comment('TRACKNUMBER'), | |
84 | tracktotal => scalar $ogg->comment('TRACKTOTAL'), | |
85 | genre => scalar $ogg->comment('GENRE'), | |
86 | file => scalar fileparse $file, | |
87 | } | |
88 | } | |
89 | ||
90 | sub mp4_format ($){ ## no critic (ProhibitSubroutinePrototypes) | |
91 | my $encoding = $_[0]; | |
92 | return 'AAC' if $encoding eq 'mp4a'; | |
93 | return 'ALAC' if $encoding eq 'alac'; | |
94 | "MP4-$encoding" | |
95 | } | |
96 | ||
97 | sub mp4info{ | |
98 | my $file=$_[0]; | |
99 | my %tag = map { ref() ? $_ : encode 'UTF-8', $_ } %{get_mp4tag $file}; | |
100 | my %info = %{get_mp4info $file}; | |
101 | ||
102 | freeze +{ | |
103 | format => mp4_format $info{ENCODING}, | |
104 | title => $tag{TITLE}, | |
105 | artist => $tag{ARTIST}, | |
106 | year => $tag{YEAR}, | |
107 | album => $tag{ALBUM}, | |
108 | tracknumber => $tag{TRACKNUM}, | |
109 | tracktotal => ($tag{TRKN} ? $tag{TRKN}->[1] : undef), | |
110 | genre => $tag{GENRE}, | |
111 | file => scalar fileparse $file, | |
112 | }; | |
113 | } | |
114 | ||
115 | sub opusinfo { | |
116 | my $file = $_[0]; | |
117 | my $of = Audio::Opusfile->new_from_file($file); | |
118 | my $tags = $of->tags; | |
119 | ||
120 | my %data = ( | |
121 | format => 'Opus', | |
122 | title => $tags->query('TITLE'), | |
123 | artist => $tags->query('ARTIST'), | |
124 | year => $tags->query('DATE'), | |
125 | album => $tags->query('ALBUM'), | |
126 | tracknumber => $tags->query('TRACKNUMBER'), | |
127 | tracktotal => $tags->query('TRACKTOTAL'), | |
128 | genre => $tags->query('GENRE'), | |
129 | file => scalar fileparse $file | |
130 | ); | |
131 | ||
132 | freeze \%data; | |
133 | } | |
134 | ||
135 | my %info = ( | |
136 | '.flac' => \&flacinfo, | |
137 | '.mp3' => \&mp3info, | |
138 | '.ogg' => \&vorbisinfo, | |
139 | '.oga' => \&vorbisinfo, | |
140 | '.mp4' => \&mp4info, | |
141 | '.aac' => \&mp4info, | |
142 | '.m4a' => \&mp4info, | |
143 | '.opus' => \&opusinfo, | |
144 | ); | |
145 | ||
146 | sub normalizer{ | |
147 | "$_[0]|".(stat $_[0])[9] | |
148 | } | |
149 | ||
150 | sub make_fragment{ join '-', map { lc =~ y/a-z0-9/_/csr } @_ } | |
151 | ||
152 | sub run { | |
153 | if ($cache) { | |
154 | tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644; ## no critic (ProhibitTie) | |
155 | $info{$_} = memoize $info{$_}, INSTALL => undef, NORMALIZER => \&normalizer, LIST_CACHE => 'FAULT', SCALAR_CACHE => [HASH => \%cache] for keys %info; | |
156 | } | |
157 | ||
158 | my %files; | |
159 | for my $file (@ARGV) { | |
160 | my ($basename, undef, $suffix) = fileparse $file, keys %info; | |
161 | $files{$basename} //= []; | |
162 | push @{$files{$basename}}, thaw scalar $info{$suffix}->($file); | |
163 | } | |
164 | ||
165 | my $ht=HTML::Template::Compiled->new( | |
166 | default_escape => 'HTML', | |
167 | global_vars => 2, | |
168 | $template eq '' ? (scalarref => \$default_template) : (filename => $template), | |
169 | ); | |
170 | ||
171 | my @files; | |
172 | for (sort keys %files) { | |
173 | my @versions = @{$files{$_}}; | |
174 | my %entry = (formats => [], map { $_ => '?' } qw/title artist year album tracknumber tracktotal genre/); | |
175 | for my $ver (@versions) { | |
176 | push @{$entry{formats}}, {format => $ver->{format}, file => $ver->{file}}; | |
177 | for my $key (keys %$ver) { | |
178 | $entry{$key} = $ver->{$key} if $ver->{$key} && $ver->{$key} ne '?'; | |
179 | } | |
180 | } | |
181 | delete $entry{$_} for qw/format file/; | |
182 | $entry{fragment} = make_fragment @entry{qw/artist title/}; | |
183 | push @files, \%entry | |
184 | } | |
185 | ||
186 | @files = sort { $a->{title} cmp $b->{title} } @files; | |
187 | $ht->param(files => \@files, prefix => $prefix); | |
188 | print $ht->output; ## no critic (RequireCheckedSyscalls) | |
189 | } | |
190 | ||
191 | $default_template = <<'HTML'; | |
192 | <!DOCTYPE html> | |
193 | <title>Music</title> | |
194 | <meta charset="utf-8"> | |
195 | <link rel="stylesheet" href="musicexpo.css"> | |
196 | <script async defer type="application/javascript" src="player.js"></script> | |
197 | ||
198 | <div id="player"></div> | |
199 | ||
200 | <table border> | |
201 | <thead> | |
202 | <tr><th>Title<th>Artist<th>Album<th>Genre<th>Track<th>Year<th>Type | |
203 | <tbody><tmpl_loop files> | |
204 | <tr><td class="title"><a href="#<tmpl_var fragment>" data-hash="#<tmpl_var fragment>"><tmpl_var title></a><td class="artist"><tmpl_var artist><td class="album"><tmpl_var album><td class="genre"><tmpl_var genre><td class="track"><tmpl_var tracknumber>/<tmpl_var tracktotal><td class="year"><tmpl_var year><td class="formats"><tmpl_loop formats><a href="<tmpl_var ...prefix><tmpl_var ESCAPE=URL file>"><tmpl_var format></a> </tmpl_loop></tmpl_loop> | |
205 | </table> | |
206 | HTML | |
207 | ||
208 | 1; | |
209 | ||
210 | __END__ | |
211 | ||
212 | =encoding utf-8 | |
213 | ||
214 | =head1 NAME | |
215 | ||
216 | App::MusicExpo - script which generates a HTML table of music tags | |
217 | ||
218 | =head1 SYNOPSIS | |
219 | ||
220 | use App::MusicExpo; | |
221 | App::MusicExpo->run; | |
222 | ||
223 | =head1 DESCRIPTION | |
224 | ||
225 | App::MusicExpo creates a HTML table from a list of songs. | |
226 | ||
227 | The default template looks like: | |
228 | ||
229 | | Title | Artist | Album | Genre | Track | Year | Type | | |
230 | |---------+---------+-----------------+---------+-------+------+------| | |
231 | | Cellule | Silence | L'autre endroit | Electro | 01/09 | 2005 | FLAC | | |
232 | ||
233 | where the type is a download link. If you have multiple files with the same | |
234 | basename (such as C<cellule.flac> and C<cellule.ogg>), they will be treated | |
235 | as two versions of the same file, so a row will be created with two download | |
236 | links, one for each format. | |
237 | ||
238 | =head1 OPTIONS | |
239 | ||
240 | =over | |
241 | ||
242 | =item B<--template> I<template> | |
243 | ||
244 | Path to the HTML::Template::Compiled template used for generating the music table. If '' (empty), uses the default format. Is empty by default. | |
245 | ||
246 | =item B<--prefix> I<prefix> | |
247 | ||
248 | Prefix for download links. Defaults to '/music/'. | |
249 | ||
250 | =item B<--cache> I<filename> | |
251 | ||
252 | Path to the cache file. Created if it does not exist. If '' (empty), disables caching. Is empty by default. | |
253 | ||
254 | =back | |
255 | ||
256 | =head1 AUTHOR | |
257 | ||
258 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> | |
259 | ||
260 | =head1 COPYRIGHT AND LICENSE | |
261 | ||
262 | Copyright (C) 2013-2016 by Marius Gavrilescu | |
263 | ||
264 | This library is free software; you can redistribute it and/or modify | |
265 | it under the same terms as Perl itself, either Perl version 5.14.2 or, | |
266 | at your option, any later version of Perl 5 you may have available. |