Updated for Ubuntu 22.04. Also fixed merge videos cmin check
[videoscripts/.git] / copy_videos_from_watchdir
1 #!/usr/bin/perl
2 # Author: Alan J. Pippin
3 # Description: Find videos from a watched dir and copy them to the video processing destination directory
4
5 use File::Copy;
6 use File::Basename;
7 use Getopt::Std;
8 use File::stat;
9 use Time::localtime;
10 use File::Pid;
11
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"; }
19
20 ####################################################################################################
21
22 # Sanity check
23 if(! -d $srcpathname) { print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
24 if(! -d $watchpathname) { print "-E- Can't find watchpath: $watchpathname\n"; exit 1; }
25
26 getopts("hvt");
27
28 sub usage {
29     print "usage: $0 [-v] [-t]\n";
30     print "   -v        verbose; print file being copied (to).\n";
31     print "   -t        test; print what will happen, but don't do anything\n";
32     return 1;
33 }
34
35 # Sanity checks / Option processing
36 if(defined $opt_h) { usage(); exit 1; }
37
38 # Our srcpathname is actually our dstpathname
39 $dstpathname = $srcpathname;
40 $srcpathname = $watchpathname;
41
42 # Only proceed if there are video files to organize
43 $video_files_found=`find \"$srcpathname/\" $movie_file_ext -o -iregex \".*\.mkv\"`;
44 if(!$video_files_found) { exit 0; }
45
46 # Only one instance of this script running at a time
47 my $pidfile = File::Pid->new({file => "$pid_file", pid => "$$"});
48 exit if $pidfile->running();
49 $pidfile->write();
50
51 # Print the date
52 system("date");
53
54 # Merge videos prior to copying them over to the destination path
55 my $errno = 0;
56
57 # Copy the videos over to the destination path
58 chdir "$srcpathname";
59 print "$video_files_found\n" if($opt_v);
60 foreach $file (`find \"$srcpathname/\" $movie_file_ext -o -iregex \".*\.mkv\"`) {
61
62     chomp($file);
63     $srcdir = dirname($file);
64     $file = basename($file);
65     $srcfile = $file;
66     $dstfile = "$dstpathname/$file";
67
68     print "-> Copying \"$srcdir/$srcfile\" to \"$dstfile\"\n";
69
70     if(!defined $opt_t) {
71         # Make sure the dstfile doesn't exist, if it does, don't do the copy
72         if(! -f "$dstfile") {
73             $errno=system("cp -p \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
74             if($errno) { print "-E- Error copying srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
75         } else {
76             print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n";
77         }
78         # Fix the permissions
79         system("chown $owner \"$dstfile\"");
80         system("chgrp $group \"$dstfile\"");
81         system("chmod $mode \"$dstfile\"");
82     }
83 }
84
85 $pidfile->remove();
86
87 print "\n\n";