Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # $VER: picrenamer.py v1.01 (08-Jun-2024) by Zeb/Slipstream
- # bugs:
- # * the picture exif tag routine is not bug free and needs to be replaced with
- # a proper EXIF tag reading routine that works on all images and is fail safe.
- # * the parsing routine currently fails to check if a parameter value is present
- from sys import argv
- from glob import glob
- from pathlib import Path
- from PIL import Image
- from os import rename
- from moviepy.editor import VideoFileClip
- import ffmpeg
- #default values
- strDirName="."
- blnCommit=False
- strAdd=""
- def checkSlash(str):
- if str[len(str)-1]=="/":
- return True
- else:
- return False
- intCount=[0,0]
- #parse the arguments on the command line
- intArgC=len(argv)
- if intArgC>1: #if no arguments use default values set at the top
- for i in range(1,intArgC):
- if argv[i]=="-d": #check for a directory name
- strDirName=argv[i+1]
- i+=1
- if argv[i]=="commit": #check for commit
- blnCommit=True
- if argv[i]=="-s": #check for a string to add
- strAdd=argv[i+1]
- i+=1
- if not checkSlash(strDirName):
- strDirName=strDirName+"/"
- print(f"Dir Name: {strDirName}")
- lstFiles=glob(strDirName+"*.*") #[j|J][p|P][g|G]
- lstFiles.sort()
- for i in lstFiles:
- tmpFileName=i.lower()
- if tmpFileName.find(".jpg")>0:
- # print(f"DBG: {tmpFileName} {tmpFileName.find('.jpg')}")
- intCount[0]+=1
- im=Image.open(i)
- tmp=im.getexif()
- dt=tmp[306].split(" ") #extract the date time into a list
- tm=dt[1].split(":") #extract the date into a list
- dt=dt[0].split(":") #extract the time into a list
- strAfter=f"{strDirName}{dt[0]}-{dt[1]}-{dt[2]} {tm[0]}-{tm[1]}-{tm[2]}"
- if len(strAdd)>0:
- strAfter=strAfter+" "+strAdd
- strAfter=strAfter+".jpg"
- file=Path(strAfter)
- if blnCommit:
- if file.exists():
- print(f"ERR: File '{strAfter}' exists ({i})")
- else:
- #print(f"Rename '{i}' to '{strAfter}'")
- rename(i,strAfter)
- else:
- print(f">'{i}' to '{strAfter}'")
- if tmpFileName.find(".mp4")>0:
- intCount[1]+=1
- video=ffmpeg.probe(i)
- strCreationDT=video['streams'][0]['tags']['creation_time']
- strCreationD=strCreationDT[0:10]
- strCreationT=strCreationDT[11:19]
- strCreationT=strCreationT.replace(':','-')
- # print(f"Find: {strCreationT.find(':')}")
- # print(f"Creation Date/Time: {strCreationD} {strCreationT}")
- strAfter=f"{strDirName}{strCreationD} {strCreationT}"
- if len(strAdd)>0:
- strAfter=strAfter+" "+strAdd
- strAfter=strAfter+".mp4"
- file=Path(strAfter)
- if blnCommit:
- if file.exists():
- print(f"ERR: File '{strAfter}' exists ({i})")
- else:
- #print(f"Rename '{i}' to '{strAfter}'")
- rename(i,strAfter)
- else:
- print(f">'{i}' to '{strAfter}'")
- if blnCommit:
- print(f"Files: {intCount[0]+intCount[1]}/{len(lstFiles)}")
- else:
- print(f"Files: {intCount[0]+intCount[1]} (nothing has been changed)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement