b285f1a4ad07e285aa427ae7186cb2ff42fca47b
[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     $srcext = "";
87     if($srcfile =~ /\.(\w+)$/) { $srcext = $1; }
88     $ext = "mkv";
89     
90     print "Found movie: srcdir: $srcdir srcfile: $srcfile srcext: $srcext dstext: $ext\n" if($opt_v);
91
92     # Throw out files not in the current srcpath
93     if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
94             
95     # Make a note of the month, year, and day this video was taken (from the modification time of the file)
96     $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
97
98     # Get the date taken from the filename
99     if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
100         $year = $1;
101         $month = $2;
102         $day = sprintf("%02d",$3);
103         $monthnum = $month;
104         $monthname = lc($month2monthname{$month});
105     }
106     # Get the date taken from the modification time
107     elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
108         $year = $3;
109         $month = $1;
110         $day = sprintf("%02d",$2);
111         $monthnum = $monthname2month{$month};
112         $monthname = lc($month2monthname{$month});
113     } else {
114         print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
115         next;
116     }
117
118     # We are ready to pick a destination folder to put the merged video in
119     $dstdir = $srcdir;
120     $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
121
122     # Check for duplicate filenames at the destination
123     $newfile = $dstfile . "." . $video_suffix;
124     if(-e "$newfile.$ext") {
125         foreach $i ($video_suffix+1 .. '999') {
126             $newfile = $dstfile . "." . sprintf("%03d",$i);
127             if(! -e "$newfile.$ext") { last; }
128         }
129         $dstfile = $newfile;
130     }
131
132     # Set the name of our unique destination file
133     $dstfile = "$newfile.$ext";
134
135     # You can only merge videos into a single destination that have the same extension/type
136     push(@{$videos{"$srcext"}{"$dstfile"}}, "\"$srcdir/$srcfile\"");
137 }
138
139 # Only merge the videos if there is more than 1 video to merge on a given day for a given ext
140 foreach $ext (sort keys %videos) {
141     foreach $video (sort keys %{$videos{$ext}}) {
142         my $num_videos = $#{$videos{$ext}{$video}} + 1;
143         if($num_videos <= 1) {
144             delete $videos{$ext}{$video};
145             next;
146         }
147     }
148 }
149     
150 # Check for duplicate filenames in the dstfiles being created for other exts
151 foreach $ext (sort keys %videos) {
152     foreach $video (sort keys %{$videos{$ext}}) {
153         # Make sure this video name is not in use as a destination for any other ext
154         foreach $checkext (sort keys %videos) {
155             if($checkext eq $ext) { next; }
156             foreach $checkvideo (sort keys %{$videos{$checkext}}) {
157                 if("$video" eq "$checkvideo") {
158                     if($video =~ /(.*?)\.(\d+)\.(\w+)$/) {
159                         $dstfile = $1;
160                         $dstnum = $2;
161                         $dstext = $3;
162                     }
163                     foreach $i ($dstnum .. '999') {
164                         $newfile = $dstfile . "." . sprintf("%03d",$i);
165                         if("$video" ne "$newfile.$dstext") { last; }
166                     }
167                     $videos{$ext}{"$newfile.$dstext"} = $videos{$ext}{$video};
168                     delete $videos{$ext}{$video};
169                 }
170             }
171         }
172     }
173 }
174
175 # Tell the user which videos we are going to merge
176 foreach $ext (sort keys %videos) {
177     foreach $video (sort keys %{$videos{$ext}}) {
178         foreach $srcfile (@{$videos{$ext}{$video}}) { 
179             print "   merging \"$srcfile\" into \"$video\"\n";
180         }
181     }
182 }
183
184 # Now actually do the merging
185 print "\n";
186 foreach $ext (sort keys %videos) {
187     foreach $video (sort keys %{$videos{$ext}}) {
188
189         my $videos = join(',', @{$videos{$ext}{$video}});
190         
191         # Only merge the videos if there is more than 1
192         #my $num_videos = $#{$videos{$ext}{$video}} + 1;
193         #if($num_videos <= 1) { next; }
194         
195         if($video =~ /(\d+)-(\d+)-(\d+)/) {
196             $year = $1;
197             $month = $2;
198             $day = sprintf("%02d",$3);
199         }
200         
201         my $pwd = `pwd`; chomp($pwd);
202         my $cmd = "";
203         if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
204         $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$video\" -i $videos";
205         if($use_compute_host) { $cmd .= "'"; }
206         if(defined $opt_t) {
207             print "\n-> Creating \"$video\"\n";
208             print "$cmd\n";
209             if(defined $opt_r) { 
210                 foreach $video (@{$videos{$ext}{$video}}) {
211                     print("rm -f $video\n");
212                 }
213             }
214         } else {
215             # Create the merged video
216             my $errno = system("$cmd");
217             $errno = $errno >> 8;
218             if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
219             # Fix the permissions
220             system("chown $owner \"$video\"");
221             system("chgrp $group \"$video\"");
222             system("chmod $mode \"$video\"");
223             # Remove the individual video files
224             if(defined $opt_r) { 
225                 foreach $video (@{$videos{$ext}{$video}}) {
226                     system("rm -f $video");
227                 }
228             }
229         }
230     }
231 }