2 # Author: Alan J. Pippin
3 # Description: Find videos from a temporary dropbox location and merge and move them into their final destination.
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 ####################################################################################################
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; }
33 my %monthname2month = (
48 my %month2monthname = (
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";
77 opendir(my $dh, $dirname) or die "Not a directory";
78 return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0;
81 sub create_playlists {
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";
90 my @all_files = readdir VIDEODIR;
92 @all_files = sort @all_files;
94 print " Removing all existing playlists from the directory\n";
95 if(! defined $opt_t) { system("rm *.$playlist_extension > /dev/null 2>&1"); }
97 foreach $file (@all_files) {
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"); }
106 print " Skipping $file since we can't extract the year and month from it\n";
112 # Sanity checks / Option processing
113 if(defined $opt_h) { usage(); exit 1; }
116 my @dstdirs = ("$opt_d");
117 create_playlists(\@dstdirs);
119 die "-E- You must specify the -d <dir> option when using the -p option\n";
124 # Only proceed if there are video files to organize
125 $video_files_found=`$find_cmd`;
126 if(!$video_files_found) { exit 0; }
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; }
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();
140 # Merge videos prior to copying them over to the destination path
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"; }
149 # Copy the videos over to the destination path
151 chdir "$srcpathname";
152 print "$find_cmd\n" if($opt_v);
153 foreach $file (`$find_cmd`) {
156 $srcdir = dirname($file);
157 $file = basename($file);
159 $ext = $file; $ext =~ s/.*\.(\S+)$/$1/; $ext = lc($ext);
161 print "Found movie: srcdir: $srcdir srcfile: $srcfile ext: $ext\n" if($opt_v);
163 # Throw out files not in the current srcpath
164 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
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);
169 # Get the date taken from the filename
170 if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
173 $day = sprintf("%02d",$3);
175 $monthname = lc($month2monthname{$month});
177 # Get the date taken from the modification time
178 elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
181 $day = sprintf("%02d",$2);
182 $monthnum = $monthname2month{$month};
183 $monthname = lc($month2monthname{$month});
185 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
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;
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; }
203 $dstfile = "$newfile.$ext";
206 print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
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; }
218 print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n";
220 # Fix the permissions
221 system("chown $owner \"$dstfile\"");
222 system("chgrp $group \"$dstfile\"");
223 system("chmod $mode \"$dstfile\"");
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) {
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);
241 # For each destination dir we copied new content into, recreate the playlists
242 create_playlists(\@dstdirs);