You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux下用Python subprocess调用ExifTool仅提取拍摄日期可行吗?

使用ExifTool通过Python subprocess提取媒体文件拍摄日期(Linux)

核心ExifTool命令

要仅获取拍摄日期而非完整元数据,可使用ExifTool的定向参数:

# 照片优先用DateTimeOriginal,视频优先用MediaCreateDate
exiftool -DateTimeOriginal -s3 /path/to/photo.jpg
exiftool -MediaCreateDate -s3 /path/to/video.mp4

参数说明:

  • -DateTimeOriginal:照片类文件的标准拍摄日期标签
  • -MediaCreateDate:视频类文件的创建/拍摄日期标签
  • -s3:仅输出标签对应的值,不附带标签名称,简化后续处理

Python subprocess实现代码

import subprocess

def extract_shot_date(file_path):
    # 可根据文件类型调整标签,或多标签兼容不同文件
    cmd = ["exiftool", "-DateTimeOriginal", "-CreateDate", "-MediaCreateDate", "-s3", file_path]
    try:
        # 捕获命令输出,转为文本格式
        result = subprocess.run(cmd, capture_output=True, text=True, check=True)
        # 去除输出末尾换行,返回第一个有效日期
        shot_date = result.stdout.strip().split('\n')[0]
        return shot_date if shot_date else None
    except subprocess.CalledProcessError:
        # 处理文件无法读取或无对应标签的情况
        return None

# 使用示例
target_file = "/home/user/photos/trip.jpg"
date = extract_shot_date(target_file)
if date:
    print(f"拍摄日期:{date}")
else:
    print("未提取到有效拍摄日期")

注意事项

  • 确保系统已安装ExifTool,Debian/Ubuntu系可执行sudo apt install libimage-exiftool-perl完成安装
  • 批量处理时,可遍历文件目录路径列表,循环调用上述函数
  • 部分特殊格式文件可能标签不同,可通过exiftool /path/to/file查看所有标签后调整参数

内容的提问来源于stack exchange,提问作者Gowtham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 08:02:37