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] [-f] [-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 " -f force it to run by ignoring the \$minage setting in organize_videos.conf\n";
64 print " -n do not copy to dest; do not copy the resultant video files to the destination dir\n";
65 print " -k keep the individual video files that are merged. By default, after a merge, individual video files that were merged are removed\n";
66 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";
67 print " -d <dir> directory to recreate the playlists in. Only needed if -p option is given\n";
70 $SIG{'INT'} = sub {die "-E- Killed by CTRL-C\n"};
74 opendir(my $dh, $dirname) or die "Not a directory";
75 return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0;
78 sub create_playlists {
81 foreach $dstdir (keys %{$dstdirs}) {
82 print "-> Recreating playlists in: $dstdir\n";
83 print "-> Creating playlists for each month & year of clips from this directory: $video_directory\n";
84 opendir(VIDEODIR, "$dstdir") or die "-E- could not open: $dstdir\n";
85 chdir "$dstdir" || die "-E- Unable to change directory to the dstdir: $dstdir\n";
87 my @all_files = readdir VIDEODIR;
89 @all_files = sort @all_files;
91 print " Removing all existing playlists from the directory\n";
92 if(! defined $opt_t) { system("rm *.$playlist_extension > /dev/null 2>&1"); }
94 foreach $file (@all_files) {
96 next if ($file !~ /\.avi$/i && $file !~ /\.mpg$/i && $file !~ /\.mkv$/i && $file !~ /\.3gp$/i && $file !~ /\.mov$/i && $file !~ /\.mts$/i);
97 if($file =~ /(\d\d\d\d)-(\d\d)-(\d\d)/) {
98 $year = $1; $month = $2;
99 print " Adding $file to $year.$playlist_extension & $year-$month.$playlist_extension\n";
100 if(! defined $opt_t) { system("echo \"$file\" >> $year.$playlist_extension"); }
101 if(! defined $opt_t) { system("echo \"$file\" >> $year-$month.$playlist_extension"); }
103 print " Skipping $file since we can't extract the year and month from it\n";
107 # Fix the permissions
108 system("chown $owner \"$dstdir/\"*.$playlist_extension");
109 system("chgrp $group \"$dstdir/\"*.$playlist_extension");
110 system("chmod $mode \"$dstdir/\"*.$playlist_extension");
114 # Sanity checks / Option processing
115 if(defined $opt_h) { usage(); exit 1; }
118 my %dstdirs = ("$opt_d",1);
119 create_playlists(\%dstdirs);
121 die "-E- You must specify the -d <dir> option when using the -p option\n";
126 # Only proceed if there are video files to organize
127 $video_files_found=`$find_cmd_with_mkv`;
128 if(!$video_files_found) { exit 0; }
130 # Only proceed if we are skipping this check or if the $srcpathname dir hasn't been modified in the last $minage minutes
131 if(! $opt_f && ((time - (stat("$srcpathname"))->mtime) < ($minage * 60))) {
135 # Only one instance of this script running at a time
136 my $pidfile = File::Pid->new({file => "$pid_file", pid => "$$"});
137 exit if $pidfile->running();
143 # Merge videos prior to copying them over to the destination path
146 if(defined $opt_t) { $merge_opts .= "-t "; }
147 if(defined $opt_k) { $merge_opts .= "-k "; }
148 if(defined $opt_v) { $merge_opts .= "-v "; }
149 if($requantize_input_video) { $merge_opts .= ' -q'; }
150 if($recompress_input_video) { $merge_opts .= ' -z'; }
151 $errno=system("$merge_videos_by_day -s \"$srcpathname\" $merge_opts");
152 $errno = $errno >> 8;
153 if($errno) { die "-E- $merge_videos_by_day encountered some errors with exit code $errno\n"; }
155 # Exit now if we are not supposed to copy the resultant video files to the destination path
162 # Copy the videos over to the destination path
164 chdir "$srcpathname";
165 print "$find_mkv\n" if($opt_v);
166 foreach $file (`$find_mkv`) {
169 $srcdir = dirname($file);
170 $file = basename($file);
172 $ext = $file; $ext =~ s/.*\.(\S+)$/$1/; $ext = lc($ext);
174 # Throw out files not in the current srcpath
175 if((! -f "$srcfile") && (! -f "$srcdir/$srcfile")) { next; }
177 # Throw out encoded files left over from a previous run
178 print "srcfile: $srcfile\n";
179 if($srcfile =~ /.hb.mp4/) { next; }
181 print "Found video: srcdir: $srcdir srcfile: $srcfile ext: $ext\n" if($opt_v);
183 # From the modification time of the file since we couldn't find it in the filename
184 $date_modified = ctime(stat("$srcdir/$srcfile")->mtime);
186 # NOTE: This file matching algorithm only applies to videos produced by merge_videos_by_day called by this script earlier
187 # We just need to take those merged videos, and extract what date to call the video by under HomeVideos
189 # Get the date taken from the filename
190 if($srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
193 $day = sprintf("%02d",$3);
195 $monthname = lc($month2monthname{$month});
197 elsif($srcfile =~ /^(\d\d\d\d)(\d\d)(\d\d)/) {
200 $day = sprintf("%02d",$3);
202 $monthname = lc($month2monthname{$month});
204 # Get the date taken from the source directory
205 elsif($srcdir =~ /(\d+)-(\d+)-(\d+)/) {
208 $day = sprintf("%02d",$3);
210 $monthname = lc($month2monthname{$month});
212 # Get the date taken from the modification time
213 elsif($date_modified =~ /\S+\s+(\S+)\s+(\d+)\s+\S+\s+(\d+)/) {
216 $day = sprintf("%02d",$2);
217 $monthnum = $monthname2month{$month};
218 $monthname = lc($month);
220 print "-E- Unable to parse year and month from this file: $srcdir/$srcfile\n";
224 # We are ready to pick a destination folder to put the video in
225 $dstdir = $dstpathname . "/" . $year;
226 $dstdirs{$dstdir} = 1;
227 $dstfile = $dstdir . "/" . $year . "-" . $monthnum . "-" . $day;
229 # Check for duplicate filenames at the destination
230 $newfile = $dstfile . "." . $video_suffix;
231 if(-e "$newfile.$ext") {
232 foreach $i ($video_suffix+1 .. '999') {
233 $newfile = $dstfile . "." . sprintf("%03d",$i);;
234 if(! -e "$newfile.$ext") { last; }
238 $dstfile = "$newfile.$ext";
241 print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
243 # Make sure the destination directories exist
244 $errno=system("mkdir -p -m $dirmode \"$dstdir\"");
245 if($errno) { print "-E- Error creating dstdir: $dstdir\n"; next; }
246 # Perform the move operation from $srcdir/$srcfile -> $dstfile
247 print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
248 # Make sure the dstfile doesn't exist, if it does, don't do the move
249 if(! -f "$dstfile") {
250 $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
251 if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
253 print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n";
255 # Fix the permissions
256 system("chown $owner \"$dstfile\"");
257 system("chgrp $group \"$dstfile\"");
258 system("chmod $mode \"$dstfile\"");
261 # Check to see if there is an empty sub directory to remove
262 if(($srcdir ne $srcpathname) && ($srcpathname ne ".")) {
263 if(is_folder_empty($srcdir)) {
264 print "-> Subdir detected for videos ($srcdir != $srcpathname)\n" if($opt_v);
265 if(! defined $opt_t) {
267 system("rm $tmpdir");
268 system("cp -R \"$srcdir\" $tmpdir > /dev/null 2>/dev/null");
269 system("rm -rf \"$srcdir\"");
270 print "-> Moved empty subdir $srcdir to $tmpdir\n" if($opt_v);
276 # For each destination dir we copied new content into, recreate the playlists
277 create_playlists(\%dstdirs);