Skip to content

Instantly share code, notes, and snippets.

@kowalcj0
Created June 12, 2022 22:41
Show Gist options
  • Save kowalcj0/b014658f6cdd4f288ef175db3474ea2a to your computer and use it in GitHub Desktop.
Save kowalcj0/b014658f6cdd4f288ef175db3474ea2a to your computer and use it in GitHub Desktop.
Fix create & modify dates for quicktime tags in a mp4 file recorded with GoPRO Hero 5 using pyexifinfo & exiftool
#!/usr/bin/env python3
import glob
import subprocess
from datetime import date, datetime, time, timedelta
from typing import List
# pip install -U pyexifinfo
# https://github.com/guinslym/pyexifinfo
import pyexifinfo as exif
def get_file_names(*, extension: str = "mp4") -> List[str]:
return sorted(glob.glob(f"./*.{extension}"))
def get_create_date(
filename: str,
*,
track: int = 0,
tag: str = "QuickTime:CreateDate",
pattern: str = "%Y:%m:%d %H:%M:%S",
) -> datetime:
details = exif.get_json(filename)
raw_create_date = details[track][tag]
return datetime.strptime(raw_create_date, pattern)
def adjust(
input_datetime: datetime,
new_date: date,
*,
hours_offset: int = 0,
minutes_offset: int = 0,
) -> datetime:
if hours_offset != 0 or minutes_offset != 0:
new_time = input_datetime + timedelta(
hours=hours_offset,
minutes=minutes_offset,
)
else:
new_time = time(
hour=input_datetime.hour,
minute=input_datetime.minute,
second=input_datetime.second,
)
new_datetime = datetime(
new_date.year,
new_date.month,
new_date.day,
new_time.hour,
new_time.minute,
new_time.second
)
return new_datetime
def set_dates(
file_name: str,
new_datetime: datetime,
*,
pattern: str = "%Y:%m:%d %H:%M:%S"
):
formatted = datetime.strftime(new_datetime, pattern)
cmd_list = [
"exiftool",
f"-quicktime:createdate='{formatted}'",
f"-quicktime:modifydate='{formatted}'",
f"-quicktime:trackcreatedate='{formatted}'",
f"-quicktime:trackmodifydate='{formatted}'",
f"-quicktime:mediamodifydate='{formatted}'",
f"-quicktime:mediacreatedate='{formatted}'",
file_name
]
print(cmd_list)
# Uncomment the next line ONLY WHEN you're happy with the new values
#subprocess.run(cmd_list, check=True)
if __name__ == "__main__":
file_names = get_file_names()
for name in file_names:
print(f"\nDates for {name}:")
create_date = get_create_date(name)
print(f"Original create date: {create_date}")
adjusted = adjust(
create_date,
date(2022, 6, 12),
hours_offset=0,
minutes_offset=-27
)
print(f"Adjusted create date: {adjusted}")
set_dates(name, adjusted)
@kowalcj0
Copy link
Author

This is a very quick and dirty way to fix the recording date of your GoPRO video.

Usage:

  1. set new date in line #87
  2. set hours and minutes offsets in lines #88 & #89
  3. run the script and check that the new dates match your needs
  4. uncomment line #76 & re-run the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment