33c0c0df51eba9813a8bc9d590ed33a7bd570de8
[videoscripts/.git] / make_home_videos.pl
1 #!/usr/bin/perl
2
3 # Prerequisites:
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
10 use Getopt::Std;
11
12 ############################################################
13 # GLOBALS
14 ############################################################
15 $uid = "ajp";
16 $gid = "ajp";
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
19 $bitrate = 4500; 
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";
44
45 $SIG{ALRM} = \&stop_dvgrab;
46 $SIG{INT}  = \&exit_now;
47 $SIG{SUSP} = \&exit_now;
48
49 ############################################################
50 # COMMAND LINE OPTIONS & PROCESSING
51 ############################################################
52 getopts('hecrasvpmb:t:n:d:');
53 if($opt_h) { &usage(); }
54
55 sub usage() {
56     print "\n";
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";
71     print "\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";
76     print "\n";
77     exit 1;
78 }
79
80 if(!$opt_n && !$opt_d) { &usage(); }
81
82 if(!$opt_c && !$opt_e && !$opt_p && $opt_m) {
83     if(!$opt_d) { &usage(); }
84     $opt_m = $opt_d;
85     $merge_only = 1;
86 }
87 else { $merge_only = 0; }
88
89 if(!$opt_c && !$opt_e && !$opt_m && $opt_p) {
90     if(!$opt_d) { &usage(); }
91     $opt_p = $opt_d;
92     $playlist_only = 1;
93 }
94 else { $playlist_only = 0; }
95
96 if(!$opt_d) { 
97     system("mkdir -p $opt_n");
98     chdir "$opt_n";
99     $opt_d = $opt_n;
100 } else {
101     if(! -d $opt_d) { die "-E- Could not cd to $opt_d\n"; }
102     chdir "$opt_d";
103     if(!$opt_n) { $opt_n = $default_tape_name; }
104 }
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);
108
109 print "\n-> Changed working directory to: $video_directory\n";
110
111 ############################################################
112 # MAIN
113 ############################################################
114 print "\n";
115
116 if($opt_c) { &capture_videos(); }
117 if($opt_e) { &encode_videos(); }
118 if($opt_m) { &merge_videos(); }
119 if($opt_p) { &create_playlists(); }
120
121 # Fix the permissions
122 print "-> Fixing permissions: chown -R $uid.$gid\n";
123 system("chown -R $uid.$gid ../$opt_d");
124
125 print "\n";
126 if($opt_r) { &reboot_system(); }
127
128 ############################################################
129 # SUBROUTINES
130 ############################################################
131
132 sub exit_now {
133     print "-E- Interupt signal recieved. Exiting..\n";
134     &stop_dvgrab();
135     exit 1;
136 }
137
138 # A little routine to stop dvgrab. It hangs when it hits the end of a tape.
139 sub stop_dvgrab {
140     my $prog = "dvgrab";
141     my @pids;
142     my $numkilled = 0;
143     @pids = `ps -efw | grep $prog | grep -v grep | grep -v $0`;
144     foreach $pid ( @pids ) {
145         $oldpid = $pid;
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");
150             $numkilled++;
151         }
152     }
153     sleep 5;
154 }
155
156 sub capture_videos {
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";
165     print "\n";
166     if(!$opt_s) {
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");
170     }
171     else { print "   $dvgrab $dvgrab_options --size $max_dv_clip_size_in_mb ${opt_n}_ 2> $dvgrab_log\n"; }
172     alarm(0);
173     sleep 5; # Give some time to catch an interrupt if needed
174     print "-> Done grabbing video from camera\n\n";
175
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;
179     closedir(VIDEODIR);
180     foreach $file (@all_files) {
181       if(($file =~ /$opt_n/) && ($file !~ /$opt_n.*?\.avi/)) { rename("$file","$file.avi"); }
182     }
183 }
184
185 sub encode_videos {
186     if(! -f $mencoder) { die "-E- Missing required component: $mencoder\n"; }
187     print "-> Creating high quality xvid copies of each video clip\n";
188   
189     system("rm -f $mencoder_log");
190     system("touch $mencoder_log");
191     
192     my $current_file = "";
193     my $current_output_file = "";
194     my $prev_output_file_dv = "";
195     my $prev_output_file_xvid = "";
196
197     print "   Processing All Videos in Directory: $video_directory\n";
198     
199     opendir(VIDEODIR, $video_directory) or die "-E- Could not open: $video_directory";
200     my @all_files = readdir VIDEODIR;
201     closedir(VIDEODIR);
202     @all_files = sort @all_files;
203     system "mkdir -p $encoded_dir";
204     
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";
212             next;
213         }
214         print "   $current_file -> ./$encoded_dir/$current_output_file\n";
215         if($opt_v) {
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";
218         }
219         if(!$opt_s) {
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");
236                 }
237             }
238             unlink "$tempfile";
239             sleep 2; # Give some time to catch an interrupt if needed
240         }
241     }
242     
243     $current_output_file = "";
244     print "-> Done creating xvid copies\n\n";
245 }
246
247 sub merge_videos {
248     if(! -f $avimerge) { die "-E- Missing required component: $avimerge\n"; }
249     print "-> Now creating merged videos for each day\n";
250     
251     system("rm -f $merge_log");
252     system("touch $merge_log");
253     system "mkdir -p $merge_dir";
254     
255     if($merge_only) {
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";
258     } else {
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";
261     }
262     print "\n";
263     
264     my @all_files = readdir VIDEODIR;
265     closedir(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" ) {
277                 my $num = 1;
278                 while(-f "./$merge_dir/$1-$2-$3.$num.avi") { $num++; }
279                 $current_output_file = "$1-$2-$3.$num.avi";
280             }
281         }
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";
284             if($opt_v) {
285                 print "   $avimerge -c -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge\n";
286             }
287             if(!$opt_s) {
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
290             }
291             $files_to_merge = "";
292             print "\n";
293         }
294         #store all the file names into one variable
295         if($merge_only) {           
296             $files_to_merge .= " ./$current_file";
297             print "   adding ./$current_file to merge list\n";
298         } else {
299             $files_to_merge .= " ./$encoded_dir/$current_file";
300             print "   adding ./$encoded_dir/$current_file to merge list\n";
301         }
302         $prev_output_file = $current_output_file;
303         $prev_output_file_xvid = $current_output_file;
304     }
305     
306     #Now merge the last one
307     if (!($current_output_file eq "")) {
308         print "   Creating merged video: ./$merge_dir/$prev_output_file_xvid\n";
309         if($opt_v) {
310             print "   $avimerge -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge\n";
311         }
312         if(!$opt_s) {
313             system("$avimerge -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge");
314         }
315         $files_to_merge = "";
316         print "\n";
317     }
318     
319     print "-> Done creating merged videos for each day\n\n";
320 }
321
322 sub create_playlists {
323     print "-> Now creating playlists of merged videos for each month & year\n";
324
325     if($playlist_only) {
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";
329     } else {
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";
333     }
334
335     my @all_files = readdir VIDEODIR;
336     closedir(VIDEODIR);
337     @all_files = sort @all_files;
338
339     print "   Removing all existing playlists from the directory\n";
340     system("rm *.$playlist_extension > /dev/null 2>&1");
341     
342     foreach $file (@all_files) {
343         next if -d $file;
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");
350     }
351     chdir "$video_directory";
352 }
353
354 # Reboot the system if requested
355 sub reboot_system {
356     print "-> Rebooting the system in 10 seconds... Press CTRL-C to abort...\n";
357     sleep 10;
358     system("sudo /sbin/reboot") if(!$opt_s);
359 }
360