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