Ruben's blog

Nothing to say

Perl RSS Server for the Sony PSP

| Comments

I created this little perl program that creates an RSS feed from a set of video files (.mp4). This is useful to wirelessly transfer files to the PSP. The files will be saved in the VIDEO folder of the PSP.

You’ll need to install MP4::Info and HTTP::Daemon from CPAN first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/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);
}

The code is also available as a gist

Comments