a35a983968ed8c032487bec607fe6284bb8d8c3a
[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 500";
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
177 sub encode_videos {
178     if(! -f $mencoder) { die "-E- Missing required component: $mencoder\n"; }
179     print "-> Creating high quality xvid copies of each video clip\n";
180   
181     system("rm -f $mencoder_log");
182     system("touch $mencoder_log");
183     
184     my $current_file = "";
185     my $current_output_file = "";
186     my $prev_output_file_dv = "";
187     my $prev_output_file_xvid = "";
188
189     print "   Processing All Videos in Directory: $video_directory\n";
190     
191     opendir(VIDEODIR, $video_directory) or die "-E- Could not open: $video_directory";
192     my @all_files = readdir VIDEODIR;
193     closedir(VIDEODIR);
194     @all_files = sort @all_files;
195     system "mkdir -p $encoded_dir";
196     
197     foreach $current_file (@all_files) {
198         next if -d $current_file;
199         next if (!($current_file =~ /\.avi$/));
200         $current_output_file = $current_file;
201         $current_output_file =~ s/\.avi$/$xvid_suffix\.avi/;
202         if (-f "./$encoded_dir/$current_output_file") {
203             print "   skipping $current_file - ./$encoded_dir/$current_output_file already exists.\n";
204             next;
205         }
206         print "   $current_file -> ./$encoded_dir/$current_output_file\n";
207         if($opt_v) {
208             print "$mencoder $encode_options_pass1 -o ./$encoded_dir/$current_output_file -idx $current_file\n";
209             print "$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file\n";
210         }
211         if(!$opt_s) {
212             $tempfile = `tempfile`; chomp($tempfile);
213             system("$mencoder $encode_options_pass1 -o ./$encoded_dir/$current_output_file -idx $current_file 2>$tempfile");
214             system("$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file 2>>$tempfile");
215             if(! -e "./$encoded_dir/$current_output_file") {
216                 print "-WARNING- File could not be encoded. Attempting repair through avisplit. Encoding reattempt will follow.\n";
217                 if(! -f $avisplit) { die "-E- Missing required component: $avisplit\n"; }
218                 $split_size = $max_dv_clip_size_in_mb * 10;
219                 system("$avisplit -s $split_size -i $current_file");
220                 #print "$avisplit -s $split_size -i $current_file\n";
221                 system("mv $current_file-0000 $current_file");
222                 system("$mencoder $encode_options_pass1 -o ./$encoded_dir/$current_output_file -idx $current_file 2>>$tempfile");
223                 system("$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file 2>>$tempfile");
224                 if(! -e "./$encoded_dir/$current_output_file") {
225                     print "-E- Errors encountered encoding $current_file\n";
226                     system("echo \"-> Errors encountered encoding $current_file\" >> $mencoder_log");
227                     system("cat $tempfile >> $mencoder_log");
228                 }
229             }
230             unlink "$tempfile";
231             sleep 2; # Give some time to catch an interrupt if needed
232         }
233     }
234     
235     $current_output_file = "";
236     print "-> Done creating xvid copies\n\n";
237 }
238
239 sub merge_videos {
240     if(! -f $avimerge) { die "-E- Missing required component: $avimerge\n"; }
241     print "-> Now creating merged videos for each day\n";
242     
243     system("rm -f $merge_log");
244     system("touch $merge_log");
245     system "mkdir -p $merge_dir";
246     
247     if($merge_only) {
248         print "-> Merging all videos by day from this directory: $video_directory\n";
249         opendir(VIDEODIR, "$video_directory") or die "-E- Could not open: $video_directory";
250     } else {
251         print "-> Merging all videos by day from this directory: $video_directory/$encoded_dir\n";
252         opendir(VIDEODIR, "$video_directory/$encoded_dir") or die "-E- Could not open: $video_directory/$encoded_dir";
253     }
254     print "\n";
255     
256     my @all_files = readdir VIDEODIR;
257     closedir(VIDEODIR);
258     @all_files = sort @all_files;
259     foreach $current_file (@all_files) {
260         next if -d $current_file;
261         next if (!($current_file =~ /\.avi$/));
262         next if (system("$mplayer $mplayer_identify_options $current_file 2>&1 | grep \"Missing video stream\" > /dev/null") == 0);
263         $current_output_file = $current_file;
264         $current_output_file =~ s/_[\-0-9]{8}\.avi$/\.avi/;
265         $current_output_file =~ s/_[\-0-9]{8}$xvid_suffix\.avi$/\.avi/;
266         if($opt_a && $current_output_file =~ /.*?(\d\d\d\d)\.(\d\d)\.(\d\d)/) {
267             $current_output_file = "$1-$2-$3.avi";
268             if( -f "./$merge_dir/$current_output_file" ) {
269                 my $num = 1;
270                 while(-f "./$merge_dir/$1-$2-$3.$num.avi") { $num++; }
271                 $current_output_file = "$1-$2-$3.$num.avi";
272             }
273         }
274         if (!($prev_output_file eq $current_output_file) && !($prev_output_file eq "")) {
275             print "   Creating merged video: ./$merge_dir/$prev_output_file_xvid\n";
276             if($opt_v) {
277                 print "   $avimerge -c -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge\n";
278             }
279             if(!$opt_s) {
280                 system("$avimerge -c -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge >> $merge_log");
281                 sleep 2; # Give some time to catch an interrupt if needed
282             }
283             $files_to_merge = "";
284             print "\n";
285         }
286         #store all the file names into one variable
287         if($merge_only) {           
288             $files_to_merge .= " ./$current_file";
289             print "   adding ./$current_file to merge list\n";
290         } else {
291             $files_to_merge .= " ./$encoded_dir/$current_file";
292             print "   adding ./$encoded_dir/$current_file to merge list\n";
293         }
294         $prev_output_file = $current_output_file;
295         $prev_output_file_xvid = $current_output_file;
296     }
297     
298     #Now merge the last one
299     if (!($current_output_file eq "")) {
300         print "   Creating merged video: ./$merge_dir/$prev_output_file_xvid\n";
301         if($opt_v) {
302             print "   $avimerge -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge\n";
303         }
304         if(!$opt_s) {
305             system("$avimerge -o ./$merge_dir/$prev_output_file_xvid -i $files_to_merge");
306         }
307         $files_to_merge = "";
308         print "\n";
309     }
310     
311     print "-> Done creating merged videos for each day\n\n";
312 }
313
314 sub create_playlists {
315     print "-> Now creating playlists of merged videos for each month & year\n";
316
317     if($playlist_only) {
318         print "-> Creating playlists for each month & year of clips from this directory: $video_directory\n";
319         opendir(VIDEODIR, "$video_directory") or die "-E- could not open: $video_directory";
320         chdir "$video_directory";
321     } else {
322         print "-> Creating playlists for each month & year of clips from this directory: $video_directory/$merge_dir\n";
323         opendir(VIDEODIR, "$video_directory/$merge_dir") or die "-E- could not open: $video_directory/$merge_dir";      
324         chdir "$video_directory/$merge_dir";
325     }
326
327     my @all_files = readdir VIDEODIR;
328     closedir(VIDEODIR);
329     @all_files = sort @all_files;
330
331     print "   Removing all existing playlists from the directory\n";
332     system("rm *.$playlist_extension > /dev/null 2>&1");
333     
334     foreach $file (@all_files) {
335         next if -d $file;
336         next if ($file !~ /\.avi$/ && $file !~ /\.mpg$/);
337         if($opt_a) { $file =~ /(\d\d\d\d)-(\d\d)-(\d\d)/; $year = $1; $month = $2; }
338         else { $file =~ /(\d\d\d\d).(\d\d).(\d\d)/; $year = $1; $month = $2; }
339         print "   Adding $file to $year.$playlist_extension & $year-$month.$playlist_extension\n";
340         system("echo \"$file\" >> $year.$playlist_extension");
341         system("echo \"$file\" >> $year-$month.$playlist_extension");
342     }
343     chdir "$video_directory";
344 }
345
346 # Reboot the system if requested
347 sub reboot_system {
348     print "-> Rebooting the system in 10 seconds... Press CTRL-C to abort...\n";
349     sleep 10;
350     system("sudo /sbin/reboot") if(!$opt_s);
351 }
352