ef624b7ee247f3d56b61f94ceecf858d20429c68
[videoscripts/.git] / organize_videos
1 #!/usr/bin/perl
2 # Author: Alan J. Pippin
3 # Description: Find videos from a temporary dropbox location and merge and move them into their final destination.
4
5 use File::Copy;
6 use File::Basename;
7 use Getopt::Std;
8 use File::stat;
9 use Time::localtime;
10 use File::Pid;
11
12 ####################################################################################################
13 # Configuration parameters - CHANGE THESE TO SUITE YOUR NEEDS
14 my $srcpathname = "/naspool/pictures/New Photos"; # Path to look for videos to move from
15 my $dstpathname = "/naspool/videos/HomeVideos"; # Path to move the videos to
16 my $merge_videos_by_day = "/naspool/videos/bin/merge_videos_by_day";
17 my $minage = "+15"; # Video file creation dates must not have changed in the last X minutes to process any of the video files
18 my $owner = "ajp"; # The owner of the files after they are moved
19 my $group = "pip"; # The group of the files after they are moved
20 my $mode = "664"; # The mode to set on each file after they are moved
21 my $playlist_extension = "pls"; # The extension to use when creating playlist files
22 my $video_suffix = "000"; # What number to start with when adding an incrementing suffix to the end of the final video clip to avoid name collisions
23 my $movie_file_ext = "-iregex \".*\.mov\" -o -iregex \".*\.3gp\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mts\" -o -iregex \".*\.mkv\"";
24 my $find_changed_cmd = "find  \"$srcpathname/\" -not -cmin $minage -a \\( $movie_file_ext \\)";
25 my $find_cmd = "find \"$srcpathname/\" $movie_file_ext";
26 ####################################################################################################
27
28 # Sanity check
29 if(! -d $srcpathname) { print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
30 if(! -d $dstpathname) { print "-E- Can't find dstpath: $dstpathname\n"; exit 1; }
31 if(! -x $merge_videos_by_day) { print "-E- Can't find required script: $merge_videos_by_day\n"; exit 1; }
32
33 my %monthname2month = (
34                        "Jan" => "01",
35                        "Feb" => "02",
36                        "Mar" => "03",
37                        "Apr" => "04",
38                        "May" => "05",
39                        "Jun" => "06",
40                        "Jul" => "07",
41                        "Aug" => "08",
42                        "Sep" => "09",
43                        "Oct" => "10",
44                        "Nov" => "11",
45                        "Dec" => "12"
46                        );
47
48 my %month2monthname = (
49                        "01" => "Jan",
50                        "02" => "Feb",
51                        "03" => "Mar",
52                        "04" => "Apr",
53                        "05" => "May",
54                        "06" => "Jun",
55                        "07" => "Jul",
56                        "08" => "Aug",
57                        "09" => "Sep",
58                        "10" => "Oct",
59                        "11" => "Nov",
60                        "12" => "Dec"
61                        );
62
63 getopts("htvrpd:");
64
65 sub usage {
66     print "usage: $0 [-v] [-t] [-r] [-p] [-d <dir>]\n";
67     print "   -v = verbose; print file being moved (to).\n";
68     print "   -t = test; print what will happen, but don't do anything\n";
69     print "   -r = remove merged video clips; after a merge, remove the individual video files that were merged\n";
70     print "   -p = Only recreate video playlists. Do this for each year & month of video clips in the directory specified by -d <dir>.\n";
71     print "   -d <dir> = Directory to recreate the playlists in. Only needed if -p option is given\n";
72     return 1;
73 }
74
75 sub is_folder_empty {
76     my $dirname = shift;
77     opendir(my $dh, $dirname) or die "Not a directory";
78     return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0;
79 }
80
81 sub create_playlists {
82     my ($dstdirs) = @_;
83     
84     foreach $dstdir (@{$dstdirs}) {
85         print "-> Recreating playlists in: $dstdir\n";
86         print "-> Creating playlists for each month & year of clips from this directory: $video_directory\n";
87         opendir(VIDEODIR, "$dstdir") or die "-E- could not open: $dstdir\n";
88         chdir "$dstdir" || die "-E- Unable to change directory to the dstdir: $dstdir\n";
89         
90         my @all_files = readdir VIDEODIR;
91         closedir(VIDEODIR);
92         @all_files = sort @all_files;
93
94         print "   Removing all existing playlists from the directory\n";
95         if(! defined $opt_t) { system("rm *.$playlist_extension > /dev/null 2>&1"); }
96     
97         foreach $file (@all_files) {
98             next if -d $file;
99             next if ($file !~ /\.avi$/i && $file !~ /\.mpg$/i && $file !~ /\.mkv$/i && $file !~ /\.3gp$/i && $file !~ /\.mov$/i && $file !~ /\.mts$/i);
100             if($file =~ /(\d\d\d\d)-(\d\d)-(\d\d)/) {
101                 $year = $1; $month = $2;
102                 print "   Adding $file to $year.$playlist_extension & $year-$month.$playlist_extension\n";
103                 if(! defined $opt_t) { system("echo \"$file\" >> $year.$playlist_extension"); }
104                 if(! defined $opt_t) { system("echo \"$file\" >> $year-$month.$playlist_extension"); }
105             } else {
106                 print "   Skipping $file since we can't extract the year and month from it\n"; 
107             }
108         }
109     }
110 }
111
112 # Sanity checks / Option processing
113 if(defined $opt_h) { usage(); exit 1; }
114 if(defined $opt_p) {
115     if(defined $opt_d) {
116         my @dstdirs = ("$opt_d"); 
117         create_playlists(\@dstdirs);
118     } else {
119         die "-E- You must specify the -d <dir> option when using the -p option\n"; 
120     }
121     exit 0;
122 }
123
124 # Only proceed if there are video files to organize
125 $video_files_found=`$find_cmd`;
126 if(!$video_files_found) { exit 0; }
127
128 # Only proceed if no files have changed in the past $cmin minutes
129 $changed_files_found=`$find_changed_cmd`;
130 if($changed_files_found) { exit 0; }
131
132 # Only one instance of this script running at a time
133 my $pidfile = File::Pid->new({file => "/tmp/organize_videos.pid"});
134 exit if $pidfile->running();
135 $pidfile->write();
136
137 # Print the date
138 system("date");
139
140 # Merge videos prior to copying them over to the destination path
141 my $errno = 0;
142 my $merge_opts = "";
143 if(defined $opt_t) { $merge_opts .= "-t "; }
144 if(defined $opt_r) { $merge_opts .= "-r "; }
145 $errno=system("$merge_videos_by_day -q -s \"$srcpathname\" $merge_opts");
146 $errno = $errno >> 8;
147 if($errno) { die "-E- $merge_videos_by_day encountered some errors with exit code $errno\n"; }
148
149 # Copy the videos over to the destination path
150 my @dstdirs;
151 chdir "$srcpathname";
152 print "$find_cmd\n" if($opt_v);
153 foreach $file (`$find_cmd`) {
154
155     chomp($file);
156     $srcdir = dirname($file);
157     $file = basename($file);
158     $srcfile = $file;
159     $ext = $file; $ext =~ s/.*\.(\S+)$/$1/; $ext = lc($ext);
160     
161     print "Found movie: srcdir: $srcdir srcfile: $srcfile ext: $ext\n" if($opt_v);
162
163     # Throw out files not in the current srcpath
164     if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
165         
166     # Make a note of the month, year, and day this video was taken (from the modification time of the file)
167     $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
168
169     # Get the date taken from the filename
170     if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
171         $year = $1;
172         $month = $2;
173         $day = sprintf("%02d",$3);
174         $monthnum = $month;
175         $monthname = lc($month2monthname{$month});
176     }
177     # Get the date taken from the modification time
178     elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
179         $year = $3;
180         $month = $1;
181         $day = sprintf("%02d",$2);
182         $monthnum = $monthname2month{$month};
183         $monthname = lc($month2monthname{$month});
184     } else {
185         print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
186         next;
187     }
188
189     # We are ready to pick a destination folder to put the picture in
190     $dstdir = $dstpathname . "/" . $year;
191     push(@dstdirs,$dstdir);
192     $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
193
194     # Check for duplicate filenames at the destination
195     $newfile = $dstfile . "." . $video_suffix;
196     if(-e "$newfile.$ext") {
197         foreach $i ($video_suffix+1 .. '999') {
198             $newfile = $dstfile . "." . sprintf("%03d",$i);;
199             if(! -e "$newfile.$ext") { last; }
200         }
201         $dstfile = $newfile;
202     }
203     $dstfile = "$newfile.$ext";
204
205     if(defined $opt_t) {
206         print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
207     } else {
208         # Make sure the destination directories exist
209         $errno=system("mkdir -p \"$dstdir\"");
210         if($errno) { print "-E- Error creating dstdir: $dstdir\n"; next; }
211         # Perform the move operation from $srcdir/$srcfile -> $dstfile
212         print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
213         # Make sure the dstfile doesn't exist, if it does, don't do the move
214         if(! -f "$dstfile") {
215             $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
216             if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
217         } else {
218             print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n";
219         }
220         # Fix the permissions
221         system("chown $owner \"$dstfile\"");
222         system("chgrp $group \"$dstfile\"");
223         system("chmod $mode \"$dstfile\"");
224     }
225
226     # Check to see if there is an empty sub directory to remove
227     if(($srcdir ne $srcpathname) && ($srcpathname ne ".")) { 
228       if(is_folder_empty($srcdir)) { 
229           print "-> Subdir detected for videos ($srcdir != $srcpathname)\n" if($opt_v);
230           if(! defined $opt_t) { 
231               $tmpdir=`tempfile`;
232               system("rm $tmpdir");
233               system("cp -R \"$srcdir\" $tmpdir > /dev/null 2>/dev/null");
234               system("rm -rf \"$srcdir\"");
235               print "-> Moved empty subdir $srcdir to $tmpdir\n" if($opt_v);
236           }
237       }
238     }
239 }
240
241 # For each destination dir we copied new content into, recreate the playlists
242 create_playlists(\@dstdirs);
243
244 $pidfile->remove();
245
246 print "\n\n";