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