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
12 ############################################################
14 ############################################################
17 #bitrate=3000 is a good choice to allow 2 90 minutes tapes to fit on one DVD
18 #bitrate=4500 is a good choice to allow 2 60 minute tapes to fit on one DVD
20 $encoded_dir = "xvid_clips";
21 $merge_dir = "daily_clips";
22 $default_tape_name = "home_video";
23 $playlist_extension = "pls";
24 $dvgrab = "/usr/local/bin/dvgrab";
25 $mencoder = "/usr/bin/mencoder";
26 $mplayer = "/usr/bin/mplayer";
27 $avimerge = "/usr/bin/avimerge";
28 $avisplit = "/usr/bin/avisplit";
29 $xvid_suffix = "_xvid$bitrate";
30 $dvgrab_log = "../dvgrab.log";
31 $mencoder_log = "../encoding_errors.log";
32 $merge_log = "../avimerge.log";
33 $max_dv_clip_size_in_mb = "1023"; #I had to keep it at this size to avoid errors until avisplit fixed them.
34 #$max_dv_clip_size_in_mb = "22000"; #avisplit still doesn't seem to do things right for large clips
35 # max_tape_grab_time=7200 Allows Max time of 2 hours to allow a tape to be dumped (good for 90 min tape)
36 # max_tape_grab_time=5400 Allows Max time of 1.5 hours to allow a tape to be dumped (good for 60 min tape)
37 $max_tape_grab_time = 5400;
38 $dvgrab_options = "--autosplit --timestamp --format dv2 --opendml --buffers 2048 --rewind";
39 $encode_options_pass1 = "-mc 0 -oac mp3lame -lameopts cbr:cbr=128 -ffourcc XVID -fps 29.97 -ovc lavc ";
40 $encode_options_pass1.= "-lavcopts vcodec=mpeg4:vhq:vbitrate=$bitrate:keyint=15:vpass=1 -vf pp=ci";
41 $encode_options_pass2 = "-mc 0 -oac mp3lame -lameopts cbr:cbr=128 -ffourcc XVID -fps 29.97 -ovc lavc ";
42 $encode_options_pass2.= "-lavcopts vcodec=mpeg4:vhq:vbitrate=$bitrate:keyint=15:vpass=2 -vf pp=ci";
43 $mplayer_identify_options = "-vo null -ao null -identify -frames 0";
45 $SIG{ALRM} = \&stop_dvgrab;
46 $SIG{INT} = \&exit_now;
47 $SIG{SUSP} = \&exit_now;
49 ############################################################
50 # COMMAND LINE OPTIONS & PROCESSING
51 ############################################################
52 getopts('hecrasvpmb:t:n:d:');
53 if($opt_h) { &usage(); }
57 print "Usage: $0 <options>\n";
58 print " -e Encode captured raw video from camera.\n";
59 print " -c Capture raw video from camera.\n";
60 print " -m Merge videos clips found in the directory specified by -d <dir>. \n";
61 print " -p Create video playlists for each year & month of video clips in the directory specified by -d <dir>.\n";
62 print " -n <tape> Name of the tape to prepend to the front of each videoclip filename.\n";
63 print " -d <dir> Directory we want to encode. If not specified, <dir>=<tape>\n";
64 print " -a Use abbreviated names for merged video clips: yyyy-mm-dd.avi vs. <tape>.yyyy.mm.dd.avi\n";
65 print " -s Simulate Mode. Don't encode, just tell me what you would do.\n";
66 print " -h Show this usage\n";
67 print " -v Show verbosity\n";
68 print " -t <min> Time in minutes to grab from the camera (default "; print $max_tape_grab_time/60; print ")\n";
69 print " -b <mb> Max size each clip grabbed should be in MB (default $max_dv_clip_size_in_mb)\n";
70 print " -r Reboot the system once the script completes.\n";
72 print "File Structure:\n";
73 print " <dir>/<tape>*.avi MiniDV Raw AVI files\n";
74 print " <dir>/$encoded_dir/<tape>*.avi XVID compressed versions of the Raw AVI files\n";
75 print " <dir>/$merge_dir/<tape>*.avi All XVID clips taken on each day combined together\n";
80 if(!$opt_n && !$opt_d) { &usage(); }
82 if(!$opt_c && !$opt_e && !$opt_p && $opt_m) {
83 if(!$opt_d) { &usage(); }
87 else { $merge_only = 0; }
89 if(!$opt_c && !$opt_e && !$opt_m && $opt_p) {
90 if(!$opt_d) { &usage(); }
94 else { $playlist_only = 0; }
97 system("mkdir -p $opt_n");
101 if(! -d $opt_d) { die "-E- Could not cd to $opt_d\n"; }
103 if(!$opt_n) { $opt_n = $default_tape_name; }
105 if($opt_t) { $max_tape_grab_time = $opt_t * 60; }
106 if($opt_b) { $max_dv_clip_size_in_mb = $opt_b; }
107 $video_directory = qx[pwd]; chomp($video_directory);
109 print "\n-> Changed working directory to: $video_directory\n";
111 ############################################################
113 ############################################################
116 if($opt_c) { &capture_videos(); }
117 if($opt_e) { &encode_videos(); }
118 if($opt_m) { &merge_videos(); }
119 if($opt_p) { &create_playlists(); }
121 # Fix the permissions
122 print "-> Fixing permissions: chown -R $uid.$gid\n";
123 system("chown -R $uid.$gid ../$opt_d");
126 if($opt_r) { &reboot_system(); }
128 ############################################################
130 ############################################################
133 print "-E- Interupt signal recieved. Exiting..\n";
138 # A little routine to stop dvgrab. It hangs when it hits the end of a tape.
143 @pids = `ps -efw | grep $prog | grep -v grep | grep -v $0`;
144 foreach $pid ( @pids ) {
146 if( $pid =~ s/^\S*\s*(\d*).*$prog.*\n$/$1/ ) {
147 if($pid eq "") { next; }
148 print "$date : -I- Stopped dvgrab Process id: \"$pid\"\n";
149 system("kill -9 $pid");
157 if(! -f $dvgrab ){ die "-E- Missing required component: $dvgrab\n"; }
158 alarm($max_tape_grab_time);
159 $minutes = $max_tape_grab_time / 60;
160 print "-> Max tape grab time set to $minutes minutes\n";
161 print "-> Grabbing raw video from the camera\n";
162 print " Sending the status of grabbing the raw video to $dvgrab_log\n";
163 print " Press CTRL-C once to stop the video grab and move onto the next step\n";
164 print " Press CTRL-C three times to stop the video grab and exit\n";
167 system("rm -f $dvgrab_log; touch $dvgrab_log; tail -f $dvgrab_log &");
168 print "$dvgrab $dvgrab_options --size $max_dv_clip_size_in_mb ${opt_n}_\n";
169 system("$dvgrab $dvgrab_options --size $max_dv_clip_size_in_mb ${opt_n}_ 2> $dvgrab_log");
171 else { print " $dvgrab $dvgrab_options --size $max_dv_clip_size_in_mb ${opt_n}_ 2> $dvgrab_log\n"; }
173 sleep 5; # Give some time to catch an interrupt if needed
174 print "-> Done grabbing video from camera\n\n";
176 # Now see if we need to add a ".avi" extension to the video files dvgrab created
177 opendir(VIDEODIR, $video_directory) or die "-E- Could not open: $video_directory";
178 my @all_files = readdir VIDEODIR;
180 foreach $file (@all_files) {
181 if(($file =~ /$opt_n/) && ($file !~ /$opt_n.*?\.avi/)) { rename("$file","$file.avi"); }
186 if(! -f $mencoder) { die "-E- Missing required component: $mencoder\n"; }
187 print "-> Creating high quality xvid copies of each video clip\n";
189 system("rm -f $mencoder_log");
190 system("touch $mencoder_log");
192 my $current_file = "";
193 my $current_output_file = "";
194 my $prev_output_file_dv = "";
195 my $prev_output_file_xvid = "";
197 print " Processing All Videos in Directory: $video_directory\n";
199 opendir(VIDEODIR, $video_directory) or die "-E- Could not open: $video_directory";
200 my @all_files = readdir VIDEODIR;
202 @all_files = sort @all_files;
203 system "mkdir -p $encoded_dir";
205 foreach $current_file (@all_files) {
206 next if -d $current_file;
207 next if (!($current_file =~ /\.avi$/));
208 $current_output_file = $current_file;
209 $current_output_file =~ s/\.avi$/$xvid_suffix\.avi/;
210 if (-f "./$encoded_dir/$current_output_file") {
211 print " skipping $current_file - ./$encoded_dir/$current_output_file already exists.\n";
214 print " $current_file -> ./$encoded_dir/$current_output_file\n";
216 print "$mencoder $encode_options_pass1 -o ./$encoded_dir/$current_output_file -idx $current_file\n";
217 print "$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file\n";
220 $tempfile = `tempfile`; chomp($tempfile);
221 system("$mencoder $encode_options_pass1 -o ./$encoded_dir/$current_output_file -idx $current_file 2>$tempfile");
222 system("$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file 2>>$tempfile");
223 if(! -e "./$encoded_dir/$current_output_file") {
224 print "-WARNING- File could not be encoded. Attempting repair through avisplit. Encoding reattempt will follow.\n";
225 if(! -f $avisplit) { die "-E- Missing required component: $avisplit\n"; }
226 $split_size = $max_dv_clip_size_in_mb * 10;
227 system("$avisplit -s $split_size -i $current_file");
228 #print "$avisplit -s $split_size -i $current_file\n";
229 system("mv $current_file-0000 $current_file");
230 system("$mencoder $encode_options_pass1 -o ./$encoded_dir/$current_output_file -idx $current_file 2>>$tempfile");
231 system("$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file 2>>$tempfile");
232 if(! -e "./$encoded_dir/$current_output_file") {
233 print "-E- Errors encountered encoding $current_file\n";
234 system("echo \"-> Errors encountered encoding $current_file\" >> $mencoder_log");
235 system("cat $tempfile >> $mencoder_log");
239 sleep 2; # Give some time to catch an interrupt if needed
243 $current_output_file = "";
244 print "-> Done creating xvid copies\n\n";
248 if(! -f $avimerge) { die "-E- Missing required component: $avimerge\n"; }
249 print "-> Now creating merged videos for each day\n";
251 system("rm -f $merge_log");
252 system("touch $merge_log");
253 system "mkdir -p $merge_dir";
256 print "-> Merging all videos by day from this directory: $video_directory\n";
257 opendir(VIDEODIR, "$video_directory") or die "-E- Could not open: $video_directory";
259 print "-> Merging all videos by day from this directory: $video_directory/$encoded_dir\n";
260 opendir(VIDEODIR, "$video_directory/$encoded_dir") or die "-E- Could not open: $video_directory/$encoded_dir";
264 my @all_files = readdir VIDEODIR;
266 @all_files = sort @all_files;
267 foreach $current_file (@all_files) {
268 next if -d $current_file;
269 next if (!($current_file =~ /\.avi$/));
270 next if (system("$mplayer $mplayer_identify_options $current_file 2>&1 | grep \"Missing video stream\" > /dev/null") == 0);
271 $current_output_file = $current_file;
272 $current_output_file =~ s/_[\-0-9]{8}\.avi$/\.avi/;
273 $current_output_file =~ s/_[\-0-9]{8}$xvid_suffix\.avi$/\.avi/;
274 if($opt_a && $current_output_file =~ /.*?(\d\d\d\d)\.(\d\d)\.(\d\d)/) {
275 $current_output_file = "$1-$2-$3.avi";
276 if( -f "./$merge_dir/$current_output_file" ) {
278 while(-f "./$merge_dir/$1-$2-$3.$num.avi") { $num++; }
279 $current_output_file = "$1-$2-$3.$num.avi";
282 if (!($prev_output_file eq $current_output_file) && !($prev_output_file eq "")) {
283 print " Creating merged video: ./$merge_dir/$prev_output_file_xvid\n";
285 print " $avimerge -c -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge\n";
288 system("$avimerge -c -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge >> $merge_log");
289 sleep 2; # Give some time to catch an interrupt if needed
291 $files_to_merge = "";
294 #store all the file names into one variable
296 $files_to_merge .= " ./$current_file";
297 print " adding ./$current_file to merge list\n";
299 $files_to_merge .= " ./$encoded_dir/$current_file";
300 print " adding ./$encoded_dir/$current_file to merge list\n";
302 $prev_output_file = $current_output_file;
303 $prev_output_file_xvid = $current_output_file;
306 #Now merge the last one
307 if (!($current_output_file eq "")) {
308 print " Creating merged video: ./$merge_dir/$prev_output_file_xvid\n";
310 print " $avimerge -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge\n";
313 system("$avimerge -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge");
315 $files_to_merge = "";
319 print "-> Done creating merged videos for each day\n\n";
322 sub create_playlists {
323 print "-> Now creating playlists of merged videos for each month & year\n";
326 print "-> Creating playlists for each month & year of clips from this directory: $video_directory\n";
327 opendir(VIDEODIR, "$video_directory") or die "-E- could not open: $video_directory";
328 chdir "$video_directory";
330 print "-> Creating playlists for each month & year of clips from this directory: $video_directory/$merge_dir\n";
331 opendir(VIDEODIR, "$video_directory/$merge_dir") or die "-E- could not open: $video_directory/$merge_dir";
332 chdir "$video_directory/$merge_dir";
335 my @all_files = readdir VIDEODIR;
337 @all_files = sort @all_files;
339 print " Removing all existing playlists from the directory\n";
340 system("rm *.$playlist_extension > /dev/null 2>&1");
342 foreach $file (@all_files) {
344 next if ($file !~ /\.avi$/ && $file !~ /\.mpg$/);
345 if($opt_a) { $file =~ /(\d\d\d\d)-(\d\d)-(\d\d)/; $year = $1; $month = $2; }
346 else { $file =~ /(\d\d\d\d).(\d\d).(\d\d)/; $year = $1; $month = $2; }
347 print " Adding $file to $year.$playlist_extension & $year-$month.$playlist_extension\n";
348 system("echo \"$file\" >> $year.$playlist_extension");
349 system("echo \"$file\" >> $year-$month.$playlist_extension");
351 chdir "$video_directory";
354 # Reboot the system if requested
356 print "-> Rebooting the system in 10 seconds... Press CTRL-C to abort...\n";
358 system("sudo /sbin/reboot") if(!$opt_s);