From e1df56954cd8fbb372ee86e12b53f4313ae77eda Mon Sep 17 00:00:00 2001
From: "Alan J. Pippin" <alan@pippins.net>
Date: Thu, 25 May 2023 21:44:44 -0600
Subject: [PATCH] Updated for Ubuntu 22.04. Also fixed merge videos cmin check

---
 check_phone_videos_are_in_originals | 26 ++++++++++++++++++++++----
 make_home_videos.pl                 |  2 +-
 make_mkv                            |  2 +-
 mkv_extract_chapter                 |  2 +-
 organize_videos                     |  9 ++++-----
 organize_videos.conf                |  6 +++---
 6 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/check_phone_videos_are_in_originals b/check_phone_videos_are_in_originals
index f002e59..0b1dd22 100755
--- a/check_phone_videos_are_in_originals
+++ b/check_phone_videos_are_in_originals
@@ -1,9 +1,16 @@
 #!/bin/bash
 # Checks to make sure all videos off of phone have been copied to the Originals directory
 # If they are missing, it means Nextcloud missed them somehow. Copy them to New Memories then.
+# 1) First connect the phone to a computer with USB debugging turned on.
+#    Authorize the connection
+# 2) Execute the command "adb tcpip 5555"
+# 3) Run the script, authorize the connection
+# 4) Run the script again
+# You should now be able to connect to the device wirelessly
 
 dcim_path="/storage/emulated/0/DCIM/Camera"
 originals="/naspool/dropbox/Originals"
+organized_logs="/var/log/organize"
 new_memories="/naspool/dropbox/New Memories"
 
 adb connect 192.168.0.23
@@ -17,8 +24,19 @@ mp4s_in_originals=$(/bin/ls -1 $originals/*.mp4 | xargs -n 1 basename)
 
 for i in $mp4s_on_phone; do
   echo "$mp4s_in_originals" | grep -q $i
-  if [[ $? == 1 ]]; then
-    echo "mp4 not found in originals: $i"
-    adb -e pull "$dcim_path/$i" "$new_memories"
-  fi
+  if [[ $? != 0 ]]; then
+    zgrep -q $i $organized_logs/organize_videos*
+    if [[ $? != 0 ]]; then
+      echo "mp4 not found in originals or organize logs: $i"
+      if [[ ! -e "$new_memories/$i" ]]; then
+        adb -e pull "$dcim_path/$i" "$new_memories"
+      else
+        echo "mp4 already copied into $new_memories"
+      fi
+    else
+      echo "mp4 found in originals or organize logs: $i"
+    fi
+  else
+     echo "mp4 found in originals or organize logs: $i"
+  fi 
 done
diff --git a/make_home_videos.pl b/make_home_videos.pl
index 6963175..d0a4a44 100755
--- a/make_home_videos.pl
+++ b/make_home_videos.pl
@@ -220,7 +220,7 @@ sub encode_videos {
 	    print "$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file\n";
 	}
 	if(!$opt_s) {
-	    $tempfile = `tempfile`; chomp($tempfile);
+	    $tempfile = `mktemp`; chomp($tempfile);
 	    system("$mencoder $encode_options_pass1 -o ./$encoded_dir/$current_output_file -idx $current_file 2>$tempfile");
 	    system("$mencoder $encode_options_pass2 -o ./$encoded_dir/$current_output_file -idx $current_file 2>>$tempfile");
 	    if(! -e "./$encoded_dir/$current_output_file") {
diff --git a/make_mkv b/make_mkv
index 0ad7534..ac85eab 100755
--- a/make_mkv
+++ b/make_mkv
@@ -278,7 +278,7 @@ if($opt_v) {
 # 3) Create a chapter file for each destination video file
 
 # tmp chapter file used by handbrake when creating mkv, but remove the 0 byte file it creates, we'll create it if we need it
-my $chapter_file = `tempfile`; chomp($chapter_file); unlink "$chapter_file";
+my $chapter_file = `mktemp`; chomp($chapter_file); unlink "$chapter_file";
 
 foreach my $key (keys %merge_videos) {   
     # Create the chapters file for each output mkv file.
diff --git a/mkv_extract_chapter b/mkv_extract_chapter
index ca33215..273484b 100755
--- a/mkv_extract_chapter
+++ b/mkv_extract_chapter
@@ -39,7 +39,7 @@ use Getopt::Std;
 ####################################################################################################
 # Configuration parameters - CHANGE THESE TO SUITE YOUR NEEDS
 my $ffmpeg=`which ffmpeg`; chomp($ffmpeg);
-my $tmpfile = `tempfile`; chomp($tmpfile);
+my $tmpfile = `mktemp`; chomp($tmpfile);
 ####################################################################################################
 
 ####################################################################################################
diff --git a/organize_videos b/organize_videos
index d62af19..a037d75 100755
--- a/organize_videos
+++ b/organize_videos
@@ -127,10 +127,9 @@ if(defined $opt_p) {
 $video_files_found=`$find_cmd_with_mkv`;
 if(!$video_files_found) { exit 0; }
 
-# Only proceed if no files have changed in the past $cmin minutes
-if(! $opt_f) { 
-  $changed_files_found=`$find_changed_cmd`;
-  if($changed_files_found) { exit 0; }
+# Only proceed if we are skipping this check or if the $srcpathname dir hasn't been modified in the last $minage minutes
+if(! $opt_f && ((time - (stat("$srcpathname"))->mtime) < ($minage * 60))) {
+  exit 0;
 }
 
 # Only one instance of this script running at a time
@@ -264,7 +263,7 @@ foreach $file (`$find_mkv`) {
 	if(is_folder_empty($srcdir)) { 
 	    print "-> Subdir detected for videos ($srcdir != $srcpathname)\n" if($opt_v);
 	    if(! defined $opt_t) { 
-		$tmpdir=`tempfile`;
+		$tmpdir=`mktemp`;
 		system("rm $tmpdir");
 		system("cp -R \"$srcdir\" $tmpdir > /dev/null 2>/dev/null");
 		system("rm -rf \"$srcdir\"");
diff --git a/organize_videos.conf b/organize_videos.conf
index b4e288f..14a2bd9 100644
--- a/organize_videos.conf
+++ b/organize_videos.conf
@@ -91,8 +91,8 @@ $no_recompress_file_ext = qr/\.(mp4)$/i;
 # This is done to ensure that all videos from a given upload from a camera have completed prior to looking for videos to merge
 $minage = "+60";
 
-# What command should be used to find files that have changed (are at least $minage old) 
-$find_changed_cmd = "find  \"$srcpathname/\" -not -cmin $minage -a \\( $movie_file_ext \\)";
+# What command should be used to find files by move_videos_from_watchdir that have changed (are at least X minutes old) 
+$find_changed_cmd = "find  \"$srcpathname/\" -not -cmin +5 -a \\( $movie_file_ext \\)";
 
 # What command should be used to find all movie files
 $find_cmd = "find \"$srcpathname/\" $movie_file_ext";
@@ -100,7 +100,7 @@ $find_cmd_with_mkv = "find \"$srcpathname/\" $movie_file_ext -o -iregex \".*\.mk
 $find_mkv = "find \"$srcpathname/\" -iregex \".*\.mkv\"";
 
 # Set the tmpfile to use, but remove the 0 byte file it creates, we'll create it if we need it
-$tmpfile = `tempfile`; chomp($tmpfile); unlink "$tmpfile";
+$tmpfile = `mktemp`; chomp($tmpfile); unlink "$tmpfile";
 
 # Set the timezone to use
 $timezone = `cat /etc/timezone`; chomp($timezone);
-- 
2.34.1