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