星期四, 7月 15, 2004

shout-icecast-mp3.pl

#!/usr/bin/perl -w

use strict;
use Shout;
use bytes;

# start the connection
my $conn = new Shout;

if ( $ARGV[0] eq "" ) {
printf ("Usage: shout-icecast-mp3.pl channel_title mount_name play_list\n");
exit(1);
}
else {
printf ("Channel Title:$ARGV[0]\nMount: $ARGV[1]\nPlaylist: $ARGV[1]\n");
}

# setup all the params
$conn->host('localhost');
$conn->port(8000);
$conn->mount("/$ARGV[1]");
$conn->password('xxxxxx');
$conn->public(0);
$conn->format(SHOUT_FORMAT_MP3);
$conn->protocol(SHOUT_PROTOCOL_HTTP);
$conn->set_audio_info(SHOUT_AI_BITRATE => 128, SHOUT_AI_SAMPLERATE => 44100);
$conn->name($ARGV[0]);
$conn->description("This music streaming station just for DEMO only");

my @BUF;
my $fn;
my $title;

# try to connect
if ($conn->open) {
print "connected...\n";
#$conn->set_metadata("song" => "Streaming from standard in");
while ( 1 ) {
open(m3uLIST, $ARGV[2]);
@BUF=;
close(m3uLIST);

my $tmp;
my $i;
my $j;
for ($i = @BUF; --$i;) {
my $j = int rand ($i+1);
next if $i == $j;
#print "$i <--> $j\n";
$tmp = $BUF[$j];
$BUF[$j] = $BUF[$i];
$BUF[$i] = $tmp;
}

my ($s1, $s2, $s3, $s4);
foreach $fn(@BUF) {
$title = $fn;
$title =~ s/\/var\/www\/html//g;
$title =~ s/\.mp3//g;
($s1, $s2, $s3, $s4)=split('\/', $title);
$conn->set_metadata("song" => "playing $s3 -- $s4 ($s2) now...");
# if we connect, grab data from stdin and shoot it to the server
my ($buff, $len);
printf("playing $fn now");
open (MP3, $fn);
while (($len = sysread(MP3, $buff, 4096)) > 0) {
unless ($conn->send($buff)) {
print "Error while sending: " . $conn->get_error . "\n";
last;
}

# must be careful not to send the data too fast :)
$conn->sync;
}
close(MP3);
}
}
# all done
$conn->close;
} else {
print "couldn't connect...\n";
}
張貼留言