Skip to content

Instantly share code, notes, and snippets.

@wrybread
Last active May 23, 2023 08:37
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 wrybread/028a4f94afa1b73bbc72e646e66e1bb0 to your computer and use it in GitHub Desktop.
Save wrybread/028a4f94afa1b73bbc72e646e66e1bb0 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
'''
This solves a problem in DJI drone cameras where the file numbers never go above DJI_0999.JPG.
This will prepend the creation date of each file and add some number to the file number (default 3,000,000). For example it'll change
DJI_0999.JPG to "2023-05-23 DJI_3000999.JPG"
It keeps track of the last number it added (in 0filename_fixer.dat), so run it on the card before importing the pics and vids. It processes
files in subdirectories of the script's directory, so keep it on the card and run it before importing.
It's smart enough to know that a file has already been processed (won't add 1,000,000 to a file
who's number is already above 1,000).
It's also smart enough to handle files with extra text and numbers added, like "DJI_0088 - Part 2.MP4" becomes
"2023-05-23 DJI_1000088 - Part 2.MP4"
'''
from __future__ import print_function
import sys, os, re, time, datetime
# prepend the file's creation date to the filename?
prepend_date = True
# If no saved file number in a file, will increment by this amount
default_saved_number = 1000000
# 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]) )
#-------------------------------------------
script_fname = os.path.splitext(os.path.basename(__file__))[0]
saved_number_data_file = os.path.join(directory, "%s.dat" % script_fname)
try: saved_number = int( open(saved_number_data_file,"r").read() )
except:
print ("No saved increment amount, using default of %s" % default_saved_number)
saved_number = default_saved_number
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]
file_number_int = int(file_number)
#print ("file_number_int=%s saved_number=%s" % (file_number_int, saved_number))
if file_number_int < 1000: #saved_number:
saved_number += 1
# increment it
#new_file_number = file_number_int + increment_amount
new_file_number = saved_number
# re-compose the filename (there's probably a more graceful way to do this)
new_filename = "DJI_" + str(new_file_number) + f.replace("DJI_"+file_number, "")
if prepend_date:
# get the file's creation time and insert it at start of filename
t = os.path.getmtime(full_filename_path)
dt = datetime.datetime.fromtimestamp(t)
date_str = dt.strftime('%Y-%m-%d')
new_filename = "%s %s" % (date_str, new_filename)
new_filename_path = os.path.join(path, new_filename)
print ("Renaming %s to %s" % (full_filename_path, new_filename_path) )
# rename it! (comment out for testing obviously)
os.rename(full_filename_path, new_filename_path)
# save the increment amount
h = open(saved_number_data_file, "w")
h.write(str(saved_number))
h.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment