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