Fixed bug in how dates were extracted from video files
authorAlan J. Pippin <alan@pippins.net>
Thu, 21 Feb 2019 16:46:48 +0000 (09:46 -0700)
committerAlan J. Pippin <ajp@pippins.net>
Thu, 21 Feb 2019 16:46:48 +0000 (09:46 -0700)
Rewrote moving videos from watchdir to support NextCloud dirs

merge_videos_by_day
move_videos_from_watchdir
organize_movies
organize_videos.conf

index cda301dae72bcf37e2d184d0e017b9196f7557b0..ec00da97e7fe64f3ea8fe1a20240b5689a397ce8 100755 (executable)
@@ -100,7 +100,7 @@ foreach $file (sort `$find_cmd`) {
     $date_taken = ctime(stat("$srcdir/$srcfile")->mtime);
     
     # Get the date taken from the filename
-    if(!$merge_by_modification_date && $srcfile =~ /^(\d+)-(\d+)-(\d+)/) {
+    if(!$merge_by_modification_date && $srcfile =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/) {
        $year = $1;
        $month = $2;
        $day = sprintf("%02d",$3);
@@ -115,7 +115,7 @@ foreach $file (sort `$find_cmd`) {
         $monthname = lc($month2monthname{$month});
     }
     # Get the date taken from the srcdir
-    elsif(!$merge_by_modification_date && $srcdir =~ /(\d+)-(\d+)-(\d+)/) {
+    elsif(!$merge_by_modification_date && $srcdir =~ /(\d\d\d\d)-(\d\d)-(\d\d)/) {
        $year = $1;
        $month = $2;
        $day = sprintf("%02d",$3);
index 9c1a242f3f8bc066133a2c2ffd48fc73c5d28e28..83ec63fa50364c5678fba0cfd491a957f8da6c77 100755 (executable)
@@ -21,7 +21,6 @@ if( -f "$mydir/organize_videos.conf.local") { require "organize_videos.conf.loca
 
 # Sanity check
 if(! -d $srcpathname) { print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
-if(! -d $watchpathname) { print "-E- Can't find watchpath: $watchpathname\n"; exit 1; }
 
 getopts("hfvt");
 
@@ -36,63 +35,81 @@ sub usage {
 # Sanity checks / Option processing
 if(defined $opt_h) { usage(); exit 1; }
 
-# Our srcpathname is actually our dstpathname
-$find_changed_cmd =~ s/$srcpathname/$watchpathname/g;
-$find_cmd_with_mkv =~ s/$srcpathname/$watchpathname/g;
-$dstpathname = $srcpathname;
-$srcpathname = $watchpathname;
-
-# Only proceed if no files have changed in the past $cmin minutes
-$changed_files_found=`$find_changed_cmd`;
-if(!$opt_f && $changed_files_found) { exit 0; }
-
-# Only proceed if there are video files to organize
-$video_files_found=`$find_cmd_with_mkv`;
-if(!$video_files_found) { exit 0; }
-
 # Only one instance of this script running at a time
 my $pidfile = File::Pid->new({file => "$pid_file", pid => "$$"});
 print "pid_file: $pid_file\n" if($opt_v);
 exit if $pidfile->running();
 $pidfile->write();
 
-# Print the date
-system("date");
-
-# Merge videos prior to copying them over to the destination path
-my $errno = 0;
-
-# Move the videos over to the destination path
-chdir "$srcpathname";
-print "$video_files_found\n" if($opt_v);
-foreach $file (`$find_cmd_with_mkv`) {
-
-    chomp($file);
-    $srcdir = dirname($file);
-    $file = basename($file);
-    $srcfile = $file;
-    $dstfile = "$dstpathname/$file";
-
-    # only move files that have been organized
-    if($srcfile !~ /\d\d\d\d-\d\d-\d\d/) { next; }
+print "watchpathname: $watchpathname\n" if($opt_v);
+foreach $watchpath (split(';', $watchpathname)) {
+    if(! -d $watchpath) { print "-E- Can't find watchpath: $watchpath\n"; exit 1; }
+    print "checking $watchpath\n" if($opt_v);
     
-    print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
-
-    if(!defined $opt_t) {
-       # Make sure the dstfile doesn't exist, if it does, don't do the move
-       if(! -f "$dstfile") {
-           $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
-           if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
-       } else {
-           print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n";
+    # Our srcpathname is actually our dstpathname
+    $new_find_changed_cmd = $find_changed_cmd;
+    $new_find_cmd_with_mkv = $find_cmd_with_mkv;
+    $new_find_changed_cmd =~ s/$srcpathname/$watchpath/g;
+    $new_find_cmd_with_mkv =~ s/$srcpathname/$watchpath/g;
+    $dstpathname = $srcpathname;
+
+    # Only proceed if no files have changed in the past $cmin minutes
+    $changed_files_found=`$new_find_changed_cmd`;
+    print "finding videos: $new_find_changed_cmd\n" if($opt_v);
+    if(!$opt_f && $changed_files_found) { next; }
+
+    # Only proceed if there are video files to organize
+    $video_files_found=`$new_find_cmd_with_mkv`;
+    print "finding videos: $new_find_cmd_with_mkv\n" if($opt_v);
+    if(!$video_files_found) { next; }
+
+    # Print the date
+    system("date");
+    my $errno = 0;
+
+    # Move the videos over to the destination path
+    chdir "$watchpath";
+    print "found: $video_files_found\n" if($opt_v);
+    foreach $file (`$new_find_cmd_with_mkv`) {
+
+       chomp($file);
+       $srcdir = dirname($file);
+       $file = basename($file);
+       $srcfile = $file;
+       $dstfile = "$dstpathname/$file";
+
+       # Make sure we have a unique dstfile
+       if(-f "$dstfile") {
+           $video_ext = $srcfile;
+           $video_ext =~ s/.*\.(\S+)$/$1/;
+           $suffix = 0;
+           $newdstfile_base = $dstfile;
+           $newdstfile_base =~ s/\.[^.]*$//;
+           do {
+               $newdstfile = "${newdstfile_base}_${suffix}.${video_ext}";
+               $suffix++; 
+           } while(-f "$newdstfile");
+           $dstfile = $newdstfile;
+       }
+       
+       print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
+
+       if(!defined $opt_t) {
+           # Make sure the dstfile doesn't exist, if it does, add a unique number to the end
+           if(! -f "$dstfile") {
+               $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
+               if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
+           } else {
+               print "-E- Unable to mv $srcdir/$srcfile -> $dstfile because it already exists\n";
+           }
+           # Fix the permissions
+           system("chown $owner \"$dstfile\"");
+           system("chgrp $group \"$dstfile\"");
+           system("chmod $mode \"$dstfile\"");
        }
-       # Fix the permissions
-       system("chown $owner \"$dstfile\"");
-       system("chgrp $group \"$dstfile\"");
-       system("chmod $mode \"$dstfile\"");
     }
 }
-
+                   
 $pidfile->remove();
 
-print "\n\n";
+#print "\n\n";
index 290f37ce36f011422880c1bfcf7da11a5b6c980f..f2c92151eefffc04a063496b72e0bde533403f60 100755 (executable)
@@ -32,7 +32,7 @@ exit if $pidfile->running();
 $pidfile->write();
 
 # Encode the movies
-encode_movies($mobile_movie_src_dir, $mobile_movie_dst_dir);
+#encode_movies($mobile_movie_src_dir, $mobile_movie_dst_dir);
 encode_movies($movie_src_dir, $movie_dst_dir);
 
 sub encode_movies {
@@ -56,6 +56,7 @@ sub encode_movies {
            my $stat = stat("$movie");
            if($stat->mtime < ($now - $minage*60)) {
                my ($base, $dir, $ext) = fileparse($movie,'\..*');
+                $base =~ s/_t\d+$//g; # remove any extra title numbers from the name
                my $output = "$movie_dst_dir/$category/$base.mkv";
                print "-> Encoding $movie => $output\n";
                my $date = `date`; chomp $date;
index 54a57a460fd7ea7af2ccab66a2a7bc76dfc539a5..aebd225840d3b7e028a9dfbb2627ca4e75cd8626 100644 (file)
@@ -22,8 +22,8 @@ $origpathname = "/naspool/dropbox/Originals";
 $originals_file_ext = qr/(mov|mp4)/i;
 $save_originals = 1;
 
-# Path to a dir to watch for videos to move to $srcpathname to be organized
-$watchpathname = "/naspool/pictures/mylio";
+# Path to a dir (or dirs separated by semis) to watch for videos to move to $srcpathname to be organized
+$watchpathname = "/naspool/cloud/alan/files/InstantUpload/Camera;/naspool/cloud/mary/files/InstantUpload/Camera;";
 
 # Path to merge_videos_by_day script
 $merge_videos_by_day = "/naspool/videos/bin/merge_videos_by_day";
@@ -92,8 +92,8 @@ $tmpfile = `tempfile`; chomp($tmpfile); unlink "$tmpfile";
 $timezone = `cat /etc/timezone`; chomp($timezone);
 
 # handbrake options used when re-encoding the videos
-$handbrake_requantize_options='--strict-anamorphic --crop 0:0:0:0 -E ac3';
-$handbrake_recompress_options='--strict-anamorphic --crop 0:0:0:0 --denoise="weak" -e x264 -q 18 -x b-adapt=2:rc-lookahead=50 -v 2 -a 1 -6 dpl2 --preset="High Profile"';
+$handbrake_requantize_options='--auto-anamorphic --crop 0:0:0:0 -E ac3';
+$handbrake_recompress_options='--auto-anamorphic --crop 0:0:0:0 --denoise="weak" -e x264 -q 18 -x b-adapt=2:rc-lookahead=50 -v 2 -a 1 -6 dpl2 --preset="HQ 1080p30 Surround"';
 
 # handbrake input file options
 $input_file_options = "-S";