Added protection about only running the organizing/merging if video files haven't...
[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 $owner = "ajp"; # The owner of the files after they are moved
21 my $group = "pip"; # The group of the files after they are moved
22 my $mode = "664"; # The mode to set on each file after they are moved
23 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
24 my $video_title_prefix = "HomeVideos:"; # What text to put on the front of the title for the merged video being created
25 my $find_cmd = "find \"$srcpathname/\" -iregex \".*\.mov\" -o -iregex \".*\.3gp\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mts\"";
26 ####################################################################################################
27
28 sub usage {
29     print "usage: $0 [-tvrh] -s <srcpath>\n";
30     print "   -s <srcpath>          specify the path to search for videos to merge under\n";
31     print "   -h <compute host>     specify the remote compute host to submit the mkvmerge job to\n";
32     print "   -v                    verbose mode; print extra information about what is being found/merged\n";
33     print "   -t                    test mode; print what will happen, but don't do anything\n";
34     print "   -r                    remove merged video clips; after a merge, remove the individual video files that were merged\n";
35     return 1;
36 }
37 if(defined $opt_h) { usage(); exit 1; }
38
39 # Sanity checks
40 if(! -x $make_mkv) { die "-E- Unable to find required script: make_mkv\n"; }
41 if(! -d $srcpathname) { &usage; print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
42 if(defined $opt_h) { $compute_host = $opt_h; }
43
44 my %monthname2month = (
45                        "Jan" => "01",
46                        "Feb" => "02",
47                        "Mar" => "03",
48                        "Apr" => "04",
49                        "May" => "05",
50                        "Jun" => "06",
51                        "Jul" => "07",
52                        "Aug" => "08",
53                        "Sep" => "09",
54                        "Oct" => "10",
55                        "Nov" => "11",
56                        "Dec" => "12"
57                        );
58
59 my %month2monthname = (
60                        "01" => "Jan",
61                        "02" => "Feb",
62                        "03" => "Mar",
63                        "04" => "Apr",
64                        "05" => "May",
65                        "06" => "Jun",
66                        "07" => "Jul",
67                        "08" => "Aug",
68                        "09" => "Sep",
69                        "10" => "Oct",
70                        "11" => "Nov",
71                        "12" => "Dec"
72                        );
73
74 # Change directories to the srcpath to search for videos to merge
75 print "-> Finding all videos under '$srcpathname' to merge by day\n";
76 my %videos;
77 chdir "$srcpathname";
78 print "$find_cmd\n" if($opt_v);
79 foreach $file (sort `$find_cmd`) {
80
81     chomp($file);
82     $srcdir = dirname($file);
83     $file = basename($file);
84     $srcfile = $file;
85     $srcext = "";
86     if($srcfile =~ /\.(\w+)$/) { $srcext = $1; }
87     $ext = "mkv";
88     
89     print "Found movie: srcdir: $srcdir srcfile: $srcfile srcext: $srcext dstext: $ext\n" if($opt_v);
90
91     # Throw out files not in the current srcpath
92     if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
93             
94     # Make a note of the month, year, and day this video was taken (from the modification time of the file)
95     $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
96
97     # Get the date taken from the filename
98     if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
99         $year = $1;
100         $month = $2;
101         $day = sprintf("%02d",$3);
102         $monthnum = $month;
103         $monthname = lc($month2monthname{$month});
104     }
105     # Get the date taken from the modification time
106     elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
107         $year = $3;
108         $month = $1;
109         $day = sprintf("%02d",$2);
110         $monthnum = $monthname2month{$month};
111         $monthname = lc($month2monthname{$month});
112     } else {
113         print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
114         next;
115     }
116
117     # We are ready to pick a destination folder to put the merged video in
118     $dstdir = $srcdir;
119     $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
120
121     # Check for duplicate filenames at the destination
122     $newfile = $dstfile . "." . $video_suffix;
123     if(-e "$newfile.$ext") {
124         foreach $i ($video_suffix+1 .. '999') {
125             $newfile = $dstfile . "." . sprintf("%03d",$i);
126             if(! -e "$newfile.$ext") { last; }
127         }
128         $dstfile = $newfile;
129     }
130
131     # Set the name of our unique destination file
132     $dstfile = "$newfile.$ext";
133
134     # You can only merge videos into a single destination that have the same extension/type
135     push(@{$videos{"$srcext"}{"$dstfile"}}, "\"$srcdir/$srcfile\"");
136 }
137
138 # Only merge the videos if there is more than 1 video to merge on a given day for a given ext
139 foreach $ext (sort keys %videos) {
140     foreach $video (sort keys %{$videos{$ext}}) {
141         my $num_videos = $#{$videos{$ext}{$video}} + 1;
142         if($num_videos <= 1) {
143             delete $videos{$ext}{$video};
144             next;
145         }
146     }
147 }
148     
149 # Check for duplicate filenames in the dstfiles being created for other exts
150 foreach $ext (sort keys %videos) {
151     foreach $video (sort keys %{$videos{$ext}}) {
152         # Make sure this video name is not in use as a destination for any other ext
153         foreach $checkext (sort keys %videos) {
154             if($checkext eq $ext) { next; }
155             foreach $checkvideo (sort keys %{$videos{$checkext}}) {
156                 if("$video" eq "$checkvideo") {
157                     if($video =~ /(.*?)\.(\d+)\.(\w+)$/) {
158                         $dstfile = $1;
159                         $dstnum = $2;
160                         $dstext = $3;
161                     }
162                     foreach $i ($dstnum .. '999') {
163                         $newfile = $dstfile . "." . sprintf("%03d",$i);
164                         if("$video" ne "$newfile.$dstext") { last; }
165                     }
166                     $videos{$ext}{"$newfile.$dstext"} = $videos{$ext}{$video};
167                     delete $videos{$ext}{$video};
168                 }
169             }
170         }
171     }
172 }
173
174 # Tell the user which videos we are going to merge
175 foreach $ext (sort keys %videos) {
176     foreach $video (sort keys %{$videos{$ext}}) {
177         foreach $srcfile (@{$videos{$ext}{$video}}) { 
178             print "   merging \"$srcfile\" into \"$video\"\n";
179         }
180     }
181 }
182
183 # Now actually do the merging
184 print "\n";
185 foreach $ext (sort keys %videos) {
186     foreach $video (sort keys %{$videos{$ext}}) {
187
188         my $videos = join(',', @{$videos{$ext}{$video}});
189         
190         # Only merge the videos if there is more than 1
191         #my $num_videos = $#{$videos{$ext}{$video}} + 1;
192         #if($num_videos <= 1) { next; }
193         
194         if($video =~ /(\d+)-(\d+)-(\d+)/) {
195             $year = $1;
196             $month = $2;
197             $day = sprintf("%02d",$3);
198         }
199         
200         my $pwd = `pwd`; chomp($pwd);
201         my $cmd = "";
202         if($use_compute_host) { $cmd .= "ssh $compute_host 'cd \"$pwd\";"; }
203         $cmd .= "$make_mkv -t \"$video_title_prefix $year-$month-$day\" -o \"$video\" -i $videos";
204         if($use_compute_host) { $cmd .= "'"; }
205         if(defined $opt_t) {
206             print "\n-> Creating \"$video\"\n";
207             print "$cmd\n";
208             if(defined $opt_r) { 
209                 foreach $video (@{$videos{$ext}{$video}}) {
210                     print("rm -f $video\n");
211                 }
212             }
213         } else {
214             # Create the merged video
215             my $errno = system("$cmd");
216             $errno = $errno >> 8;
217             if($errno) { die "-E- make_mkv encountered some errors with exit code $errno\n"; }
218             # Fix the permissions
219             system("chown $owner \"$video\"");
220             system("chgrp $group \"$video\"");
221             system("chmod $mode \"$video\"");
222             # Remove the individual video files
223             if(defined $opt_r) { 
224                 foreach $video (@{$videos{$ext}{$video}}) {
225                     system("rm -f $video");
226                 }
227             }
228         }
229     }
230 }