Commit | Line | Data |
---|---|---|
7eb41a1b | 1 | package App::MusicExpo; |
a5dd5c0d | 2 | use 5.014000; |
75c564d8 | 3 | use strict; |
21cd9240 MG |
4 | use warnings; |
5 | ||
17c8e5f6 | 6 | our $VERSION = '1.001_001'; |
7eb41a1b | 7 | |
21cd9240 | 8 | use Audio::FLAC::Header qw//; |
21cd9240 MG |
9 | use HTML::Template::Compiled qw//; |
10 | use Memoize qw/memoize/; | |
b8e18bed | 11 | use MP3::Info qw/get_mp3tag/; |
513a3718 | 12 | use Ogg::Vorbis::Header::PurePerl; |
ddd1adc5 | 13 | use MP4::Info qw/get_mp4tag get_mp4info/; |
e2ce550b | 14 | use Audio::Opusfile qw//; |
21cd9240 MG |
15 | |
16 | use DB_File qw//; | |
b8e18bed | 17 | use Encode qw/encode/; |
21cd9240 MG |
18 | use File::Basename qw/fileparse/; |
19 | use Fcntl qw/O_RDWR O_CREAT/; | |
20 | use Getopt::Long; | |
21 | use Storable qw/thaw freeze/; | |
a535e879 | 22 | use sort 'stable'; |
21cd9240 MG |
23 | |
24 | ################################################## | |
25 | ||
75c564d8 MG |
26 | my $default_template; |
27 | ||
21cd9240 | 28 | our $prefix='/music/'; |
fb015c82 | 29 | our $cache=''; |
75c564d8 | 30 | our $template=''; |
21cd9240 MG |
31 | |
32 | GetOptions ( | |
a5dd5c0d MG |
33 | 'template:s' => \$template, |
34 | 'prefix:s' => \$prefix, | |
35 | 'cache:s' => \$cache, | |
21cd9240 MG |
36 | ); |
37 | ||
21cd9240 | 38 | sub flacinfo{ |
6a1f7df9 MG |
39 | my $file=$_[0]; |
40 | my $flac=Audio::FLAC::Header->new($file); | |
6a1f7df9 MG |
41 | |
42 | freeze +{ | |
43 | format => 'FLAC', | |
b484a129 MG |
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'), | |
b8e18bed | 51 | file => scalar fileparse $file, |
6a1f7df9 | 52 | } |
21cd9240 MG |
53 | } |
54 | ||
55 | sub mp3info{ | |
6a1f7df9 | 56 | my $file=$_[0]; |
b8e18bed | 57 | my %tag = map { encode 'UTF-8', $_ } %{get_mp3tag $file}; |
a5dd5c0d | 58 | my @trkn = split m#/#s, $tag{TRACKNUM} // ''; |
6a1f7df9 MG |
59 | |
60 | freeze +{ | |
61 | format => 'MP3', | |
b484a129 MG |
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}, | |
b8e18bed | 69 | file => scalar fileparse $file, |
6a1f7df9 | 70 | } |
21cd9240 MG |
71 | } |
72 | ||
513a3718 | 73 | sub vorbisinfo{ |
6a1f7df9 MG |
74 | my $file=$_[0]; |
75 | my $ogg=Ogg::Vorbis::Header::PurePerl->new($file); | |
6a1f7df9 MG |
76 | |
77 | freeze +{ | |
78 | format => 'Vorbis', | |
fbeeac22 MG |
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'), | |
b8e18bed | 86 | file => scalar fileparse $file, |
6a1f7df9 | 87 | } |
513a3718 MG |
88 | } |
89 | ||
a5dd5c0d | 90 | sub mp4_format ($){ ## no critic (ProhibitSubroutinePrototypes) |
ddd1adc5 MG |
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]; | |
b8e18bed | 99 | my %tag = map { ref() ? $_ : encode 'UTF-8', $_ } %{get_mp4tag $file}; |
ddd1adc5 | 100 | my %info = %{get_mp4info $file}; |
ddd1adc5 MG |
101 | |
102 | freeze +{ | |
103 | format => mp4_format $info{ENCODING}, | |
b484a129 MG |
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}, | |
b8e18bed | 111 | file => scalar fileparse $file, |
ddd1adc5 MG |
112 | }; |
113 | } | |
114 | ||
8c158a92 MG |
115 | sub opusinfo { |
116 | my $file = $_[0]; | |
e2ce550b MG |
117 | my $of = Audio::Opusfile->new_from_file($file); |
118 | my $tags = $of->tags; | |
8c158a92 MG |
119 | |
120 | my %data = ( | |
121 | format => 'Opus', | |
e2ce550b MG |
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'), | |
8c158a92 MG |
129 | file => scalar fileparse $file |
130 | ); | |
131 | ||
132 | freeze \%data; | |
133 | } | |
134 | ||
3f12a322 MG |
135 | my %info = ( |
136 | '.flac' => \&flacinfo, | |
137 | '.mp3' => \&mp3info, | |
138 | '.ogg' => \&vorbisinfo, | |
139 | '.oga' => \&vorbisinfo, | |
140 | '.mp4' => \&mp4info, | |
141 | '.aac' => \&mp4info, | |
142 | '.m4a' => \&mp4info, | |
8c158a92 | 143 | '.opus' => \&opusinfo, |
3f12a322 MG |
144 | ); |
145 | ||
21cd9240 | 146 | sub normalizer{ |
6a1f7df9 | 147 | "$_[0]|".(stat $_[0])[9] |
21cd9240 MG |
148 | } |
149 | ||
447902f9 MG |
150 | sub make_fragment{ join '-', map { lc =~ y/a-z0-9/_/csr } @_ } |
151 | ||
21cd9240 | 152 | sub run { |
cc5d06b5 | 153 | if ($cache) { |
a5dd5c0d | 154 | tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644; ## no critic (ProhibitTie) |
3f12a322 | 155 | $info{$_} = memoize $info{$_}, INSTALL => undef, NORMALIZER => \&normalizer, LIST_CACHE => 'FAULT', SCALAR_CACHE => [HASH => \%cache] for keys %info; |
cc5d06b5 | 156 | } |
6a1f7df9 MG |
157 | |
158 | my %files; | |
159 | for my $file (@ARGV) { | |
3f12a322 | 160 | my ($basename, undef, $suffix) = fileparse $file, keys %info; |
6a1f7df9 | 161 | $files{$basename} //= []; |
3f12a322 | 162 | push @{$files{$basename}}, thaw scalar $info{$suffix}->($file); |
6a1f7df9 MG |
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; | |
a535e879 MG |
172 | for (sort keys %files) { |
173 | my @versions = @{$files{$_}}; | |
b484a129 | 174 | my %entry = (formats => [], map { $_ => '?' } qw/title artist year album tracknumber tracktotal genre/); |
9d6d04a4 | 175 | for my $ver (@versions) { |
c08ff73c MG |
176 | push @{$entry{formats}}, {format => $ver->{format}, file => $ver->{file}}; |
177 | for my $key (keys %$ver) { | |
a71c6c1e | 178 | $entry{$key} = $ver->{$key} if $ver->{$key} && $ver->{$key} ne '?'; |
9d6d04a4 MG |
179 | } |
180 | } | |
181 | delete $entry{$_} for qw/format file/; | |
447902f9 | 182 | $entry{fragment} = make_fragment @entry{qw/artist title/}; |
6a1f7df9 MG |
183 | push @files, \%entry |
184 | } | |
185 | ||
6dcbbf44 | 186 | @files = sort { $a->{title} cmp $b->{title} } @files; |
95979388 | 187 | $ht->param(files => \@files, prefix => $prefix); |
a5dd5c0d | 188 | print $ht->output; ## no critic (RequireCheckedSyscalls) |
21cd9240 MG |
189 | } |
190 | ||
75c564d8 MG |
191 | $default_template = <<'HTML'; |
192 | <!DOCTYPE html> | |
193 | <title>Music</title> | |
194 | <meta charset="utf-8"> | |
de93afc5 | 195 | <link rel="stylesheet" href="musicexpo.css"> |
9b989f96 MG |
196 | <script async defer type="application/javascript" src="player.js"></script> |
197 | ||
198 | <div id="player"></div> | |
75c564d8 MG |
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> | |
447902f9 | 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> |
75c564d8 MG |
205 | </table> |
206 | HTML | |
207 | ||
21cd9240 MG |
208 | 1; |
209 | ||
210 | __END__ | |
211 | ||
bb86a416 MG |
212 | =encoding utf-8 |
213 | ||
21cd9240 MG |
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 | ||
d5aa2e89 MG |
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. | |
21cd9240 MG |
237 | |
238 | =head1 OPTIONS | |
239 | ||
240 | =over | |
241 | ||
242 | =item B<--template> I<template> | |
243 | ||
75c564d8 | 244 | 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 |
245 | |
246 | =item B<--prefix> I<prefix> | |
247 | ||
248 | Prefix for download links. Defaults to '/music/'. | |
249 | ||
250 | =item B<--cache> I<filename> | |
251 | ||
fb015c82 | 252 | Path to the cache file. Created if it does not exist. If '' (empty), disables caching. Is empty by default. |
21cd9240 MG |
253 | |
254 | =back | |
255 | ||
256 | =head1 AUTHOR | |
257 | ||
258 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> | |
259 | ||
260 | =head1 COPYRIGHT AND LICENSE | |
261 | ||
60376c0c | 262 | Copyright (C) 2013-2016 by Marius Gavrilescu |
21cd9240 MG |
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. |