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