From 0dcc686b19108baae3356c0c90bb58afa279c6bf Mon Sep 17 00:00:00 2001 From: ChaotiCryptidz Date: Sun, 6 Feb 2022 16:13:06 +0000 Subject: [PATCH] fallback to ffmpeg for tag extraction when mutagen fails --- musicutil/utils/load_tag_information.py | 65 +++++++++++++++++++++++-- shell.nix | 2 +- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/musicutil/utils/load_tag_information.py b/musicutil/utils/load_tag_information.py index 39e6b40..90ff6a8 100644 --- a/musicutil/utils/load_tag_information.py +++ b/musicutil/utils/load_tag_information.py @@ -1,10 +1,13 @@ from ..types import File, Tags +from ..meta import ffprobe_path + +from subprocess import run as run_command +from json import loads as load_json_string from mutagen.mp3 import EasyMP3 as MP3 from mutagen.flac import FLAC - -def load_tag_information(file: File) -> Tags: +def load_tag_information_mutagen(file: File) -> Tags: path = file.join_path_to() tags = Tags() if file.extension == "mp3": @@ -16,6 +19,60 @@ def load_tag_information(file: File) -> Tags: tags.title = flac["title"][0] tags.artist = flac["artist"][0] else: - print("Invalid / Unsupported Container") - exit() + raise Exception("Could Not Load Tags Using Mutagen") return tags + + + +def load_tag_information_ffmpeg(file: File) -> Tags: + path = file.join_path_to() + tags = Tags() + + command_args = [ + ffprobe_path, + "-v", "quiet", + "-print_format", "json", + "-show_format", + path + ] + + ffprobe_output = run_command(command_args, capture_output=True).stdout + + data = load_json_string(ffprobe_output) + file_tags = data["format"]["tags"] + + title = None + artist = None + + try: + if "title" in file_tags.keys(): + title = file_tags["title"] + elif "TITLE" in file_tags.keys(): + title = file_tags["TITLE"] + + if "artist" in file_tags.keys(): + artist = file_tags["artist"] + elif "ARTIST" in file_tags.keys(): + artist = file_tags["ARTIST"] + except Exception as e: + print(data["format"]["tags"], file, e) + exit() + + if title is None or artist is None: + raise Exception("Could Not Load Tags Using FFprobe") + + tags.title = title + tags.artist = artist + return tags + +def load_tag_information(file: File) -> Tags: + try: + tags = load_tag_information_mutagen(file) + return tags + except: + try: + tags = load_tag_information_ffmpeg(file) + return tags + except: + print(f"Could not get tags for file {file.filename}. Exiting.") + exit() diff --git a/shell.nix b/shell.nix index 2975e87..8e0f2a7 100644 --- a/shell.nix +++ b/shell.nix @@ -2,6 +2,6 @@ let fold-to-ascii = (py: py.callPackage ./nix-extra-deps/fold-to-ascii.nix { }); my_python = pkgs.python39.withPackages - (py: with py; [ py.mutagen (fold-to-ascii py) py.autopep8 py.pyyaml ]); + (py: with py; [ (fold-to-ascii py) py.mutagen py.autopep8 py.pyyaml ]); in pkgs.mkShell { packages = with pkgs; [ my_python ffmpeg fd r128gain ]; }