Add OGG Vorbis support
[app-musicexpo.git] / lib / App / MusicExpo.pm
CommitLineData
c2a060a4 1package App::MusicExpo 0.002;
21cd9240 2use v5.14;
75c564d8 3use strict;
21cd9240
MG
4use warnings;
5
6use Audio::FLAC::Header qw//;
21cd9240
MG
7use HTML::Template::Compiled qw//;
8use Memoize qw/memoize/;
9use MP3::Tag qw//;
513a3718 10use Ogg::Vorbis::Header::PurePerl;
21cd9240
MG
11
12use DB_File qw//;
13use File::Basename qw/fileparse/;
14use Fcntl qw/O_RDWR O_CREAT/;
15use Getopt::Long;
16use Storable qw/thaw freeze/;
17
18##################################################
19
75c564d8
MG
20my $default_template;
21
21cd9240 22our $prefix='/music/';
fb015c82 23our $cache='';
75c564d8 24our $template='';
21cd9240
MG
25
26GetOptions (
27 "template=s" => \$template,
28 "prefix=s" => \$prefix,
29 "cache=s" => \$cache,
21cd9240
MG
30);
31
32
33sub fix{
34 utf8::decode($_[0]);
d718c66c 35 $_[0]
21cd9240
MG
36}
37
38sub flacinfo{
39 my $file=$_[0];
40 my $flac=Audio::FLAC::Header->new($file);
e0fa3070 41 $file = $prefix . scalar fileparse $file;
21cd9240
MG
42
43 freeze +{
44 format => 'FLAC',
45 title => fix ($flac->tags('TITLE') // '?'),
46 artist => fix ($flac->tags('ARTIST') // '?'),
47 year => fix ($flac->tags('DATE') // '?'),
48 album => fix ($flac->tags('ALBUM') // '?'),
49 tracknumber => fix ($flac->tags('TRACKNUMBER') // '?'),
50 tracktotal => fix ($flac->tags('TRACKTOTAL') // '?'),
51 genre => fix ($flac->tags('GENRE') // '?'),
52 path => $file,
53 }
54}
55
56sub mp3info{
57 my $file=$_[0];
58 my $mp3=MP3::Tag->new($file);
e0fa3070 59 $file = $prefix . scalar fileparse $file;
21cd9240
MG
60
61 freeze +{
62 format => 'MP3',
63 title => fix ($mp3->title || '?'),
64 artist => fix ($mp3->artist || '?'),
65 year => fix ($mp3->year || '?'),
66 album => fix ($mp3->album || '?'),
67 tracknumber => fix ($mp3->track1 || '?'),
68 tracktotal => fix ($mp3->track2 || '?'),
69 genre => fix ($mp3->genre) || '?',
70 path => $file,
71 }
72}
73
513a3718
MG
74sub vorbisinfo{
75 my $file=$_[0];
76 my $ogg=Ogg::Vorbis::Header::PurePerl->new($file);
77
78 freeze +{
79 format => 'Vorbis',
80 title => fix($ogg->comment('TITLE') || '?'),
81 artist => fix ($ogg->comment('artist') || '?'),
82 year => fix ($ogg->comment('DATE') || '?'),
83 album => fix ($ogg->comment('ALBUM') || '?'),
84 tracknumber => fix ($ogg->comment('TRACKNUMBER') || '?'),
85 tracktotal => fix ($ogg->comment('TRACKTOTAL') || '?'),
86 genre => fix ($ogg->comment('GENRE')) || '?',
87 path => $file,
88 }
89}
90
21cd9240
MG
91sub normalizer{
92 "$_[0]|".(stat $_[0])[9]
93}
94
95sub run {
fb015c82
MG
96 tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644 unless $cache eq '';
97 memoize 'flacinfo', NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] unless $cache eq '';
98 memoize 'mp3info' , NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] unless $cache eq '';
513a3718 99 memoize 'vorbisinfo' , NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] unless $cache eq '';
21cd9240
MG
100
101 my @files;
102 for my $file (@ARGV) {
103 push @files, thaw flacinfo $file if $file =~ /.flac$/i;
104 push @files, thaw mp3info $file if $file =~ /.mp3$/i;
513a3718 105 push @files, thaw vorbisinfo $file if $file =~ /.og(?:g|a)$/i;
21cd9240
MG
106 }
107
e0fa3070
MG
108 my $ht=HTML::Template::Compiled->new(
109 default_escape => 'HTML',
110 $template eq '' ? (scalarref => \$default_template) : (filename => $template),
111 );
21cd9240
MG
112 $ht->param(files=>[sort { $a->{title} cmp $b->{title} } @files]);
113 print $ht->output;
114}
115
75c564d8
MG
116$default_template = <<'HTML';
117<!DOCTYPE html>
118<title>Music</title>
119<meta charset="utf-8">
120<link rel="stylesheet" href="/music.css">
121
122<table border>
123<thead>
124<tr><th>Title<th>Artist<th>Album<th>Genre<th>Track<th>Year<th>Type
125<tbody><tmpl_loop files>
e0fa3070 126<tr><td><a href="<tmpl_var ESCAPE=URL path>"><tmpl_var title></a><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_var format></tmpl_loop>
75c564d8
MG
127</table>
128HTML
129
21cd9240
MG
1301;
131
132__END__
133
134=head1 NAME
135
136App::MusicExpo - script which generates a HTML table of music tags
137
138=head1 SYNOPSIS
139
140 use App::MusicExpo;
141 App::MusicExpo->run;
142
143=head1 DESCRIPTION
144
145App::MusicExpo creates a HTML table from a list of songs.
146
147The default template looks like:
148
149 | Title | Artist | Album | Genre | Track | Year | Type |
150 |---------+---------+-----------------+---------+-------+------+------|
151 | Cellule | Silence | L'autre endroit | Electro | 01/09 | 2005 | FLAC |
152
153where the title is a download link.
154
155=head1 OPTIONS
156
157=over
158
159=item B<--template> I<template>
160
75c564d8 161Path to the HTML::Template::Compiled template used for generating the music table. If '' (empty), uses the default format. Is empty by default.
21cd9240
MG
162
163=item B<--prefix> I<prefix>
164
165Prefix for download links. Defaults to '/music/'.
166
167=item B<--cache> I<filename>
168
fb015c82 169Path to the cache file. Created if it does not exist. If '' (empty), disables caching. Is empty by default.
21cd9240
MG
170
171=back
172
173=head1 AUTHOR
174
175Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
176
177=head1 COPYRIGHT AND LICENSE
178
179Copyright (C) 2013 by Marius Gavrilescu
180
181This library is free software; you can redistribute it and/or modify
182it under the same terms as Perl itself, either Perl version 5.14.2 or,
183at your option, any later version of Perl 5 you may have available.
This page took 0.022596 seconds and 4 git commands to generate.