Apache-MP3
view release on metacpan or search on metacpan
}
#################################################
# interesting configuration directives start here
#################################################
#utility subroutine for configuration
sub get_dir {
my $self = shift;
my ($config,$default) = @_;
my $dir = $self->r->dir_config($config) || $default;
return $dir if $dir =~ m!^/!; # looks like a path
return $dir if $dir =~ m!^\w+://!; # looks like a URL
return $self->default_dir . '/' . $dir;
}
# return true if downloads are allowed from this directory
sub download_ok {
my $d = shift->r->dir_config('AllowDownload') || '';
return $d !~ /$NO/oi;
}
# return true if streaming is allowed from this directory
sub stream_ok {
my $d = shift->r->dir_config('AllowStream') || '';
return $d !~ /$NO/oi;
}
# return true if playing locally is allowed
sub playlocal_ok {
my $d = shift->r->dir_config('AllowPlayLocally') || '';
return $d =~ /$YES/oi;
}
# return true if we should check that the client can accomodate streaming
sub check_stream_client {
my $d = shift->r->dir_config('CheckStreamClient') || '';
return $d =~ /$YES/oi;
}
# return true if client can stream
sub is_stream_client {
my $r = shift->r;
my $h = $r->headers_in;
$h->{'Icy-MetaData'} # winamp/xmms
|| $h->{'Bandwidth'} # realplayer
|| $h->{'Accept'} =~ m!\baudio/mpeg\b! # mpg123 and others
|| $h->{'User-Agent'} =~ m!^NSPlayer/! # Microsoft media player
|| $h->{'User-Agent'} =~ m!^xmms/!;
}
# whether to read info for each MP3 file (might take a long time)
sub read_mp3_info {
my $d = shift->r->dir_config('ReadMP3Info') || '';
return $d !~ /$NO/oi;
}
# whether to time out streams
sub stream_timeout {
shift->r->dir_config('StreamTimeout') || 0;
}
# how long an album list is considered so long we should put buttons
# at the top as well as the bottom
sub file_list_is_long { shift->r->dir_config('LongList') || 10 }
sub home_label {
my $self = shift;
my $home = $self->r->dir_config('HomeLabel') ||
$self->x('Home');
return lc($home) eq 'hostname' ? $self->r->hostname : $home;
}
sub path_style { # style for the path to parent directories
lc(shift->r->dir_config('PathStyle')) || 'Staircase';
}
# where is our cache directory (if any)
sub cache_dir {
my $self = shift;
return unless my $dir = $self->r->dir_config('CacheDir');
my $rootdir = Apache2::ServerUtil::server_root();
return $dir if $dir =~ m!^/!;
return "$rootdir/$dir";
}
# columns to display
sub subdir_columns {shift->r->dir_config('SubdirColumns') || SUBDIRCOLUMNS }
sub playlist_columns {shift->r->dir_config('PlaylistColumns') || PLAYLISTCOLUMNS }
# various configuration variables
sub default_dir { shift->r->dir_config('BaseDir') || BASE_DIR }
sub stylesheet { shift->get_dir('Stylesheet', STYLESHEET) }
sub parent_icon { shift->get_dir('ParentIcon',PARENTICON) }
sub cd_list_icon {
my $self = shift;
my $subdir = shift;
my $image = $self->r->dir_config('CoverImageSmall') || COVERIMAGESMALL;
my $directory_specific_icon = $self->r->filename."/$subdir";
my $uri = escape($subdir)."/$image";
# override the icon filename if the dir is fully pathed
if (substr($subdir, 0, 1) eq "/") {
$directory_specific_icon = $self->r->lookup_uri($subdir)->filename;
}
$directory_specific_icon .= "/$image";
return -e $directory_specific_icon
? $uri
: $self->get_dir('DirectoryIcon',CDLISTICON);
}
sub playlist_icon {
my $self = shift;
my $image = $self->r->dir_config('PlaylistImage') || PLAYLISTIMAGE;
my $directory_specific_icon = $self->r->filename."/$image";
warn $directory_specific_icon if DEBUG;
return -e $directory_specific_icon
? $self->r->uri . "/$image"
: $self->get_dir('PlaylistIcon',PLAYLISTICON);
}
cddbcmd cddb read soundtrack cb115c11 > INDEX
to create an INDEX file for the Mulholland Drive soundtrack. The
32-bit disc ID can be obtained with a program such as cd-discid.
=item 8. Set up an information cache directory (optional)
In order to generate its MP3 listing, Apache::MP3 must open each sound
file, extract its header information, and close it. This is time
consuming, particularly when recursively generating playlists across
multiple directories. To speed up this process, Apache::MP3 has the
ability cache MP3 file information in a separate directory area.
To configure this, choose a directory that the Web server has write
access for, such as /usr/tmp. Then add a configuration variable like
the following to the <Location> directive:
PerlSetVar CacheDir /usr/tmp/mp3_cache
If the designated directory does not exist, Apache::MP3 will attempt
to create it, limited of course by the Web server's privileges. You
may need to create the mp3_cache directory yourself if /usr/tmp is not
world writable.
=back
Open up the MP3 URL in your favorite browser. You should be able to
see directory listings, and download and stream your songs. If things
don't seem to be working, checking the server error log for messages.
=head1 CUSTOMIZING
Apache::MP3 can be customized in three ways: (1) by changing
per-directory variables; (2) changing settings in the Apache::MP3
cascading stylesheet; and (3) subclassing Apache::MP3 or
Apache::MP3::Sorted.
=head2 Per-directory configuration variables
Per-directory variables are set by I<PerlSetVar> directives in the
Apache::MP3 E<lt>LocationE<gt> or E<lt>DirectoryE<gt> section. For
example, to change the icon displayed next to subdirectories of MP3s,
you would use I<PerlSetVar> to change the I<DirectoryIcon> variable:
PerlSetVar DirectoryIcon big_cd.gif
This following table summarizes the configuration variables. A more
detailed explanation of each follows in the subsequent sections.
Table 1: Configuration Variables
Name Value Default
---- ----- -------
GENERAL OPTIONS
AllowDownload yes|no yes
AllowStream yes|no yes
AllowPlayLocally yes|no yes
CheckStreamClient yes|no no
ReadMP3Info yes|no yes
StreamTimeout integer 0
DIRECTORY OPTIONS
BaseDir URL /apache_mp3
CacheDir path -none-
HelpImgURL URL apache_mp3_fig1.gif:374x292
StreamBase URL -none-
LocalNet subnet -none-
DISPLAY OPTIONS
ArrowIcon URL right_arrow.gif
CoverImage filename cover.jpg
CoverImageSmall filename cover_small.jpg
PlaylistImage filename playlist.jpg
DescriptionFormat string -see below-
DirectoryIcon URL cd_icon_small.gif
PlaylistIcon URL playlist.gif
Fields list title,artist,duration,bitrate
HomeLabel string "Home" (or translation)
LongList integer 10
MissingComment string "unknown" (or translation)
PathStyle Staircase|Arrows|Slashes Staircase
SongIcon URL sound.gif
SubdirColumns integer 1
Stylesheet URL apache_mp3.css
TitleIcon URL cd_icon.gif
DefaultLanguage languagetag en-US
=head2 General Configuration Variables
=over 4
=item AllowDownload I<yes|no>
You may wish for users to be able to stream songs but not download
them to their local disk. If you set AllowDownload to "no",
Apache::MP3 will not generate a download link for MP3 files. It will
also activate some code that makes it inconvenient (but not
impossible) for users to download the MP3s.
The module recognizes the arguments "yes", "no", "true" and "false".
The default is "yes".
Note that this setting only affects MP3 files. Other files, including
cover art and playlists, can still be downloaded.
=item AllowStream I<yes|no>
If you set AllowStream to "no", users will not be able to stream songs
or generate playlists. I am not sure why one would want this feature,
but it is included for completeness. The default is "yes."
=item AllowPlayLocally I<yes|no>
If you set AllowPlayLocally to "yes", then the playlists generated by
the module will point to the physical files when handling requests
from a user that happens to be working on the same machine. This is
more efficient, and allows the user to pause playback, fast forward,
and so on. Otherwise, the module will treat local users and remote
users the same. The default is "no".
=item CheckStreamClient I<yes|no>
Setting CheckStreamClient to "yes" enables code that checks whether
the client claims to be able to accept streaming MPEG data. This
check isn't foolproof, but supports at least the most popular MP3
decoders (WinAmp, RealPlayer, xmms, mpg123). It also makes it harder
for users to download songs by pretending to be a streaming player.
The default is "no".
=item ReadMP3Info I<yes|no>
This controls whether to extract field information from the MP3
files. The default is "yes".
If "no" is specified, all fields in the directory listing will be
blank except for I<filename> and I<description>, which will both be
set to the physical filename of the MP3 file.
=item StreamTimeout I<integer>
For demo mode, you can specify a stream timeout in seconds.
Apache::MP3 will cease streaming the file after the time specified.
Because this feature uses the average bitrate of the song, it may be
off by a second or two when streaming variable bitrate MP3s.
=back
=head2 Configuration Variables Affecting Paths and Directories
=over 4
=item BaseDir I<URL>
The B<BaseDir> variable sets the URL in which Apache::MP3 will look
for its icons and stylesheet. You may use any absolute local or
remote URL. Relative URLs are not accepted.
The default is "/apache_mp3."
=item CacheDir I<path>
This variable sets the directory path for Apache::MP3's cache of MP3
file information. This must be an absolute path in the physical file
system and be writable by Apache. If not specified, Apache::MP3 will
not cache the file information, resulting in slower performance on
large directories.
=item HelpImgURL I<URL:widthxheight>
The URL of the image that's inlined on the page that appears
when the user presses the "Quick Help
Summary" link at the bottom of the page. You can declare the
size of this image
by adding ":WxH" to the end of the URL, where W and H are the width
and height, respectively.
Default: apache_mp3_help.gif:614x498
Note: I prepared this image on an airplane, so it isn't as clean as I
would like. Volunteers to make a better help page are welcomed!
=item StreamBase I<URL>
A URL to use as the base for streaming. The default is to use the
same host for both directory listings and streaming. This may be of
use when running behind a firewall and the web server can't figure out
the correct address for the playlist automatically.
Example:
If the song requested is http://www.foobar.com/Songs/Madonna_live.m3u?stream=1
and B<StreamBase> is set to I<http://streamer.myhost.net>, then the URL
placed in the playlist will be
http://streamer.myhost.net/Songs/Madonna_live.m3u?stream=1
The path part of the URL is simply appended to StreamBase. If you
want to do more sophisticated URL processing, use I<mod_rewrite> or
( run in 1.573 second using v1.01-cache-2.11-cpan-140bd7fdf52 )