This commit is contained in:
huangge1199 2024-08-12 11:41:10 +08:00
parent ace8089776
commit c37f01f39b

View File

@ -3,10 +3,11 @@
# @date 2023/10/23 # @date 2023/10/23
# @file renamePhotoBatch.py # @file renamePhotoBatch.py
import os import os
import glob import os.path
import exifread import time
import os.path, time import exifread
from moviepy.editor import VideoFileClip
# 获取照片的拍摄时间 # 获取照片的拍摄时间
@ -21,14 +22,31 @@ def get_photo_taken_time(image_path):
def get_photo_update_time(image_path): def get_photo_update_time(image_path):
with open(image_path, 'rb') as f: with open(image_path, 'rb') as f:
return time.ctime(os.stat(image_path).st_mtime) timestamp = time.strptime(time.ctime(os.stat(image_path).st_mtime),'%a %b %d %H:%M:%S %Y')
return f"{timestamp.tm_year}{timestamp.tm_mon}{timestamp.tm_mday}_{timestamp.tm_hour}{timestamp.tm_min}{timestamp.tm_sec}"
def get_video_creation_time(video_path):
try:
# 使用 moviepy 读取视频文件
clip = VideoFileClip(video_path)
# 获取视频文件的元数据
metadata = clip.reader.infos
# 获取创建时间
creation_time = metadata.get('creation_time', 'Unknown')
return creation_time
except Exception as e:
print(f"Error: {e}")
return None
def solution(): def solution():
# 获取当前时间并格式化为年月日时分秒毫秒 # 获取当前时间并格式化为年月日时分秒毫秒
# 设置照片所在的文件夹路径 # 设置照片所在的文件夹路径
# folder_path = "C:\\Users\\hyy\\Pictures\\图片" folder_path = "C:\\Users\\hyy\\Pictures\\图片"
folder_path = "C:\\Users\\hyy\\Pictures\\轩辕龙儿的文件收集20240803" # folder_path = "C:\\Users\\hyy\\Pictures\\轩辕龙儿的文件收集20240803"
# 获取文件夹中的所有照片文件 # 获取文件夹中的所有照片文件
# photo_files = glob.glob(os.path.join(folder_path, "*")) # photo_files = glob.glob(os.path.join(folder_path, "*"))
@ -37,16 +55,23 @@ def solution():
# 遍历照片文件并重命名 # 遍历照片文件并重命名
for photo in photo_files: for photo in photo_files:
name = photo name = photo
if name.endswith('.jpeg'):
continue
photo = folder_path+"\\"+photo photo = folder_path+"\\"+photo
creation_time_stamp = get_photo_taken_time(photo) if name.endswith('.MOV'):
creation_time_stamp = get_photo_update_time(photo)
formatted_time = "VIDEO_" + creation_time_stamp
else:
creation_time_stamp = get_photo_taken_time(photo)
formatted_time = "IMG_" + creation_time_stamp.values.replace(':', '').replace(' ', '_')
if creation_time_stamp is None: if creation_time_stamp is None:
continue continue
if name.startswith('IMG'): # if name.startswith('IMG'):
file_name, file_ext = os.path.splitext(photo) # file_name, file_ext = os.path.splitext(photo)
file_name = file_name[:-2] # if file_name.find('-'):
os.rename(photo, f"{file_name}.jpeg") # file_name = file_name[:-2]
continue # os.rename(photo, f"{file_name}{file_ext}")
formatted_time = "IMG_" + creation_time_stamp.values.replace(':', '').replace(' ', '_') # continue
file_name, file_ext = os.path.splitext(photo) file_name, file_ext = os.path.splitext(photo)
if file_name.startswith('.'): if file_name.startswith('.'):
file_name, file_ext = os.path.splitext(file_name) file_name, file_ext = os.path.splitext(file_name)