examples/dms2vodcast.pl

filter: Statementstmt 81.6% 49 / 60 Branchbran 55.0% 11 / 20 Conditioncond 41.6% 5 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 68.8% 73 / 106 CC 15 SCAR 19.0
CC
15
Cov
71%
parse_content_directory
10.9
mpeg2tompeg4
6.6
is_command_option
4.0
CRAP 6.7
« blib/lib/Net/UPnP/AV/MediaServer.pm ↑ Summary blib/lib/Net/UPnP/GW/Gateway.pm »
1 #!/usr/bin/env perl
2  
3 1 ▶use Net::UPnP::ControlPoint;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Net::UPnP::AV::MediaServer;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Net::UPnP::AV::RSS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Shell qw(curl ffmpeg);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #curl('--version');
10 #ffmpeg('-version');
11  
12 #------------------------------
13 # program info
14 #------------------------------
15  
16 $program_name = 'DLNA Media Sever 2 Vodcast';
17 $copy_right = 'Copyright (c) 2005 Satoshi Konno';
18 $script_name = 'dms2vodcast.pl';
19 $script_version = '1.0.3';
20  
21 #------------------------------
22 # global variables
23 #------------------------------
24  
25 @dms_content_list = ();
26  
27 #------------------------------
28 # command option
29 #------------------------------
30  
31 $rss_file_name = "";
32 $base_directory = "./";
33 $rss_base_url= "http://localhost";
34 $rss_description = "CyberGarage Vodcast";
35 $rss_language = "";
36 $rss_link= "";
37 $rss_title = "CyberGarage";
38 $requested_count = 0;
39 $mp4_format = 'ipod';
40 $title_regexp = "";
41
42 @command_opt = (
43 ['-b', '--base-url', '<url>', 'Set the base url in the item link property of the output RSS file'],
44 ['-B', '--base-directory', '<url>', 'Set the base directory to output the RSS file and the MPEG4 files'],
45 ['-d', '--rss-description', '<description>', 'Set the description tag in the output RSS file'],
46 ['-g', '--rss-language', '<language>', 'Set the language tag in the output RSS file'],
47 ['-h', '--help', '', 'This is help text.'],
48 ['-l', '--rss-link', '<link>', 'Set the link tag in the output RSS file'],
49 ['-r', '--requested-count', '<url>', 'Set the max request count to the media server contents'],
50 ['-t', '--rss-title', '<file>', 'Set the title tag in the output RSS file'],
51 ['-f', '--mp4-format', '<ipod | psp>', 'Set the MPEG4 format'],
52 ['-s', '--search-title', '<regular expression>', 'Set the regular expression of the content titles by UTF-8'],
53 );
54  
55 2 ▶sub is_command_option {
Subroutine is_command_option: called CC=4 SCAR=13.9
56 2 ($opt) = @_;
57 2 ▶ for ($n=0; $n<@command_opt; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @command_opt) 12 1 13
58 12 ▶ if ($opt eq $command_opt[$n][0] || $opt eq $command_opt[$n][1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt eq $command_opt[$n][0] or $opt eq $command_opt[$n][1]) 1 11 12
Condition66%2/3
$opt eq $command_opt[$n][0] or $opt eq $command_opt[$n][1]
l!l&&r!l&&!r
1011
MC/DC50%1/2
$opt eq $command_opt[$n][0] or $opt eq $command_opt[$n][1]
$opt eq $command_opt[$n][0] $opt eq $command_opt[$n][1]
Truth table66%2/3
A or B
A= $opt eq $command_opt[$n][0]B= $opt eq $command_opt[$n][1]
ABresult
1X1
011
000
59 1 return $n;
60 }
61 }
62 1 return -1;
63 }
64  
65 #------------------------------
66 # main (pase command line)
67 #------------------------------
68  
69 for ($i=0; $i<(@ARGV); $i++) {
70 $opt = $ARGV[$i];
71 $opt_num = is_command_option($opt);
72 $opt_short_name = '';
73 if ($opt_num < 0) {
74 if ($opt =~ m/^-/) {
75 print "$script_name : option $opt is unknown\n";
76 print "$script_name : try \'$script_name --help\' for more information \n";
77 exit 1;
78 }
79 }
80 else {
81 $opt_short_name = $command_opt[$opt_num][0];
82 }
83 if ($opt_short_name eq '-h') {
84 print "Usage : $script_name [options...] <output RSS file name>\n";
85 print "Options : \n";
86 $max_opt_output_len = 0;
87 for ($n=0; $n<@command_opt; $n++) {
88 $opt_output_len = length("$command_opt[$n][0]\/$command_opt[$n][1] $command_opt[$n][2]");
89 if ($max_opt_output_len <= $opt_output_len) {
90 $max_opt_output_len = $opt_output_len;
91 }
92 }
93 for ($n=0; $n<@command_opt; $n++) {
94 $opt_output_str = "$command_opt[$n][0]\/$command_opt[$n][1] $command_opt[$n][2]";
95 print $opt_output_str;
96 for ($j=0; $j<($max_opt_output_len-length($opt_output_str)); $j++) {
97 print " ";
98 }
99 print " $command_opt[$n][3]\n";
100 }
101 exit 1;
102 } elsif ($opt_short_name eq '-b') {
103 $rss_base_url = $ARGV[++$i];
104 } elsif ($opt_short_name eq '-B') {
105 $base_directory = $ARGV[++$i];
106 } elsif ($opt_short_name eq '-d') {
107 $rss_description = $ARGV[++$i];
108 } elsif ($opt_short_name eq '-g') {
109 $rss_language = $ARGV[++$i];
110 } elsif ($opt_short_name eq '-l') {
111 $rss_link = $ARGV[++$i];
112 } elsif ($opt_short_name eq '-r') {
113 $requested_count = $ARGV[++$i];
114 } elsif ($opt_short_name eq '-t') {
115 $rss_title = $ARGV[++$i];
116 } elsif ($opt_short_name eq '-f') {
117 $mp4_format = $ARGV[++$i];
118 if ($mp4_format ne 'ipod' && $mp4_format ne 'psp') {
119 print "Unkown MPEG4 format : $mp4_format !!\n";
120 exit 1;
121 }
122 } elsif ($opt_short_name eq '-s') {
123 $title_regexp = $ARGV[++$i];
124 } else {
125 $rss_file_name = $opt;
126 }
127 }
128  
129 if (length($rss_file_name) <= 0) {
130 print "$script_name : Must specify a output RSS file name\n";
131 print "$script_name : try \'$script_name --help\' for more information \n";
132 exit 1 ;
133 }
134  
135 print "$program_name (v$script_version), $copy_right\n";
136 print "Output RSS file name = $rss_file_name\n";
137 print " title : $rss_title\n";
138 print " description : $rss_description\n";
139 print " language : $rss_language\n";
140 print " base url : $rss_base_url\n";
141 print " base directory : $base_directory\n";
142 print " requested_count : $requested_count\n";
143 print " mp4_format : $mp4_format\n";
144 print " search regexp : $title_regexp\n";
145  
146 #------------------------------
147 # main
148 #------------------------------
149  
150 my $obj = Net::UPnP::ControlPoint->new();
151  
152 $retry_cnt = 0;
153 @dev_list = ();
154 while (@dev_list <= 0 || $retry_cnt > 5) {
155 # @dev_list = $obj->search(st =>'urn:schemas-upnp-org:device:MediaServer:1', mx => 10);
156 @dev_list = $obj->search(st =>'upnp:rootdevice', mx => 3);
157 $retry_cnt++;
158 }
159  
160 $devNum= 0;
161 foreach $dev (@dev_list) {
162 $device_type = $dev->getdevicetype();
163 if ($device_type ne 'urn:schemas-upnp-org:device:MediaServer:1') {
164 next;
165 }
166 unless ($dev->getservicebyname('urn:schemas-upnp-org:service:ContentDirectory:1')) {
167 next;
168 }
169 print "[$devNum] : " . $dev->getfriendlyname() . "\n";
170 $mediaServer = Net::UPnP::AV::MediaServer->new();
171 $mediaServer->setdevice($dev);
172 $mediaServer->walkcontent(callback => sub { parse_content_directory($mediaServer, $_[0]); });
173 die $mediaServer->getwalkerror() if $mediaServer->getwalkerror();
174 $devNum++;
175 }
176  
177 #------------------------------
178 # Output RSS file
179 #------------------------------
180  
181 if (@dms_content_list <= 0) {
182 print "Couldn't find video contents !!\n";
183 exit 1;
184 }
185  
186 $output_rss_filename = $base_directory . $rss_file_name;
187  
188 open(RSS_FILE, '>:encoding(UTF-8)', $output_rss_filename) || die "Couldn't open the specifed output file($output_rss_filename)\n";
189  
190 $rss_header = Net::UPnP::AV::RSS::header(
191 title => $rss_title, language => $rss_language,
192 description => $rss_description, link => $rss_link);
193 print RSS_FILE $rss_header;
194  
195 foreach $content (@dms_content_list){
196 $title = $content->{'title'};
197 $fname = $content->{'file_name'};
198 $fsize = $content->{'file_size'};
199  
200 $mp4_link = $rss_base_url . $fname;
201 $mp4_item = Net::UPnP::AV::RSS::item(
202 title => $title, link => $mp4_link, size => $fsize);
203 print RSS_FILE $mp4_item;
204 }
205  
206 $rss_footer = <<"RSS_FOOTER";
207 </channel>
208 </rss>
209 RSS_FOOTER
210 print RSS_FILE $rss_footer;
211  
212 close(RSS_FILE);
213  
214 $rss_outputed_items = @dms_content_list;
215 print "Outputed $rss_outputed_items RSS items to $output_rss_filename\n";
216  
217 #------------------------------
218 # parse_content_directory
219 #------------------------------
220  
221 1 ▶sub parse_content_directory {
Subroutine parse_content_directory: called CC=8 SCAR=23.9
222 1 ($mediaServer, $content) = @_;
223 1 my $objid = $content->getid();
224  
225 1 ▶ if ($content->isitem()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->isitem) 1 0 1
226 1 my $title = $content->gettitle();
227 1 my $mime = $content->getcontenttype();
228 1 ▶ if ( ($mime =~ m/video/) && ( (length($title_regexp) == 0) || ($title =~ m/$title_regexp/) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mime =~ /video/ and length $title_regexp == 0 || $title =~ /$title_regexp/) 1 0 1
Condition33%2/6
$mime =~ /video/ and length $title_regexp == 0 || $title =~ /$title_regexp/
!ll&&!rl&&r
001
length $title_regexp == 0 or $title =~ /$title_regexp/
l!l&&r!l&&!r
100
MC/DC0%0/3
$mime =~ /video/ and length $title_regexp == 0 || $title =~ /$title_regexp/
$mime =~ /video/ length $title_regexp == 0 $title =~ /$title_regexp/
Truth table25%1/4
A and B or C
A= $mime =~ /video/B= length $title_regexp == 0C= $title =~ /$title_regexp/
ABCresult
0XX0
1000
11X1
1011
229 1 my $dms_content_count = @dms_content_list;
230 1 ▶ if ($requested_count == 0 || $dms_content_count < $requested_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requested_count == 0 or $dms_content_count < $requested_count) 1 0 1
Condition33%1/3
$requested_count == 0 or $dms_content_count < $requested_count
l!l&&r!l&&!r
100
MC/DC0%0/2
$requested_count == 0 or $dms_content_count < $requested_count
$requested_count == 0 $dms_content_count < $requested_count
Truth table33%1/3
A or B
A= $requested_count == 0B= $dms_content_count < $requested_count
ABresult
1X1
011
000
231 1 my $mp4_content = mpeg2tompeg4($mediaServer, $content);
232 1 ▶ if (defined($mp4_content)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mp4_content) 1 0 1
233 1 push(@dms_content_list, $mp4_content);
234 }
235 }
236 }
237 }
238
239 }
240  
241 #------------------------------
242 # mpeg2tompeg4
243 #------------------------------
244  
245 1 ▶sub mpeg2tompeg4 {
Subroutine mpeg2tompeg4: called CC=5 SCAR=18.9
246 1 ($mediaServer, $content) = @_;
247 1 my $objid = $content->getid();
248 1 my $title = $content->gettitle();
249 1 my $url = $content->geturl();
250
251 1 print "[$objid] $title ($url)\n";
252
253 1 my $dev = $mediaServer->getdevice();
254 1 my $dev_friendlyname = $dev->getfriendlyname();
255 1 my $dev_udn = $dev->getudn();
256 1 $dev_udn =~ s/:/-/g;
257
258 1 my $filename_body = $dev_friendlyname . "_" . $dev_udn . "_" . $objid;
259 1 $filename_body =~ s/ //g;
260 1 $filename_body =~ s/\//-/g;
261
262 1 my $mpeg2_file_name = $filename_body . ".mpeg";
263 1 my $mpeg4_file_name = $filename_body . "_" . $mp4_format . ".m4v";
264 1 my $output_mpeg4_file_name = $base_directory . $mpeg4_file_name;
265  
266 1 ▶ if (!(-e $output_mpeg4_file_name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $output_mpeg4_file_name) 0 1 1
267 0 $curl_opt = "\"$url\" -o \"$mpeg2_file_name\"";
268 0 print "curl $curl_opt\n";
269 0 curl($curl_opt);
270  
271 0 ▶ if ($mp4_format eq 'psp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mp4_format eq 'psp') { } 0 0 0
272 0 $ffmpeg_opt = "-y -i \"$mpeg2_file_name\" -bitexact -fixaspect -s 320x240 -r 29.97 -b 768 -ar 24000 -ab 32 -f psp \"$output_mpeg4_file_name\"";
273 }
274 else {
275 0 $ffmpeg_opt = "-y -i \"$mpeg2_file_name\" -bitexact -fixaspect -s 320x240 -r 29.97 -b 850 -acodec aac -ac 2 -ar 44100 -ab 64 -f mp4 \"$output_mpeg4_file_name\"";
276 }
277
278 0 print "ffmpeg $ffmpeg_opt\n";
279 0 ffmpeg($ffmpeg_opt);
280
281 0 unlink($mpeg2_file_name);
282 }
283
284 1 ▶ if (!(-e $output_mpeg4_file_name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $output_mpeg4_file_name) 0 1 1
285 0 return undef;
286 }
287
288 1 my $mpeg4_file_size = -s $output_mpeg4_file_name;
289
290 1 ▶ if ($mpeg4_file_size <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mpeg4_file_size <= 0) 0 1 1
291 0 return undef;
292 }
293
294 1 my %info = (
295 'objid' => $objid,
296 'title' => $title,
297 'file_name' => $mpeg4_file_name,
298 'file_size' => $mpeg4_file_size,
299 );
300
301 1 return \%info;
302 }
303  
304 exit 0;
305  
« blib/lib/Net/UPnP/AV/MediaServer.pm ↑ Summary blib/lib/Net/UPnP/GW/Gateway.pm »