From b32247eff4c10a78c3a23efa509d4c6273590227 Mon Sep 17 00:00:00 2001 From: Lucas Vivohost Date: Mon, 14 Sep 2026 12:47:09 +0200 Subject: [PATCH] initial audiobook python script intendet to relieve all metadata scripts --- audiobook_metadata/metadata_audiobooks.py | 60 +++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 audiobook_metadata/metadata_audiobooks.py diff --git a/audiobook_metadata/metadata_audiobooks.py b/audiobook_metadata/metadata_audiobooks.py new file mode 100644 index 0000000..d3e9f43 --- /dev/null +++ b/audiobook_metadata/metadata_audiobooks.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +import sys +import subprocess +from pathlib import Path + + +ROOT = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(".") + +audio_extensions = { + ".m4b", + ".m4a", + ".mp3", + ".flac", + ".ogg", +} + + +def get_metadata(field, file): + try: + result = subprocess.run( + ["kid3-cli", "-c", f"get {field}", str(file)], + stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, + text=True, + ) + return result.stdout.strip() + + except (OSError, subprocess.SubprocessError): + return "" + + +# Include ROOT itself, matching find "$ROOT" -type d +for directory in [ROOT] + [p for p in ROOT.rglob("*") if p.is_dir()]: + audio_files = sorted( + ( + file + for file in directory.iterdir() + if file.is_file() + and file.suffix.lower() in audio_extensions + ), + key=lambda p: str(p), + ) + + if not audio_files: + continue + + first_file = audio_files[0] + + artist = get_metadata("artist", first_file) + album = get_metadata("album", first_file) + composer = get_metadata("composer", first_file) + comment = get_metadata("comment", first_file) + + print("====≠===============================") + print(f"Folder: {directory.name}") + print(f"Album: {album}") + print(f"Artist: {artist}") + print(f"Narrator: {composer}") + print(f"Comment: {comment}")