Created
August 1, 2010 22:56
-
-
Save ecerulm/503868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# this small program starts a http daeomon listening on port 1111 and provides a RSS feed | |
# to all .mp4 files stored in the 'videos' folder. | |
# This program is intended to transfer movie files via wireless. Using the sony psp RSS feed utility | |
# 1. Start the server with ./rssstandaloneserver.pl | |
# 2. Copy some video files on videos subfolder | |
# 3. Point you PSP browser to the http://<address>:<port>/ and the psp browser will display a page to | |
# subscribe to the feed. | |
# 4. Go to Psp->Network->RSS Channel and select the new feed | |
# 5. A list of items should appear and pressing X will download the video files to your VIDEO folder | |
# on the PSP memory stick | |
# Please note that depending of your firmware and the encoder you used on your files PSP may refuse | |
# to play those files from the VIDEO folder. The VIDEO folder is not just like the MP_ROOT/100ANV01 | |
# folder, it behaves a different way. So please first check and transfer some of your files via USB to the | |
# VIDEO folder and check that the PSP is able to play them from there. | |
# If you encode your files using the Media Manager for PSP software then those files will work in any folder. | |
# If you use 3GP encoder and QVGA MPEG-4 then those also will work in the VIDEO folder. but if you use | |
# another resolution or AVC codec then it won't work. | |
use HTTP::Daemon; | |
use HTTP::Status; | |
use XML::RSS; | |
use MP4::Info; | |
use File::stat; | |
use Time::localtime; | |
use URI::Escape; | |
use Encode; | |
use LWP::MediaTypes; | |
#configuration | |
my $feedtitle = "Perl Video Feed"; | |
my $feeddesc = "ecerulm perl video feed"; | |
my $hostname = "192.168.1.3"; | |
my $port = 1111; | |
my $debug = 1; | |
#end of configuration | |
my $rootaddr="http://" . $hostname . ":" . $port; | |
my $ct = "video/m4v"; | |
LWP::MediaTypes::add_type($ct => qw(mp4 MP4)); | |
my $d = HTTP::Daemon->new(LocalPort => $port) || die; | |
print "Please contact me at: <URL:", $d->url, ">\n"; | |
while (my $c = $d->accept) { | |
while (my $r = $c->get_request) { | |
my $url = URI::Escape::uri_unescape($r->url->path); | |
print $r->method . " " . "$url\n" if $debug; | |
if ($r->method eq 'GET' and $url eq "/") { | |
print "sending index.htm\n"; | |
$c->send_file_response("index.htm"); | |
} elsif ($url eq "/index.rss") { | |
print "generating RSS content\n"; | |
my $rss = new XML::RSS (version => '2.0'); | |
$rss->channel(title => $feedtitle, | |
link => $rootaddr, | |
description => $feeddesc, | |
); | |
$rss->image(title => 'Perl video feed', | |
url => $rootaddr . "/images/feedimage.jpg", | |
link => $rootaddr, | |
width => 88, | |
height => 115, | |
description => 'feed logo' | |
); | |
# videos | |
my @fileList = <videos/*.MP4>; | |
foreach $file (@fileList) { | |
my $tag = get_mp4tag($file) or die "No TAG info"; | |
$date_string = ctime(stat($file)->mtime); | |
#my $enclosurelink = "http://192.168.1.3:1111/" . URI::Escape::uri_escape_utf8($file); | |
my $enclosurelink = $rootaddr . "/" . URI::Escape::uri_escape_utf8($tag->{NAM}) . ".MP4"; | |
#my $enclosurelink =~ s/videos%2F/videos\//; | |
$rss->add_item(title => $tag->{NAM}, | |
enclosure => { | |
url=>$enclosurelink, | |
type=>$ct, | |
}, | |
description => $tag->{NAM}, | |
pubDate=>$date_string | |
); | |
} | |
# or save it to a file | |
my $rs = new HTTP::Response(RC_OK); | |
$rs->header('Content-type', "application/rss+xml"); | |
$rs->content($rss->as_string) if $r->method eq 'GET'; | |
$c->send_response($rs); | |
print "RSS content sent\n" if $debug; | |
} elsif (-e "." . $url) { | |
print "the $url maps directly to a file in the filesystem\n" if $debug; | |
if ($r->method eq 'GET') { | |
print "sending " . $r->method . " " . $url . "\n"; | |
$c->send_file_response("." . $url) if $r->method eq 'GET'; | |
} else { | |
print "sending HEAD " . $url . "\n"; | |
$c->send_basic_header; | |
print $c "Content-type: $ct\n\n"; | |
} | |
} else { | |
print "$url doesn't map to file directly. We assume the url is the movie title\n" if $debug; | |
my $t = $url; | |
$t = Encode::decode("UTF-8", $t); | |
$t = substr($t,1,-4); #remove the ".mp4" part. | |
print "looking for a file with movie title: $t\n" if $debug; | |
my @files = <videos/*.MP4>; | |
my $found = 0; | |
foreach $f (@files) { | |
my $tag = get_mp4tag($f) or next; | |
if ($tag->{NAM} eq $t) { | |
print "sending " . $f . " file\n"; | |
$c->send_file_response($f); | |
$found = 1; | |
last; | |
} | |
} | |
unless ($found) { | |
print "cannot find " . $url . " using method " . $r->method . "\n"; | |
$c->send_error(RC_NOT_FOUND); | |
} | |
} | |
} | |
$c->close; | |
undef($c); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment