Ticket #5006: handler.pl.patch
File handler.pl.patch, 2.7 KB (added by , 16 years ago) |
---|
-
handler.pl
old new 18 18 $|++; 19 19 20 20 our $ffmpeg_pid; 21 our $mencoder_pid; 22 our $stream_file; 23 24 # $use_ffmpeg = 1; 21 25 22 26 # Shutdown cleanup, of various types 23 27 $SIG{'TERM'} = \&shutdown_handler; … … 27 31 } 28 32 sub shutdown_handler { 29 33 kill(1, $ffmpeg_pid) if ($ffmpeg_pid); 34 kill(1, $mencoder_pid) if ($mencoder_pid); 35 unlink($stream_file) if ($stream_file); 30 36 } 31 37 32 38 # Which show are we streaming? … … 51 57 } 52 58 } 53 59 60 # Find mencoder 61 $mencoder = ''; 62 foreach my $path (split(/:/, $ENV{'PATH'}.':/usr/local/bin:/usr/bin'), '.') { 63 if (-e "$path/mencoder") { 64 $mencoder = "$path/mencoder"; 65 last; 66 } 67 } 68 54 69 # Get the basename from the database 55 70 my $sh = $dbh->prepare('SELECT basename, title, subtitle 56 71 FROM recorded … … 160 175 } 161 176 elsif ($ENV{'REQUEST_URI'} =~ /\.flv$/i) { 162 177 # Print the movie 178 if ($use_ffmpeg) { 163 179 $ffmpeg_pid = open(DATA, 164 180 "$ffmpeg -y -i ".shell_escape($filename) 165 181 .' -s '.shell_escape("${width}x$height") … … 176 192 print header(-type => 'video/x-flv'); 177 193 my $buffer; 178 194 while (read DATA, $buffer, 262144) { 179 print $buffer; 195 print $buffer; 180 196 } 181 197 close DATA; 182 198 exit; 199 } else { 200 # Print the movie 201 $unique = int(rand(100000)); 202 $stream_file = "/tmp/${basename}-${unique}.flv"; 203 $command = "$mencoder -oac mp3lame -lameopts abr:br=" 204 . ${abitrate} . ' -srate 22050' 205 .' -ovc lavc -lavcopts vcodec=flv:vbitrate=' 206 .${vbitrate} . ':mbd=2:mv0:trell' 207 .':v4mv:cbp:last_pred=3' 208 .' -vf crop=698:466,scale=320:240,lavcdeint,harddup' 209 .' -ofps 24 -sws 2 '.shell_escape($filename) 210 .' -of lavf -o '.shell_escape($stream_file).' 2>&1 |' 211 ; 212 $mencoder_pid = open(PROC, $command); 213 unless ($mencoder_pid) { 214 print header(), "Can't do mencoder: $!"; 215 exit; 216 } 217 for($i=0; ($i < 10) && !(-e $stream_file); $i++) { 218 sleep(1); 219 } 220 unless (sysopen DATA, $stream_file, O_RDONLY) { 221 print header(), "Can't read $stream_file: $!"; 222 exit; 223 } 224 225 print header(-type => 'video/x-flv'); 226 my $buffer; 227 my $tempBuf; 228 while (read PROC, $tempBuf, 64) { 229 while (sysread DATA, $buffer, 262144) { 230 print $buffer; 231 } 232 } 233 while (sysread DATA, $buffer, 262144) { 234 print $buffer; 235 } 236 237 close DATA; 238 close PROC; 239 exit; 240 } 183 241 } 184 242 185 243 # File size