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