Fixed up scripts to support importing videos from phones
[videoscripts/.git] / copy_videos_from_watchdir
diff --git a/copy_videos_from_watchdir b/copy_videos_from_watchdir
new file mode 100755 (executable)
index 0000000..e64a505
--- /dev/null
@@ -0,0 +1,87 @@
+#!/usr/bin/perl
+# Author: Alan J. Pippin
+# Description: Find videos from a watched dir and copy them to the video processing destination directory
+
+use File::Copy;
+use File::Basename;
+use Getopt::Std;
+use File::stat;
+use Time::localtime;
+use File::Pid;
+
+####################################################################################################
+# Configuration parameters
+$mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir); unshift @INC,("$mydir");
+# Default configuration values
+require "organize_videos.conf";
+# Override defaults with local customizations
+if( -f "$mydir/organize_videos.conf.local") { require "organize_videos.conf.local"; }
+
+####################################################################################################
+
+# 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("hvt");
+
+sub usage {
+    print "usage: $0 [-v] [-t]\n";
+    print "   -v        verbose; print file being copied (to).\n";
+    print "   -t        test; print what will happen, but don't do anything\n";
+    return 1;
+}
+
+# Sanity checks / Option processing
+if(defined $opt_h) { usage(); exit 1; }
+
+# Our srcpathname is actually our dstpathname
+$dstpathname = $srcpathname;
+$srcpathname = $watchpathname;
+
+# Only proceed if there are video files to organize
+$video_files_found=`find \"$srcpathname/\" $movie_file_ext -o -iregex \".*\.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 => "$$"});
+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;
+
+# Copy the videos over to the destination path
+chdir "$srcpathname";
+print "$video_files_found\n" if($opt_v);
+foreach $file (`find \"$srcpathname/\" $movie_file_ext -o -iregex \".*\.mkv\"`) {
+
+    chomp($file);
+    $srcdir = dirname($file);
+    $file = basename($file);
+    $srcfile = $file;
+    $dstfile = "$dstpathname/$file";
+
+    print "-> Copying \"$srcdir/$srcfile\" to \"$dstfile\"\n";
+
+    if(!defined $opt_t) {
+       # Make sure the dstfile doesn't exist, if it does, don't do the copy
+       if(! -f "$dstfile") {
+           $errno=system("cp -p \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
+           if($errno) { print "-E- Error copying srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
+       } else {
+           print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n";
+       }
+       # Fix the permissions
+       system("chown $owner \"$dstfile\"");
+       system("chgrp $group \"$dstfile\"");
+       system("chmod $mode \"$dstfile\"");
+    }
+}
+
+$pidfile->remove();
+
+print "\n\n";