Advertisement
Yesideez

picrenamer.py

Jun 7th, 2024
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | Source Code | 0 0
  1. # $VER: picrenamer.py v1.01 (08-Jun-2024) by Zeb/Slipstream
  2.  
  3. # bugs:
  4. #   * the picture exif tag routine is not bug free and needs to be replaced with
  5. #     a proper EXIF tag reading routine that works on all images and is fail safe.
  6. #   * the parsing routine currently fails to check if a parameter value is present
  7.  
  8. from sys import argv
  9. from glob import glob
  10. from pathlib import Path
  11. from PIL import Image
  12. from os import rename
  13. from moviepy.editor import VideoFileClip
  14. import ffmpeg
  15.  
  16. #default values
  17. strDirName="."
  18. blnCommit=False
  19. strAdd=""
  20.  
  21. def checkSlash(str):
  22.   if str[len(str)-1]=="/":
  23.     return True
  24.   else:
  25.     return False
  26.  
  27. intCount=[0,0]
  28.  
  29. #parse the arguments on the command line
  30. intArgC=len(argv)
  31. if intArgC>1: #if no arguments use default values set at the top
  32.   for i in range(1,intArgC):
  33.     if argv[i]=="-d": #check for a directory name
  34.       strDirName=argv[i+1]
  35.       i+=1
  36.     if argv[i]=="commit": #check for commit
  37.       blnCommit=True
  38.     if argv[i]=="-s": #check for a string to add
  39.       strAdd=argv[i+1]
  40.       i+=1
  41.  
  42. if not checkSlash(strDirName):
  43.   strDirName=strDirName+"/"
  44. print(f"Dir Name: {strDirName}")
  45. lstFiles=glob(strDirName+"*.*") #[j|J][p|P][g|G]
  46. lstFiles.sort()
  47.  
  48. for i in lstFiles:
  49.   tmpFileName=i.lower()
  50.   if tmpFileName.find(".jpg")>0:
  51. #    print(f"DBG: {tmpFileName} {tmpFileName.find('.jpg')}")
  52.     intCount[0]+=1
  53.     im=Image.open(i)
  54.     tmp=im.getexif()
  55.     dt=tmp[306].split(" ") #extract the date time into a list
  56.     tm=dt[1].split(":")    #extract the date into a list
  57.     dt=dt[0].split(":")    #extract the time into a list
  58.     strAfter=f"{strDirName}{dt[0]}-{dt[1]}-{dt[2]} {tm[0]}-{tm[1]}-{tm[2]}"
  59.     if len(strAdd)>0:
  60.       strAfter=strAfter+" "+strAdd
  61.     strAfter=strAfter+".jpg"
  62.     file=Path(strAfter)
  63.     if blnCommit:
  64.       if file.exists():
  65.         print(f"ERR: File '{strAfter}' exists ({i})")
  66.       else:
  67.         #print(f"Rename '{i}' to '{strAfter}'")
  68.         rename(i,strAfter)
  69.     else:
  70.       print(f">'{i}' to '{strAfter}'")
  71.   if tmpFileName.find(".mp4")>0:
  72.     intCount[1]+=1
  73.     video=ffmpeg.probe(i)
  74.     strCreationDT=video['streams'][0]['tags']['creation_time']
  75.     strCreationD=strCreationDT[0:10]
  76.     strCreationT=strCreationDT[11:19]
  77.     strCreationT=strCreationT.replace(':','-')
  78. #    print(f"Find: {strCreationT.find(':')}")
  79. #    print(f"Creation Date/Time: {strCreationD} {strCreationT}")
  80.     strAfter=f"{strDirName}{strCreationD} {strCreationT}"
  81.     if len(strAdd)>0:
  82.       strAfter=strAfter+" "+strAdd
  83.     strAfter=strAfter+".mp4"
  84.     file=Path(strAfter)
  85.     if blnCommit:
  86.       if file.exists():
  87.         print(f"ERR: File '{strAfter}' exists ({i})")
  88.       else:
  89.         #print(f"Rename '{i}' to '{strAfter}'")
  90.         rename(i,strAfter)
  91.     else:
  92.       print(f">'{i}' to '{strAfter}'")
  93.  
  94. if blnCommit:
  95.   print(f"Files: {intCount[0]+intCount[1]}/{len(lstFiles)}")
  96. else:
  97.   print(f"Files: {intCount[0]+intCount[1]} (nothing has been changed)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement