Work with raw bytes instead of UTF-8
[app-musicexpo.git] / lib / App / MusicExpo.pm
CommitLineData
7eb41a1b 1package App::MusicExpo;
21cd9240 2use v5.14;
75c564d8 3use strict;
21cd9240
MG
4use warnings;
5
a06c3a2b 6our $VERSION = '0.005';
7eb41a1b 7
21cd9240 8use Audio::FLAC::Header qw//;
21cd9240
MG
9use HTML::Template::Compiled qw//;
10use Memoize qw/memoize/;
11use MP3::Tag qw//;
513a3718 12use Ogg::Vorbis::Header::PurePerl;
ddd1adc5 13use MP4::Info qw/get_mp4tag get_mp4info/;
21cd9240
MG
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
75c564d8
MG
23my $default_template;
24
21cd9240 25our $prefix='/music/';
fb015c82 26our $cache='';
75c564d8 27our $template='';
21cd9240
MG
28
29GetOptions (
598a92c5
MG
30 "template:s" => \$template,
31 "prefix:s" => \$prefix,
32 "cache:s" => \$cache,
21cd9240
MG
33);
34
21cd9240 35sub flacinfo{
6a1f7df9
MG
36 my $file=$_[0];
37 my $flac=Audio::FLAC::Header->new($file);
38 $file = scalar fileparse $file;
39
40 freeze +{
41 format => 'FLAC',
5614c544
MG
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') // '?',
6a1f7df9
MG
49 file => $file,
50 }
21cd9240
MG
51}
52
53sub mp3info{
6a1f7df9
MG
54 my $file=$_[0];
55 my $mp3=MP3::Tag->new($file);
56 $file = scalar fileparse $file;
57
58 freeze +{
59 format => 'MP3',
5614c544
MG
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 || '?',
6a1f7df9
MG
67 file => $file,
68 }
21cd9240
MG
69}
70
513a3718 71sub vorbisinfo{
6a1f7df9
MG
72 my $file=$_[0];
73 my $ogg=Ogg::Vorbis::Header::PurePerl->new($file);
74 $file = scalar fileparse $file;
75
76 freeze +{
77 format => 'Vorbis',
5614c544
MG
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') || '?',
6a1f7df9
MG
85 file => $file,
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];
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
3f12a322
MG
115my %info = (
116 '.flac' => \&flacinfo,
117 '.mp3' => \&mp3info,
118 '.ogg' => \&vorbisinfo,
119 '.oga' => \&vorbisinfo,
120 '.mp4' => \&mp4info,
121 '.aac' => \&mp4info,
122 '.m4a' => \&mp4info,
123);
124
21cd9240 125sub normalizer{
6a1f7df9 126 "$_[0]|".(stat $_[0])[9]
21cd9240
MG
127}
128
447902f9
MG
129sub make_fragment{ join '-', map { lc =~ y/a-z0-9/_/csr } @_ }
130
21cd9240 131sub run {
3f12a322 132 my %info = %info;
cc5d06b5
MG
133 if ($cache) {
134 tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644;
3f12a322 135 $info{$_} = memoize $info{$_}, INSTALL => undef, NORMALIZER => \&normalizer, LIST_CACHE => 'FAULT', SCALAR_CACHE => [HASH => \%cache] for keys %info;
cc5d06b5 136 }
6a1f7df9
MG
137
138 my %files;
139 for my $file (@ARGV) {
3f12a322 140 my ($basename, undef, $suffix) = fileparse $file, keys %info;
6a1f7df9 141 $files{$basename} //= [];
3f12a322 142 push @{$files{$basename}}, thaw scalar $info{$suffix}->($file);
6a1f7df9
MG
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 => []);
9d6d04a4 155 for my $ver (@versions) {
c08ff73c
MG
156 push @{$entry{formats}}, {format => $ver->{format}, file => $ver->{file}};
157 for my $key (keys %$ver) {
9d6d04a4
MG
158 $entry{$key} = $ver->{$key} if $ver->{$key} ne '?';
159 }
160 }
161 delete $entry{$_} for qw/format file/;
447902f9 162 $entry{fragment} = make_fragment @entry{qw/artist title/};
6a1f7df9
MG
163 push @files, \%entry
164 }
165
6dcbbf44 166 @files = sort { $a->{title} cmp $b->{title} } @files;
95979388 167 $ht->param(files => \@files, prefix => $prefix);
6a1f7df9 168 print $ht->output;
21cd9240
MG
169}
170
75c564d8
MG
171$default_template = <<'HTML';
172<!DOCTYPE html>
173<title>Music</title>
174<meta charset="utf-8">
de93afc5 175<link rel="stylesheet" href="musicexpo.css">
9b989f96
MG
176<script async defer type="application/javascript" src="player.js"></script>
177
178<div id="player"></div>
75c564d8
MG
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>
447902f9 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>
75c564d8
MG
185</table>
186HTML
187
21cd9240
MG
1881;
189
190__END__
191
bb86a416
MG
192=encoding utf-8
193
21cd9240
MG
194=head1 NAME
195
196App::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
205App::MusicExpo creates a HTML table from a list of songs.
206
207The 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
d5aa2e89
MG
213where the type is a download link. If you have multiple files with the same
214basename (such as C<cellule.flac> and C<cellule.ogg>), they will be treated
215as two versions of the same file, so a row will be created with two download
216links, one for each format.
21cd9240
MG
217
218=head1 OPTIONS
219
220=over
221
222=item B<--template> I<template>
223
75c564d8 224Path to the HTML::Template::Compiled template used for generating the music table. If '' (empty), uses the default format. Is empty by default.
21cd9240
MG
225
226=item B<--prefix> I<prefix>
227
228Prefix for download links. Defaults to '/music/'.
229
230=item B<--cache> I<filename>
231
fb015c82 232Path to the cache file. Created if it does not exist. If '' (empty), disables caching. Is empty by default.
21cd9240
MG
233
234=back
235
236=head1 AUTHOR
237
238Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
239
240=head1 COPYRIGHT AND LICENSE
241
a06c3a2b 242Copyright (C) 2013-2015 by Marius Gavrilescu
21cd9240
MG
243
244This library is free software; you can redistribute it and/or modify
245it under the same terms as Perl itself, either Perl version 5.14.2 or,
246at your option, any later version of Perl 5 you may have available.
This page took 0.028986 seconds and 4 git commands to generate.