X-Git-Url: http://git.pippins.net/embedvideo/.git/?a=blobdiff_plain;f=move_videos_from_watchdir;h=864d33e1c8265869001dd8202fc1ce33f37e0c2e;hb=aea4af9869a2086c5085afb34a1bd790940c8b7d;hp=9c1a242f3f8bc066133a2c2ffd48fc73c5d28e28;hpb=bfa24cc8c0228028cae669b2fa0adddef10470ae;p=videoscripts%2F.git diff --git a/move_videos_from_watchdir b/move_videos_from_watchdir index 9c1a242..864d33e 100755 --- a/move_videos_from_watchdir +++ b/move_videos_from_watchdir @@ -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,91 @@ 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"; + # 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 srcdir is group writable + if(not -w "$srcdir") { + print "-W- srcdir is not group writable, can't move videos out of it: $srcdir\n"; + next; + } + + # Make sure we have a unique dstfile + if($overwrite_dest == 0 && -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, don't do the move - if(! -f "$dstfile") { + 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 "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n"; + #} 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\""); } -} + # Update nextcloud file cache so it knows what files we have moved + $watchpath =~ s/$nextcloudbase//g; + system("$occ files:scan --path \"$watchpath\" > /dev/null"); +} + $pidfile->remove(); -print "\n\n"; +#print "\n\n";