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