Bug fixes
[videoscripts/.git] / merge_videos_by_day
1 #!/usr/bin/perl
2 # Author: Alan J. Pippin
3 # Description: For the given srcpath, merge all the videos that were taken on the same day into a single mkv file
4
5 use File::Copy;
6 use File::Basename;
7 use Getopt::Std;
8 use File::stat;
9 use Time::localtime;
10
11 # Early command line options processing
12 getopts("rh:tvs:");
13 my $srcpathname = $opt_s;
14
15 ####################################################################################################
16 # Configuration parameters - CHANGE THESE TO SUITE YOUR NEEDS
17 my $compute_host = "pippin.pippins.net"; # I need this since this script is run from a virtual machine 
18 my $use_compute_host = 1; # Set to 1 to use a remote compute host to run the mkvmerge command. Set to 0 to use the local host to run it.
19 my $make_mkv = "/naspool/videos/bin/make_mkv"; # Update this to be the path to the make_mkv script
20 my $minage = "+10"; # Files must be older than X minutes to move
21 my $owner = "ajp"; # The owner of the files after they are moved
22 my $group = "pip"; # The group of the files after they are moved
23 my $mode = "664"; # The mode to set on each file after they are moved
24 my $video_suffix = "000"; # What number to start with when adding an incrementing suffix to the end of the video clip to avoid name collisons
25 my $video_title_prefix = "HomeVideos:"; # What text to put on the front of the title for the merged video being created
26 my $find_cmd = "find \"$srcpathname/\" -cmin $minage -iregex \".*\.mov\" -o -iregex \".*\.3gp\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mts\"";
27 ####################################################################################################
28
29 sub usage {
30     print "usage: $0 [-tvrh] -s <srcpath>\n";
31     print "   -s <srcpath>          specify the path to search for videos to merge under\n";
32     print "   -h <compute host>     specify the remote compute host to submit the mkvmerge job to\n";
33     print "   -v                    verbose mode; print extra information about what is being found/merged\n";
34     print "   -t                    test mode; print what will happen, but don't do anything\n";
35     print "   -r                    remove merged video clips; after a merge, remove the individual video files that were merged\n";
36     return 1;
37 }
38 if(defined $opt_h) { usage(); exit 1; }
39
40 # Sanity checks
41 if(! -x $make_mkv) { die "-E- Unable to find required script: make_mkv\n"; }
42 if(! -d $srcpathname) { &usage; print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
43 if(defined $opt_h) { $compute_host = $opt_h; }
44
45 my %monthname2month = (
46                        "Jan" => "01",
47                        "Feb" => "02",
48                        "Mar" => "03",
49                        "Apr" => "04",
50                        "May" => "05",
51                        "Jun" => "06",
52                        "Jul" => "07",
53                        "Aug" => "08",
54                        "Sep" => "09",
55                        "Oct" => "10",
56                        "Nov" => "11",
57                        "Dec" => "12"
58                        );
59
60 my %month2monthname = (
61                        "01" => "Jan",
62                        "02" => "Feb",
63                        "03" => "Mar",
64                        "04" => "Apr",
65                        "05" => "May",
66                        "06" => "Jun",
67                        "07" => "Jul",
68                        "08" => "Aug",
69                        "09" => "Sep",
70                        "10" => "Oct",
71                        "11" => "Nov",
72                        "12" => "Dec"
73                        );
74
75 # Change directories to the srcpath to search for videos to merge
76 print "-> Finding all videos under '$srcpathname' to merge by day\n";
77 my %videos;
78 chdir "$srcpathname";
79 print "$find_cmd\n" if($opt_v);
80 foreach $file (sort `$find_cmd`) {
81
82     chomp($file);
83     $srcdir = dirname($file);
84     $file = basename($file);
85     $srcfile = $file;
86     $ext = "mkv";
87     
88     print "Found movie: srcdir: $srcdir srcfile: $srcfile ext: $ext\n" if($opt_v);
89
90     # Throw out files not in the current srcpath
91     if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
92             
93     # Make a note of the month, year, and day this video was taken (from the modification time of the file)
94     $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
95
96     # Get the date taken from the filename
97     if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
98         $year = $1;
99         $month = $2;
100         $day = sprintf("%02d",$3);
101         $monthnum = $month;
102         $monthname = lc($month2monthname{$month});
103     }
104     # Get the date taken from the modification time
105     elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
106         $year = $3;
107         $month = $1;
108         $day = sprintf("%02d",$2);
109         $monthnum = $monthname2month{$month};
110         $monthname = lc($month2monthname{$month});
111     } else {
112         print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
113         next;
114     }
115
116     # We are ready to pick a destination folder to put the merged video in
117     $dstdir = $srcdir;
118     $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
119
120     # Check for duplicate filenames at the destination
121     $newfile = $dstfile . "." . $video_suffix;
122     if(-e "$newfile.$ext") {
123         foreach $i ($video_suffix+1 .. '999') {
124             $newfile = $dstfile . "." . $i;
125             if(! -e "$newfile.$ext") { last; }
126         }
127         $dstfile = $newfile;
128     }
129     $dstfile = "$newfile.$ext";
130
131     push(@{$videos{"$dstfile"}}, "\"$srcdir/$srcfile\"");
132 }
133
134 # Tell the user which videos we are going to merge
135 foreach $video (sort keys %videos) {
136     # Only merge the videos if there is more than 1
137     my $num_videos = $#{$videos{$video}} + 1;
138     if($num_videos <= 1) { next; }
139
140     foreach $srcfile (@{$videos{$video}}) { 
141         print "   merging \"$srcfile\" into \"$video\"\n";
142     }
143 }
144
145 # Now actually do the merging
146 foreach $video (sort keys %videos) {
147
148     my $videos = join(',', @{$videos{$video}});
149
150     # Only merge the videos if there is more than 1
151     my $num_videos = $#{$videos{$video}} + 1;
152     if($num_videos <= 1) { next; }
153     
154     if($video =~ /(\d+)-(\d+)-(\d+)/) {
155         $year = $1;
156         $month = $2;
157         $day = sprintf("%02d",$3);
158     }
159     
160     my $pwd = `pwd`; chomp($pwd);
161     my $cmd = "";
162     if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
163     $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$video\" -i $videos";
164     if($use_compute_host) { $cmd .= "'"; }
165     if(defined $opt_t) {
166         print "\n-> Creating \"$video\"\n";
167         print "$cmd\n";
168         if(defined $opt_r) { 
169             foreach $video (@{$videos{$video}}) {
170                 print("rm -f $video\n");
171             }
172         }
173     } else {
174         # Create the merged video
175         my $errno = system("$cmd");
176         if($errno > 0) { $errno = $errno - 255; }
177         if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
178         # Fix the permissions
179         system("chown $owner \"$video\"");
180         system("chgrp $group \"$video\"");
181         system("chmod $mode \"$video\"");
182         # Remove the individual video files
183         if(defined $opt_r) { 
184             foreach $video (@{$videos{$video}}) {
185                 system("rm -f $video");
186             }
187         }
188     }
189 }