Last active
May 23, 2023 08:37
-
-
Save wrybread/028a4f94afa1b73bbc72e646e66e1bb0 to your computer and use it in GitHub Desktop.
Revisions
-
wrybread revised this gist
May 23, 2023 . 1 changed file with 19 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,25 +4,29 @@ 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 @@ -45,15 +49,12 @@ 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] @@ -73,12 +74,20 @@ # 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) @@ -92,5 +101,4 @@ -
wrybread created this gist
May 22, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,96 @@ #!/usr/bin/python ''' This solves a problem in DJI drone cameras where the file numbers never go above DJI_0999.JPG. This will add some number to the file number (default 1,000,000). For example it'll change DJI_0999.JPG to DJI_1000999.JPG It keeps track of the last number it added (in 0filename_fixer-v2.dat), so run it on the card before importing the pics and vids. It processes files in subdirectories of the script's directory, so I keep it in the DCIM folder of the SD 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 1000). It's also smart enough to handle files with extra text and numbers added, like "DJI_0088 - Part 2.MP4" becomes "DJI_1000088 - Part 2.MP4" ''' from __future__ import print_function import sys, os, re # 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 #listing = os.listdir(directory) #for f in listing: 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, "") 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()