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
14 $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir); unshift @INC,("$mydir");
15 # Default configuration values
16 require "organize_videos.conf";
17 # Override defaults with local customizations
18 if( -f "$mydir/organize_videos.conf.local") { require "organize_videos.conf.local"; }
20 ####################################################################################################
23 if(! -d $srcpathname) { print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
24 if(! -d $dstpathname) { print "-E- Can't find dstpath: $dstpathname\n"; exit 1; }
25 if(! -x $merge_videos_by_day) { print "-E- Can't find required script: $merge_videos_by_day\n"; exit 1; }
27 my %monthname2month = (
42 my %month2monthname = (
60 print "usage: $0 [-v] [-t] [-r] [-p] [-d <dir>]\n";
61 print " -v verbose; print file being moved (to).\n";
62 print " -t test; print what will happen, but don't do anything\n";
63 print " -n do not copy to dest; do not copy the resultant video files to the destination dir\n";
64 print " -k keep the individual video files that are merged. By default, after a merge, individual video files that were merged are removed\n";
65 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";
66 print " -d <dir> directory to recreate the playlists in. Only needed if -p option is given\n";
72 opendir(my $dh, $dirname) or die "Not a directory";
73 return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0;
76 sub create_playlists {
79 foreach $dstdir (keys %{$dstdirs}) {
80 print "-> Recreating playlists in: $dstdir\n";
81 print "-> Creating playlists for each month & year of clips from this directory: $video_directory\n";
82 opendir(VIDEODIR, "$dstdir") or die "-E- could not open: $dstdir\n";
83 chdir "$dstdir" || die "-E- Unable to change directory to the dstdir: $dstdir\n";
85 my @all_files = readdir VIDEODIR;
87 @all_files = sort @all_files;
89 print " Removing all existing playlists from the directory\n";
90 if(! defined $opt_t) { system("rm *.$playlist_extension > /dev/null 2>&1"); }
92 foreach $file (@all_files) {
94 next if ($file !~ /\.avi$/i && $file !~ /\.mpg$/i && $file !~ /\.mkv$/i && $file !~ /\.3gp$/i && $file !~ /\.mov$/i && $file !~ /\.mts$/i);
95 if($file =~ /(\d\d\d\d)-(\d\d)-(\d\d)/) {
96 $year = $1; $month = $2;
97 print " Adding $file to $year.$playlist_extension & $year-$month.$playlist_extension\n";
98 if(! defined $opt_t) { system("echo \"$file\" >> $year.$playlist_extension"); }
99 if(! defined $opt_t) { system("echo \"$file\" >> $year-$month.$playlist_extension"); }
101 print " Skipping $file since we can't extract the year and month from it\n";
105 # Fix the permissions
106 system("chown $owner \"$dstdir/\"*.$playlist_extension");
107 system("chgrp $group \"$dstdir/\"*.$playlist_extension");
108 system("chmod $mode \"$dstdir/\"*.$playlist_extension");
112 # Sanity checks / Option processing
113 if(defined $opt_h) { usage(); exit 1; }
116 my %dstdirs = ("$opt_d",1);
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_with_mkv`;
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 => "$pid_file", 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_k) { $merge_opts .= "-k "; }
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 # Exit now if we are not supposed to copy the resultant video files to the destination path
156 # Copy the videos over to the destination path
158 chdir "$srcpathname";
159 print "$find_cmd_with_mkv\n" if($opt_v);
160 foreach $file (`$find_cmd_with_mkv`) {
163 $srcdir = dirname($file);
164 $file = basename($file);
166 $ext = $file; $ext =~ s/.*\.(\S+)$/$1/; $ext = lc($ext);
168 print "Found movie: srcdir: $srcdir srcfile: $srcfile ext: $ext\n" if($opt_v);
170 # Throw out files not in the current srcpath
171 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
173 # Make a note of the month, year, and day this video was taken (from the modification time of the file)
174 $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
176 # Get the date taken from the filename
177 if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
180 $day = sprintf("%02d",$3);
182 $monthname = lc($month2monthname{$month});
184 elsif($srcfile =~ /^(\d\d\d\d)(\d\d)(\d\d)/) {
187 $day = sprintf("%02d",$3);
189 $monthname = lc($month2monthname{$month});
191 # Get the date taken from the modification time
192 elsif($date_taken =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
195 $day = sprintf("%02d",$2);
196 $monthnum = $monthname2month{$month};
197 $monthname = lc($month2monthname{$month});
199 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
203 # We are ready to pick a destination folder to put the video in
204 $dstdir = $dstpathname . "/" . $year;
205 $dstdirs{$dstdir} = 1;
206 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
208 # Check for duplicate filenames at the destination
209 $newfile = $dstfile . "." . $video_suffix;
210 if(-e "$newfile.$ext") {
211 foreach $i ($video_suffix+1 .. '999') {
212 $newfile = $dstfile . "." . sprintf("%03d",$i);;
213 if(! -e "$newfile.$ext") { last; }
217 $dstfile = "$newfile.$ext";
221 print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
223 # Make sure the destination directories exist
224 $errno=system("mkdir -p \"$dstdir\"");
225 if($errno) { print "-E- Error creating dstdir: $dstdir\n"; next; }
226 # Perform the move operation from $srcdir/$srcfile -> $dstfile
227 print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
228 # Make sure the dstfile doesn't exist, if it does, don't do the move
229 if(! -f "$dstfile") {
230 $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
231 if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
233 print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n";
235 # Fix the permissions
236 system("chown $owner \"$dstfile\"");
237 system("chgrp $group \"$dstfile\"");
238 system("chmod $mode \"$dstfile\"");
241 # Check to see if there is an empty sub directory to remove
242 if(($srcdir ne $srcpathname) && ($srcpathname ne ".")) {
243 if(is_folder_empty($srcdir)) {
244 print "-> Subdir detected for videos ($srcdir != $srcpathname)\n" if($opt_v);
245 if(! defined $opt_t) {
247 system("rm $tmpdir");
248 system("cp -R \"$srcdir\" $tmpdir > /dev/null 2>/dev/null");
249 system("rm -rf \"$srcdir\"");
250 print "-> Moved empty subdir $srcdir to $tmpdir\n" if($opt_v);
256 # For each destination dir we copied new content into, recreate the playlists
257 create_playlists(\%dstdirs);