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