4 # This script requires the following linux packages to be installed:
5 # 1) dvgrab : apt-get install dvgrab
6 # 2) mencoder : http://www.mplayerhq.hu/homepage/design7/dload.html
7 # 3) transcode : vi sources.list; deb ftp://ftp.nerim.net/debian-marillat sarge main
8 # apt-get install transcode
9 # 4) avimerge : apt-get install transcode-utils
13 ############################################################
15 ############################################################
18 #bitrate=3000 is a good choice to allow 2 90 minutes tapes to fit on one DVD
19 #bitrate=4500 is a good choice to allow 2 60 minute tapes to fit on one DVD
21 $encoded_dir = "xvid_clips";
22 $merge_dir = "daily_clips";
23 $default_tape_name = "home_video";
24 $playlist_extension = "pls";
25 $dvgrab = "/usr/bin/dvgrab";
26 $mencoder = "/usr/bin/mencoder";
27 $mplayer = "/usr/bin/mplayer";
28 $avimerge = "/usr/bin/avimerge";
29 $avisplit = "/usr/bin/avisplit";
30 $xvid_suffix = "_xvid$bitrate";
31 $dvgrab_log = "../dvgrab.log";
32 $mencoder_log = "../encoding_errors.log";
33 $merge_log = "../avimerge.log";
34 $max_dv_clip_size_in_mb = "1023"; #I had to keep it at this size to avoid errors until avisplit fixed them.
35 #$max_dv_clip_size_in_mb = "22000"; #avisplit still doesn't seem to do things right for large clips
36 # max_tape_grab_time=7200 Allows Max time of 2 hours to allow a tape to be dumped (good for 90 min tape)
37 # max_tape_grab_time=5400 Allows Max time of 1.5 hours to allow a tape to be dumped (good for 60 min tape)
38 $max_tape_grab_time = 7200;
39 #$dvgrab_options = "--autosplit --timestamp --format dv2 --opendml --buffers 2048 --rewind";
40 #$dvgrab_options = "-autosplit -timestamp -format dv2 -opendml -buffers 2048 -rewind";
41 $dvgrab_options = "-autosplit -timestamp -format dv2 -opendml -rewind";
42 $encode_options_pass1 = "-mc 0 -oac mp3lame -lameopts cbr:cbr=128 -ffourcc XVID -fps 29.97 -ovc lavc ";
43 $encode_options_pass1.= "-lavcopts vcodec=mpeg4:vhq:vbitrate=$bitrate:keyint=15:vpass=1 -vf pp=ci";
44 $encode_options_pass2 = "-mc 0 -oac mp3lame -lameopts cbr:cbr=128 -ffourcc XVID -fps 29.97 -ovc lavc ";
45 $encode_options_pass2.= "-lavcopts vcodec=mpeg4:vhq:vbitrate=$bitrate:keyint=15:vpass=2 -vf pp=ci";
46 $mplayer_identify_options = "-vo null -ao null -identify -frames 0";
48 $SIG{ALRM} = \&stop_dvgrab;
49 $SIG{INT} = \&exit_now;
50 $SIG{SUSP} = \&exit_now;
52 ############################################################
53 # COMMAND LINE OPTIONS & PROCESSING
54 ############################################################
55 getopts('hecrasvpmb:t:n:d:');
56 if($opt_h) { &usage(); }
60 print "Usage: $0 <options>\n";
61 print " -e Encode captured raw video from camera.\n";
62 print " -c Capture raw video from camera.\n";
63 print " -m Merge videos clips found in the directory specified by -d <dir>. \n";
64 print " -p Create video playlists for each year & month of video clips in the directory specified by -d <dir>.\n";
65 print " -n <tape> Name of the tape to prepend to the front of each videoclip filename.\n";
66 print " -d <dir> Directory we want to encode. If not specified, <dir>=<tape>\n";
67 print " -a Use abbreviated names for merged video clips: yyyy-mm-dd.avi vs. <tape>.yyyy.mm.dd.avi\n";
68 print " -s Simulate Mode. Don't encode, just tell me what you would do.\n";
69 print " -h Show this usage\n";
70 print " -v Show verbosity\n";
71 print " -t <min> Time in minutes to grab from the camera (default "; print $max_tape_grab_time/60; print ")\n";
72 print " -b <mb> Max size each clip grabbed should be in MB (default $max_dv_clip_size_in_mb)\n";
73 print " -r Reboot the system once the script completes.\n";
75 print "File Structure:\n";
76 print " <dir>/<tape>*.avi MiniDV Raw AVI files\n";
77 print " <dir>/$encoded_dir/<tape>*.avi XVID compressed versions of the Raw AVI files\n";
78 print " <dir>/$merge_dir/<tape>*.avi All XVID clips taken on each day combined together\n";
83 if(!$opt_n && !$opt_d) { &usage(); }
85 if(!$opt_c && !$opt_e && !$opt_p && $opt_m) {
86 if(!$opt_d) { &usage(); }
90 else { $merge_only = 0; }
92 if(!$opt_c && !$opt_e && !$opt_m && $opt_p) {
93 if(!$opt_d) { &usage(); }
97 else { $playlist_only = 0; }
100 system("mkdir -p $opt_n");
104 if(! -d $opt_d) { die "-E- Could not cd to $opt_d\n"; }
106 if(!$opt_n) { $opt_n = $default_tape_name; }
108 if($opt_t) { $max_tape_grab_time = $opt_t * 60; }
109 if($opt_b) { $max_dv_clip_size_in_mb = $opt_b; }
110 $video_directory = qx[pwd]; chomp($video_directory);
112 print "\n-> Changed working directory to: $video_directory\n";
114 ############################################################
116 ############################################################
119 if($opt_c) { &capture_videos(); }
120 if($opt_e) { &encode_videos(); }
121 if($opt_m) { &merge_videos(); }
122 if($opt_p) { &create_playlists(); }
124 # Fix the permissions
125 print "-> Fixing permissions: chown -R $uid.$gid\n";
126 system("chown -R $uid.$gid ../$opt_d");
129 if($opt_r) { &reboot_system(); }
131 ############################################################
133 ############################################################
136 print "-E- Interupt signal recieved. Exiting..\n";
141 # A little routine to stop dvgrab. It hangs when it hits the end of a tape.
146 @pids = `ps -efw | grep $prog | grep -v grep | grep -v $0`;
147 foreach $pid ( @pids ) {
149 if( $pid =~ s/^\S*\s*(\d*).*$prog.*\n$/$1/ ) {
150 if($pid eq "") { next; }
151 print "$date : -I- Stopped dvgrab Process id: \"$pid\"\n";
152 system("kill -9 $pid");
160 if(! -f $dvgrab ){ die "-E- Missing required component: $dvgrab\n"; }
161 alarm($max_tape_grab_time);
162 $minutes = $max_tape_grab_time / 60;
163 print "-> Max tape grab time set to $minutes minutes\n";
164 print "-> Grabbing raw video from the camera\n";
165 print " Sending the status of grabbing the raw video to $dvgrab_log\n";
166 print " Press CTRL-C once to stop the video grab and move onto the next step\n";
167 print " Press CTRL-C three times to stop the video grab and exit\n";
170 system("rm -f $dvgrab_log; touch $dvgrab_log; tail -f $dvgrab_log &");
171 print "$dvgrab $dvgrab_options --size $max_dv_clip_size_in_mb ${opt_n}_\n";
172 system("$dvgrab $dvgrab_options --size $max_dv_clip_size_in_mb ${opt_n}_ 2> $dvgrab_log");
174 else { print " $dvgrab $dvgrab_options --size $max_dv_clip_size_in_mb ${opt_n}_ 2> $dvgrab_log\n"; }
176 sleep 5; # Give some time to catch an interrupt if needed
177 print "-> Done grabbing video from camera\n\n";
179 # Now see if we need to add a ".avi" extension to the video files dvgrab created
180 opendir(VIDEODIR, $video_directory) or die "-E- Could not open: $video_directory";
181 my @all_files = readdir VIDEODIR;
183 foreach $file (@all_files) {
184 if(($file =~ /$opt_n/) && ($file !~ /$opt_n.*?\.avi/)) { rename("$file","$file.avi"); }
189 if(! -f $mencoder) { die "-E- Missing required component: $mencoder\n"; }
190 print "-> Creating high quality xvid copies of each video clip\n";
192 system("rm -f $mencoder_log");
193 system("touch $mencoder_log");
195 my $current_file = "";
196 my $current_output_file = "";
197 my $prev_output_file_dv = "";
198 my $prev_output_file_xvid = "";
200 print " Processing All Videos in Directory: $video_directory\n";
202 opendir(VIDEODIR, $video_directory) or die "-E- Could not open: $video_directory";
203 my @all_files = readdir VIDEODIR;
205 @all_files = sort @all_files;
206 system "mkdir -p $encoded_dir";
208 foreach $current_file (@all_files) {
209 next if -d $current_file;
210 next if (!($current_file =~ /\.avi$/));
211 $current_output_file = $current_file;
212 $current_output_file =~ s/\.avi$/$xvid_suffix\.avi/;
213 if (-f "./$encoded_dir/$current_output_file") {
214 print " skipping $current_file - ./$encoded_dir/$current_output_file already exists.\n";
217 print " $current_file -> ./$encoded_dir/$current_output_file\n";
219 print "$mencoder $encode_options_pass1 -o ./$encoded_dir/$current_output_file -idx $current_file\n";
220 print "$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file\n";
223 $tempfile = `tempfile`; chomp($tempfile);
224 system("$mencoder $encode_options_pass1 -o ./$encoded_dir/$current_output_file -idx $current_file 2>$tempfile");
225 system("$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file 2>>$tempfile");
226 if(! -e "./$encoded_dir/$current_output_file") {
227 print "-WARNING- File could not be encoded. Attempting repair through avisplit. Encoding reattempt will follow.\n";
228 if(! -f $avisplit) { die "-E- Missing required component: $avisplit\n"; }
229 $split_size = $max_dv_clip_size_in_mb * 10;
230 system("$avisplit -s $split_size -i $current_file");
231 #print "$avisplit -s $split_size -i $current_file\n";
232 system("mv $current_file-0000 $current_file");
233 system("$mencoder $encode_options_pass1 -o ./$encoded_dir/$current_output_file -idx $current_file 2>>$tempfile");
234 system("$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file 2>>$tempfile");
235 if(! -e "./$encoded_dir/$current_output_file") {
236 print "-E- Errors encountered encoding $current_file\n";
237 system("echo \"-> Errors encountered encoding $current_file\" >> $mencoder_log");
238 system("cat $tempfile >> $mencoder_log");
242 sleep 2; # Give some time to catch an interrupt if needed
246 $current_output_file = "";
247 print "-> Done creating xvid copies\n\n";
251 if(! -f $avimerge) { die "-E- Missing required component: $avimerge\n"; }
252 print "-> Now creating merged videos for each day\n";
254 system("rm -f $merge_log");
255 system("touch $merge_log");
256 system "mkdir -p $merge_dir";
259 print "-> Merging all videos by day from this directory: $video_directory\n";
260 opendir(VIDEODIR, "$video_directory") or die "-E- Could not open: $video_directory";
262 print "-> Merging all videos by day from this directory: $video_directory/$encoded_dir\n";
263 opendir(VIDEODIR, "$video_directory/$encoded_dir") or die "-E- Could not open: $video_directory/$encoded_dir";
267 my @all_files = readdir VIDEODIR;
269 @all_files = sort @all_files;
270 foreach $current_file (@all_files) {
271 next if -d $current_file;
272 next if (!($current_file =~ /\.avi$/));
273 next if (system("$mplayer $mplayer_identify_options $current_file 2>&1 | grep \"Missing video stream\" > /dev/null") == 0);
274 $current_output_file = $current_file;
275 $current_output_file =~ s/_[\-0-9]{8}\.avi$/\.avi/;
276 $current_output_file =~ s/_[\-0-9]{8}$xvid_suffix\.avi$/\.avi/;
277 if($opt_a && $current_output_file =~ /.*?(\d\d\d\d)\.(\d\d)\.(\d\d)/) {
278 $current_output_file = "$1-$2-$3.avi";
279 if( -f "./$merge_dir/$current_output_file" ) {
281 while(-f "./$merge_dir/$1-$2-$3.$num.avi") { $num++; }
282 $current_output_file = "$1-$2-$3.$num.avi";
285 if (!($prev_output_file eq $current_output_file) && !($prev_output_file eq "")) {
286 print " Creating merged video: ./$merge_dir/$prev_output_file_xvid\n";
288 print " $avimerge -c -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge\n";
291 system("$avimerge -c -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge >> $merge_log");
292 sleep 2; # Give some time to catch an interrupt if needed
294 $files_to_merge = "";
297 #store all the file names into one variable
299 $files_to_merge .= " ./$current_file";
300 print " adding ./$current_file to merge list\n";
302 $files_to_merge .= " ./$encoded_dir/$current_file";
303 print " adding ./$encoded_dir/$current_file to merge list\n";
305 $prev_output_file = $current_output_file;
306 $prev_output_file_xvid = $current_output_file;
309 #Now merge the last one
310 if (!($current_output_file eq "")) {
311 print " Creating merged video: ./$merge_dir/$prev_output_file_xvid\n";
313 print " $avimerge -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge\n";
316 system("$avimerge -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge");
318 $files_to_merge = "";
322 print "-> Done creating merged videos for each day\n\n";
325 sub create_playlists {
326 print "-> Now creating playlists of merged videos for each month & year\n";
329 print "-> Creating playlists for each month & year of clips from this directory: $video_directory\n";
330 opendir(VIDEODIR, "$video_directory") or die "-E- could not open: $video_directory";
331 chdir "$video_directory";
333 print "-> Creating playlists for each month & year of clips from this directory: $video_directory/$merge_dir\n";
334 opendir(VIDEODIR, "$video_directory/$merge_dir") or die "-E- could not open: $video_directory/$merge_dir";
335 chdir "$video_directory/$merge_dir";
338 my @all_files = readdir VIDEODIR;
340 @all_files = sort @all_files;
342 print " Removing all existing playlists from the directory\n";
343 system("rm *.$playlist_extension > /dev/null 2>&1");
345 foreach $file (@all_files) {
347 next if ($file !~ /\.avi$/ && $file !~ /\.mpg$/);
348 if($opt_a) { $file =~ /(\d\d\d\d)-(\d\d)-(\d\d)/; $year = $1; $month = $2; }
349 else { $file =~ /(\d\d\d\d).(\d\d).(\d\d)/; $year = $1; $month = $2; }
350 print " Adding $file to $year.$playlist_extension & $year-$month.$playlist_extension\n";
351 system("echo \"$file\" >> $year.$playlist_extension");
352 system("echo \"$file\" >> $year-$month.$playlist_extension");
354 chdir "$video_directory";
357 # Reboot the system if requested
359 print "-> Rebooting the system in 10 seconds... Press CTRL-C to abort...\n";
361 system("sudo /sbin/reboot") if(!$opt_s);