Bump version and update Changes
[app-musicexpo.git] / lib / App / MusicExpo.pm
... / ...
CommitLineData
1package App::MusicExpo;
2use v5.14;
3use strict;
4use warnings;
5
6our $VERSION = '0.005';
7
8use Audio::FLAC::Header qw//;
9use HTML::Template::Compiled qw//;
10use Memoize qw/memoize/;
11use MP3::Tag qw//;
12use Ogg::Vorbis::Header::PurePerl;
13use MP4::Info qw/get_mp4tag get_mp4info/;
14
15use DB_File qw//;
16use File::Basename qw/fileparse/;
17use Fcntl qw/O_RDWR O_CREAT/;
18use Getopt::Long;
19use Storable qw/thaw freeze/;
20
21##################################################
22
23my $default_template;
24
25our $prefix='/music/';
26our $cache='';
27our $template='';
28
29GetOptions (
30 "template:s" => \$template,
31 "prefix:s" => \$prefix,
32 "cache:s" => \$cache,
33);
34
35
36sub fix{
37 my $copy = $_[0];
38 utf8::decode($copy);
39 $copy
40}
41
42sub flacinfo{
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 }
58}
59
60sub mp3info{
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 }
76}
77
78sub vorbisinfo{
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 }
94}
95
96sub 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
103sub 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
122sub normalizer{
123 "$_[0]|".(stat $_[0])[9]
124}
125
126sub run {
127 if ($cache) {
128 tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644;
129 memoize $_, NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] for qw/flacinfo mp3info vorbisinfo mp4info/;
130 }
131
132 my %files;
133 for my $file (@ARGV) {
134 my $info;
135 $info = thaw flacinfo $file if $file =~ /\.flac$/i;
136 $info = thaw mp3info $file if $file =~ /\.mp3$/i;
137 $info = thaw vorbisinfo $file if $file =~ /\.og(?:g|a)$/i;
138 $info = thaw mp4info $file if $file =~ /\.mp4|\.aac|\.m4a$/i;
139 next unless defined $info;
140 my $basename = fileparse $file, '.flac', '.mp3', '.ogg', '.oga', '.mp4', '.aac', '.m4a';
141 $files{$basename} //= [];
142 push @{$files{$basename}}, $info;
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 push @files, \%entry
163 }
164
165 @files = sort { $a->{title} cmp $b->{title} } @files;
166 $ht->param(files => \@files, prefix => $prefix);
167 print $ht->output;
168}
169
170$default_template = <<'HTML';
171<!DOCTYPE html>
172<title>Music</title>
173<meta charset="utf-8">
174<link rel="stylesheet" href="musicexpo.css">
175<script async defer type="application/javascript" src="player.js"></script>
176
177<div id="player"></div>
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>
183<tr><td class="title"><tmpl_var title><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>
184</table>
185HTML
186
1871;
188
189__END__
190
191=encoding utf-8
192
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
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.
216
217=head1 OPTIONS
218
219=over
220
221=item B<--template> I<template>
222
223Path to the HTML::Template::Compiled template used for generating the music table. If '' (empty), uses the default format. Is empty by default.
224
225=item B<--prefix> I<prefix>
226
227Prefix for download links. Defaults to '/music/'.
228
229=item B<--cache> I<filename>
230
231Path to the cache file. Created if it does not exist. If '' (empty), disables caching. Is empty by default.
232
233=back
234
235=head1 AUTHOR
236
237Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
238
239=head1 COPYRIGHT AND LICENSE
240
241Copyright (C) 2013-2015 by Marius Gavrilescu
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.010535 seconds and 4 git commands to generate.