Files
media-batch-tools/view_metadata.py
T
2026-06-03 13:29:14 +00:00

85 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""
Show metadata for one file.
Drag a file onto this script, or run:
python3 view_metadata.py path/to/file/or/directory [...]
If more than one file is provided or found, only the first file is shown.
"""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
from shutil import which
def first_file_from_path(path: Path) -> Path | None:
path = path.expanduser()
if path.is_file():
return path.resolve()
if path.is_dir():
for child in sorted(path.rglob("*"), key=lambda item: str(item).lower()):
if child.is_file():
return child.resolve()
return None
def choose_file(args: list[str]) -> Path | None:
for arg in args:
file_path = first_file_from_path(Path(arg))
if file_path is not None:
return file_path
return None
def pause_if_interactive() -> None:
if sys.stdin.isatty():
try:
input("\nPress Enter to close...")
except EOFError:
pass
def run(argv: list[str]) -> int:
if len(argv) < 2:
print("Usage: python3 view_metadata.py path/to/file/or/directory [...]")
return 2
exiftool = which("exiftool")
if exiftool is None:
print("exiftool was not found on PATH.")
return 1
file_path = choose_file(argv[1:])
if file_path is None:
print("No file found.")
return 1
print(f"Showing metadata for: {file_path}\n", flush=True)
command = [
exiftool,
"-a",
"-u",
"-ee",
"-G0:1",
"-s",
"-charset",
"filename=UTF8",
str(file_path),
]
return subprocess.run(command).returncode
def main(argv: list[str]) -> int:
try:
return run(argv)
finally:
pause_if_interactive()
if __name__ == "__main__":
raise SystemExit(main(sys.argv))