84 lines
2.2 KiB
Python
Executable File
84 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Interactively view, edit, or remove metadata fields in one or more files."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
|
|
from tools.console import (
|
|
clear_screen,
|
|
launch_terminal_if_needed,
|
|
pause_if_interactive,
|
|
prompt_yes_no,
|
|
)
|
|
from tools.exiftool import require_exiftool, run_exiftool_json
|
|
from tools.filesystem import collect_files, input_paths
|
|
from tools.metadata_editor import apply_fields, edit_fields, write_arguments
|
|
|
|
|
|
def run(argv: list[str]) -> int:
|
|
try:
|
|
args = input_paths(argv)
|
|
except RuntimeError as exc:
|
|
print(exc)
|
|
return 1
|
|
if not args:
|
|
print("No inputs selected.")
|
|
return 0
|
|
files = collect_files(args, print_missing=True)
|
|
if not files:
|
|
print("No files found.")
|
|
return 1
|
|
try:
|
|
exiftool = require_exiftool()
|
|
metadata = run_exiftool_json(exiftool, files, quicktime_utc=False)
|
|
except (RuntimeError, subprocess.CalledProcessError, json.JSONDecodeError) as exc:
|
|
print(f"Could not read metadata: {exc}")
|
|
return 1
|
|
|
|
fields_state = None
|
|
while True:
|
|
try:
|
|
fields = edit_fields(metadata, fields_state)
|
|
except RuntimeError as exc:
|
|
print(exc)
|
|
return 1
|
|
if fields is None:
|
|
print("Nothing changed.")
|
|
return 0
|
|
args = write_arguments(fields)
|
|
clear_screen(scrollback=True)
|
|
if not args:
|
|
print("No metadata changes selected.")
|
|
else:
|
|
print(f"Planned metadata changes for {len(files)} file(s):")
|
|
for argument in args:
|
|
print(f" {argument}")
|
|
if prompt_yes_no("Apply these changes?", True):
|
|
break
|
|
fields_state = fields
|
|
|
|
failures = apply_fields(exiftool, files, fields)
|
|
if failures:
|
|
print("\nSome files failed:")
|
|
for failure in failures:
|
|
print(f" - {failure}")
|
|
return 1
|
|
print("\nDone.")
|
|
return 0
|
|
|
|
|
|
def main(argv: list[str]) -> int:
|
|
if launch_terminal_if_needed(argv):
|
|
return 0
|
|
try:
|
|
return run(argv)
|
|
finally:
|
|
pause_if_interactive()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main(sys.argv))
|