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 " -n do not copy to dest; do not copy the resultant video files to the destination dir\n";
70 print " -k keep the individual video files that are merged. By default, after a merge, individual video files that were merged are removed\n";
71 print " -p only recreate video playlists, do not process any video files. Do this for each year & month of video clips in the directory specified by -d <dir>.\n";
72 print " -d <dir> directory to recreate the playlists in. Only needed if -p option is given\n";
78 opendir(my $dh, $dirname) or die "Not a directory";
79 return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0;
82 sub create_playlists {
85 foreach $dstdir (@{$dstdirs}) {
86 print "-> Recreating playlists in: $dstdir\n";
87 print "-> Creating playlists for each month & year of clips from this directory: $video_directory\n";
88 opendir(VIDEODIR, "$dstdir") or die "-E- could not open: $dstdir\n";
89 chdir "$dstdir" || die "-E- Unable to change directory to the dstdir: $dstdir\n";
91 my @all_files = readdir VIDEODIR;
93 @all_files = sort @all_files;
95 print " Removing all existing playlists from the directory\n";
96 if(! defined $opt_t) { system("rm *.$playlist_extension > /dev/null 2>&1"); }
98 foreach $file (@all_files) {
100 next if ($file !~ /\.avi$/i && $file !~ /\.mpg$/i && $file !~ /\.mkv$/i && $file !~ /\.3gp$/i && $file !~ /\.mov$/i && $file !~ /\.mts$/i);
101 if($file =~ /(\d\d\d\d)-(\d\d)-(\d\d)/) {
102 $year = $1; $month = $2;
103 print " Adding $file to $year.$playlist_extension & $year-$month.$playlist_extension\n";
104 if(! defined $opt_t) { system("echo \"$file\" >> $year.$playlist_extension"); }
105 if(! defined $opt_t) { system("echo \"$file\" >> $year-$month.$playlist_extension"); }
107 print " Skipping $file since we can't extract the year and month from it\n";
113 # Sanity checks / Option processing
114 if(defined $opt_h) { usage(); exit 1; }
117 my @dstdirs = ("$opt_d");
118 create_playlists(\@dstdirs);
120 die "-E- You must specify the -d <dir> option when using the -p option\n";
125 # Only proceed if there are video files to organize
126 $video_files_found=`$find_cmd`;
127 if(!$video_files_found) { exit 0; }
129 # Only proceed if no files have changed in the past $cmin minutes
130 $changed_files_found=`$find_changed_cmd`;
131 if($changed_files_found) { exit 0; }
133 # Only one instance of this script running at a time
134 my $pidfile = File::Pid->new({file => "/tmp/organize_videos.pid"});
135 exit if $pidfile->running();
141 # Merge videos prior to copying them over to the destination path
144 if(defined $opt_t) { $merge_opts .= "-t "; }
145 if(defined $opt_k) { $merge_opts .= "-k "; }
146 $errno=system("$merge_videos_by_day -q -s \"$srcpathname\" $merge_opts");
147 $errno = $errno >> 8;
148 if($errno) { die "-E- $merge_videos_by_day encountered some errors with exit code $errno\n"; }
150 # Exit now if we are not supposed to copy the resultant video files to the destination path
157 # Copy the videos over to the destination path
159 chdir "$srcpathname";
160 print "$find_cmd\n" if($opt_v);
161 foreach $file (`$find_cmd`) {
164 $srcdir = dirname($file);
165 $file = basename($file);
167 $ext = $file; $ext =~ s/.*\.(\S+)$/$1/; $ext = lc($ext);
169 print "Found movie: srcdir: $srcdir srcfile: $srcfile ext: $ext\n" if($opt_v);
171 # Throw out files not in the current srcpath
172 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
174 # Make a note of the month, year, and day this video was taken (from the modification time of the file)
175 $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
177 # Get the date taken from the filename
178 if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
181 $day = sprintf("%02d",$3);
183 $monthname = lc($month2monthname{$month});
185 # Get the date taken from the modification time
186 elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
189 $day = sprintf("%02d",$2);
190 $monthnum = $monthname2month{$month};
191 $monthname = lc($month2monthname{$month});
193 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
197 # We are ready to pick a destination folder to put the video in
198 $dstdir = $dstpathname . "/" . $year;
199 push(@dstdirs,$dstdir);
200 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
202 # Check for duplicate filenames at the destination
203 $newfile = $dstfile . "." . $video_suffix;
204 if(-e "$newfile.$ext") {
205 foreach $i ($video_suffix+1 .. '999') {
206 $newfile = $dstfile . "." . sprintf("%03d",$i);;
207 if(! -e "$newfile.$ext") { last; }
211 $dstfile = "$newfile.$ext";
215 print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
217 # Make sure the destination directories exist
218 $errno=system("mkdir -p \"$dstdir\"");
219 if($errno) { print "-E- Error creating dstdir: $dstdir\n"; next; }
220 # Perform the move operation from $srcdir/$srcfile -> $dstfile
221 print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
222 # Make sure the dstfile doesn't exist, if it does, don't do the move
223 if(! -f "$dstfile") {
224 $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
225 if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
227 print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n";
229 # Fix the permissions
230 system("chown $owner \"$dstfile\"");
231 system("chgrp $group \"$dstfile\"");
232 system("chmod $mode \"$dstfile\"");
235 # Check to see if there is an empty sub directory to remove
236 if(($srcdir ne $srcpathname) && ($srcpathname ne ".")) {
237 if(is_folder_empty($srcdir)) {
238 print "-> Subdir detected for videos ($srcdir != $srcpathname)\n" if($opt_v);
239 if(! defined $opt_t) {
241 system("rm $tmpdir");
242 system("cp -R \"$srcdir\" $tmpdir > /dev/null 2>/dev/null");
243 system("rm -rf \"$srcdir\"");
244 print "-> Moved empty subdir $srcdir to $tmpdir\n" if($opt_v);
250 # For each destination dir we copied new content into, recreate the playlists
251 create_playlists(\@dstdirs);