#!/usr/bin/perl ###################################################################################### # HDTV movie processor # # Look through a directory of recorded HDTV movies and convert them to something more manageable and # reasonable. # # This scripat assues the files are named show_channel_date.m2t where date # is mm-dd-yy. This date can # be obtained from `date +%m-%d-%y`. For instance, you could use the following in a # cronjob : # # dtvstream -c 49 -p 1 -m 60 -o westwing_49_`date +%m-%d-%y`.m2t # -c channel # -p program (subchannel) # -m minutes # -o outfile ##################################################################################### use strict; use LWP::UserAgent; use HTTP::Cookies; our $movie_dir = '/movies'; our $mencoder = '/usr/bin/mencoder'; our $mplayer = '/usr/bin/mplayer'; our $min_size = 280; #after transcode, delete source file if avi is above this MB size ## dont edit below this line ######################################################## our $url; our $req; our $res; our @months = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); our ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks); our $ua = LWP::UserAgent->new; $ua->agent("Mozilla/8.0"); # pretend we are very capable browser $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt", autosave => 1)); # just in case they use cookies foreach our $file (glob("$movie_dir/*")){ my ($show,$channel,$date); $file =~ s/^$movie_dir\///i; if($file =~ /(.*)\.m2t$/){ # this is an unprocessed movie file my $newfilename = $1; # uncomment for mplayer method my $probe_output = `$mplayer -identify -frames 0 "$movie_dir/$file" 2>/dev/null`; my ($width_in,$height_in); if($probe_output =~ /ID_VIDEO_WIDTH=(\d+)/){ $width_in = $1; } if($probe_output =~ /ID_VIDEO_HEIGHT=(\d+)/){ $height_in = $1; } my $encoded=0; if(-e "$movie_dir/$newfilename.avi"){ my $count =1; until(!(-e "$movie_dir/$newfilename$count.avi")){ $count++; } $newfilename = "$newfilename$count"; } print "$file -> $newfilename.avi\n"; # channel uses 1920x1080 1080i if($height_in eq "1080"){ `$mencoder "$movie_dir/$file" -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vqscale=3:vhq:v4mv:autoaspect:trell -vf field=0,scale=960:540 -o "$movie_dir/$newfilename.avi"`; $encoded = 1; # channel uses 480p or 480i (we deinterlace here no matter what (does this break p?)) }elsif($height_in eq "480"){ #`$mencoder "$movie_dir/$file" -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vqscale=3:vhq:v4mv:autoaspect:trell -vf field=0 -o "$movie_dir/$newfilename.avi"`; `$mencoder "$movie_dir/$file" -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vqscale=3:vhq:v4mv:autoaspect:trell -vf lavcdeint -o "$movie_dir/$newfilename.avi"`; $encoded = 1; # channel uses 720p }elsif($height_in eq "720"){ `$mencoder "$movie_dir/$file" -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vqscale=3:vhq:v4mv:autoaspect:trell -o "$movie_dir/$newfilename.avi"`; $encoded = 1; # default case - this should never happen }else{ print "ERROR: Don't know how to handle $file! height says it is $height_in from $probe_output.\n"; } ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat("$movie_dir/$newfilename.avi"); if(($size > (1024*1024*$min_size)) && $encoded){ unlink("$movie_dir/$file"); print "deleted $file.\n" } } }