fallback to ffmpeg for tag extraction when mutagen fails

This commit is contained in:
ChaotiCryptidz 2022-02-06 16:13:06 +00:00
parent e78e69d35a
commit 0dcc686b19
2 changed files with 62 additions and 5 deletions

View file

@ -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()

View file

@ -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 ]; }