Skip to content

Instantly share code, notes, and snippets.

@bsacheri
Created November 10, 2023 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsacheri/eb56b486c69b07775a41557aba7f5917 to your computer and use it in GitHub Desktop.
Save bsacheri/eb56b486c69b07775a41557aba7f5917 to your computer and use it in GitHub Desktop.
Make adjustments to the timestamp of .jpg and .mp4 files names
# Make adjustments to the timestamp of .jpg and .mp4 files names
#
# This is useful if your camera's clock is wrong or not handling daylight saving time.
#
# Place this Python file in a folder that has .jpg and .mp4 files from the Maxto M3 camera.
# When you run the script, it will rename files by subtracting changeHour and changeMinute
# from the formatted time in the filename. It will do the same for the file Modified date.
# You will be prompted to enter a password. If no password is entered it will run a test
# and put the results into the log file.
#
# Example output:
# 20231108200516_009309.MP4, 20231108190216_009309.MP4, 2023-11-08 20:08:16, 2023-11-08 19:02:16
#
# Ben Sacherich - 11/9/2023
# Lots of help from Phind.com
# https://www.phind.com/search?cache=cdevpqn0dh9ht7e8amhmimnq
import os
import logging
from datetime import datetime, timedelta
import winsound
frequency = 2000 # Set Frequency To 2500 Hertz
duration = 100 # Set Duration To 1000 ms == 1 second
# Set the amount of hours and minute to SUBTRACT from the file time.
changeHour=1
changeMinute=3
# Set up the logger
logging.basicConfig(filename='logfile.log', level=logging.INFO, format='%(message)s')
# Write a header row to the log file
logging.info(f'Original Filename,New Filename,Original file time,New file time')
# Define the directory path as the current directory of the .py file
dir_path = os.getcwd()
os.system('cls') # clear the screen on windows
print ("\nThis will adjust the filename and the file timestamp of all .jpg and .mp4\nfiles in the " + dir_path + " folder.")
print ("\nIt will change the time by" , -changeHour , "hours and" , -changeMinute , "minutes.\n")
winsound.Beep(frequency, duration) # beep to provide feedback
# Prompt the user to confirm if this is a test, with 'yes' as the default answer
is_test = input("Type the password to make this permanent or press Enter to test: ").lower() or 'test'
print("\n")
# Iterate over all files in the directory
for file_name in os.listdir(dir_path):
f_path = os.path.join(dir_path, file_name)
# Check if the file extension is .jpg or .mp4, ignoring case
if os.path.splitext(file_name)[1].lower() in ['.jpg', '.mp4']:
# Extract the timestamp from the file name
timestamp_str = file_name.split('_')[0]
# Convert the timestamp string to a datetime object
timestamp_format = "%Y%m%d%H%M%S"
timestamp_dt = datetime.strptime(timestamp_str, timestamp_format)
# Subtract 3 hours and 45 minutes from the datetime object
# timestamp_dt = timestamp_dt - timedelta(hours=3, minutes=45)
timestamp_dt = timestamp_dt - timedelta(hours=changeHour, minutes=changeMinute)
# Convert the datetime object back to a string
new_timestamp_str = timestamp_dt.strftime(timestamp_format)
# Construct the new file name
new_file_name = new_timestamp_str + "_" + file_name.split('_')[1]
# Get the original file's modification date and time
original_modified_time = datetime.fromtimestamp(os.path.getmtime(f_path))
print (file_name, new_file_name)
# If this is not a test, rename the file and change the access and modification times on the file object
if is_test == 'noundo':
new_f_path = os.path.join(dir_path, new_file_name)
os.rename(f_path, new_f_path)
os.utime(new_f_path, (timestamp_dt.timestamp(), timestamp_dt.timestamp()))
# Write to the log file
logging.info(f'{file_name},{new_file_name},{original_modified_time},{timestamp_dt}')
if is_test == 'noundo':
logging.info(f'- - - Changes have been made. - - -')
print ('- - - Changes have been made. - - -')
winsound.Beep(frequency, 500) # long beep to provide feedback
winsound.Beep(frequency, duration)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment