Last active
May 23, 2023 21:47
-
-
Save wrybread/42e59debccdf876151309e6a99144cbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
''' | |
This script solves a problem with DJI drones where the file numbers never go above DJI_0999.MP4, and so the filenames aren't unique. | |
This adds the time and date the photo or video was taken at the start of the filename, preserving whatever text has already been added | |
but removing the "DJI_0999". So for example "DJI_0999 - mountains.MP4" created on May 5, 2023 at 11:05am becomes | |
"2023-05-23-110530 - mountains.MP4". And "DJI_0999.MP4" becomes "2023-05-23-110530.MP4". | |
And it checks to see if there's already a file with that timestamp, and if so adds an extra number ("2023-05-23-110530 (2).MP4", | |
"2023-05-23-110530 (3).MP4" etc.), which might happen with burst photos. | |
And it won't add the date if it's already been added, meaning the script can be run on the same files without adding the date again. | |
And it won't add the date to any file that doesn't begin with "DJI_", so it will only add the date to the DJI created pics, videos and | |
meta data files. | |
It runs on the folder the script is in and all subfolders, so it's meant to be left in the DCIM folder on the SD card and run before importing | |
the pics and videos. | |
''' | |
from __future__ import print_function | |
import sys, os, re, time, datetime | |
# Where are the files to process? (Default is directory of script). Will process subdirectories too. | |
directory = os.path.dirname( os.path.abspath(sys.argv[0]) ) | |
#----------------------------------------------------- | |
for path, dirs, files in os.walk(directory): | |
for f in files: | |
full_filename_path = os.path.join(path, f) | |
if f[:4] == "DJI_": | |
# gets the number in the filename. For example "DJI_0991.MP4" and "DJI_0991 - part 2.MP4" both return "0991" | |
file_number = re.findall('DJI_(.+?)\W', f) [0] | |
# Get the file's creation time and make a timestamp. Apparently there are cross-platform | |
# issues with creation time (getctime() or pathlib) | |
t = os.path.getmtime(full_filename_path) | |
dt = datetime.datetime.fromtimestamp(t) | |
date_str = dt.strftime("%Y-%m-%d-%H%M%S") | |
# construct the new filename | |
new_filename = date_str + f.lstrip("DJI_"+file_number) | |
new_filename_path = os.path.join(path, new_filename) | |
# if the new filename already exists (maybe multiple pics taken that second in burst mode) add a number to the file | |
if os.path.exists(new_filename_path): | |
extension = os.path.splitext(new_filename_path)[1] | |
for i in range(2, 1000): | |
test = os.path.splitext(new_filename_path)[0] + " ("+str(i)+")"+extension | |
if not os.path.exists(test): | |
new_filename_path = test | |
break | |
# rename it! (comment out for testing obviously) | |
print ("Renaming %s to %s" % (full_filename_path, new_filename_path) ) | |
os.rename(full_filename_path, new_filename_path) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment