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