From cb660ed1efe9806045d753d02e5c0b656919d639 Mon Sep 17 00:00:00 2001
From: "Alan J. Pippin" <alan@pippins.net>
Date: Sun, 13 Nov 2022 18:13:11 -0700
Subject: [PATCH] Fixed the way we handle creation dates in videos missing
 timezones

---
 make_mkv             | 17 +++++++++++++----
 merge_videos_by_day  |  9 +++++++--
 organize_videos.conf |  6 +++++-
 3 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/make_mkv b/make_mkv
index f048116..0ad7534 100755
--- a/make_mkv
+++ b/make_mkv
@@ -20,7 +20,8 @@ use DateTime::Format::Duration;
 use DateTime::Format::Strptime qw( );
 
 # Set our ffmpeg creation_time format
-$ffmpeg_time_format = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'UTC', on_error => 'croak');
+$ffmpeg_time_format_utc = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'UTC', on_error => 'croak');
+$ffmpeg_time_format_local = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'local', on_error => 'croak');
 
 ####################################################################################################
 # Configuration parameters
@@ -72,8 +73,12 @@ foreach $video (split(/,/, $opt_i)) {
     if(! -r "$video") { die "-E- Unable to read input video file: $video\n"; }
     my $mtime_epoch = 0;
     my $creation_time = `$ffmpeg -i "$video" 2>&1 | grep "creation_time" | head -n 1 | awk '{print \$3}'`;
+    my $brands = `$ffmpeg -i "$video" 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
     if($creation_time) {
-	my $date_taken = $ffmpeg_time_format->parse_datetime($creation_time);
+	my $date_taken = $ffmpeg_time_format_utc->parse_datetime($creation_time);
+	if ($brands && $brands =~ /$local_tz_brands/) {
+	    $date_taken = $ffmpeg_time_format_local->parse_datetime($creation_time);
+	}
 	$date_taken->set_time_zone('local');
 	$mtime_epoch = $date_taken->epoch;
     } else {
@@ -291,8 +296,12 @@ foreach my $key (keys %merge_videos) {
 	my $sec  = 0;
 	my $mtime_epoch = 0;
 	my $creation_time = `$ffmpeg -i "$video" 2>&1 | grep "creation_time" | head -n 1 | awk '{print \$3}'`;
-	if($creation_time) {
-	    my $date_taken = $ffmpeg_time_format->parse_datetime($creation_time);
+    	my $brands = `$ffmpeg -i "$video" 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
+    	if($creation_time) {
+            my $date_taken = $ffmpeg_time_format_utc->parse_datetime($creation_time);
+            if ($brands && $brands =~ /$local_tz_brands/) {
+                $date_taken = $ffmpeg_time_format_local->parse_datetime($creation_time);
+            }
 	    $date_taken->set_time_zone('local');
 	    $mtime_epoch = $date_taken->epoch;
 	} else {
diff --git a/merge_videos_by_day b/merge_videos_by_day
index 193a7bd..3fdb426 100755
--- a/merge_videos_by_day
+++ b/merge_videos_by_day
@@ -10,7 +10,8 @@ use Time::localtime;
 use DateTime::Format::Strptime qw( );
 
 # Set our ffmpeg creation_time format
-$ffmpeg_time_format = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'UTC', on_error => 'croak');
+$ffmpeg_time_format_utc = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'UTC', on_error => 'croak');
+$ffmpeg_time_format_local = DateTime::Format::Strptime->new(pattern=>'%Y-%m-%dT%H:%M:%S', time_zone => 'local', on_error => 'croak');
 $ctime_format = DateTime::Format::Strptime->new(pattern=>'%a %b %d %H:%M:%S %Y', time_zone => 'local', on_error => 'croak');
 
 # Early command line options processing
@@ -104,8 +105,12 @@ foreach $file (sort `$find_cmd`) {
 
     # Make a note of the month, year, and day this video was taken
     $creation_time = `$ffmpeg -i "$srcdir/$srcfile" 2>&1 | grep "creation_time" | head -n 1 | awk '{print \$3}'`;
+    $brands = `$ffmpeg -i "$srcdir/$srcfile" 2>&1 | grep "compatible_brands" | tail -n 1`; chomp($brands);
     if($creation_time) {
-	$date_taken = $ffmpeg_time_format->parse_datetime($creation_time);
+	$date_taken = $ffmpeg_time_format_utc->parse_datetime($creation_time);
+        if ($brands && $brands =~ /$local_tz_brands/) {
+            $date_taken = $ffmpeg_time_format_local->parse_datetime($creation_time);
+        }
     } else {
 	# From the modification time of the file since we couldn't find it in the video file
 	$date_modified = ctime(stat("$srcdir/$srcfile")->mtime);
diff --git a/organize_videos.conf b/organize_videos.conf
index 27b7933..b4e288f 100644
--- a/organize_videos.conf
+++ b/organize_videos.conf
@@ -20,9 +20,13 @@ $dstpathname = "/naspool/videos/HomeVideos";
 # Path to move the originals to
 $origpathname = "/naspool/dropbox/Originals";
 $originals_file_ext = qr/(mov|mp4)/i;
-$originals_no_copy_brands = qr/mp42avc1/i; # Don't copy videos from camcorder to Originals because we keep these videos on those cards
+#$originals_no_copy_brands = qr/mp42avc1/i; # Don't copy videos from camcorder to Originals because we keep these videos on those cards
+$originals_no_copy_brands = qr/none/i; # We no longer keep the cards
 $save_originals = 1;
 
+# Brands that store time as local vs UTC
+$local_tz_brands = qr/mp42avc1/i; # the camcorder sets the creation time to the local time with no timezone (so UTC), so treat them as local timestamps, not UTC timestamps
+
 # 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;";
 
-- 
2.34.1