From 07d4daf40dc5862c3e265812652236e3203fdcec Mon Sep 17 00:00:00 2001 From: ChaotiCryptidz Date: Thu, 10 Feb 2022 12:44:15 +0000 Subject: [PATCH] add more options to replaygain on process --- .direnv/flake-profile | 1 + .direnv/flake-profile-1-link | 1 + .envrc | 2 +- flake.nix | 7 ++++--- musicutil/commands/process_command.py | 21 +++++++++++++-------- musicutil/types.py | 4 +++- musicutil/utils/load_tag_information.py | 12 +++++++++--- 7 files changed, 32 insertions(+), 16 deletions(-) create mode 120000 .direnv/flake-profile create mode 120000 .direnv/flake-profile-1-link diff --git a/.direnv/flake-profile b/.direnv/flake-profile new file mode 120000 index 0000000..0c05709 --- /dev/null +++ b/.direnv/flake-profile @@ -0,0 +1 @@ +flake-profile-1-link \ No newline at end of file diff --git a/.direnv/flake-profile-1-link b/.direnv/flake-profile-1-link new file mode 120000 index 0000000..774ae49 --- /dev/null +++ b/.direnv/flake-profile-1-link @@ -0,0 +1 @@ +/nix/store/636l6zba529dbpcnyr0vmnq5kynfcqpr-nix-shell-env \ No newline at end of file diff --git a/.envrc b/.envrc index 977c7a4..3550a30 100644 --- a/.envrc +++ b/.envrc @@ -1 +1 @@ -use nix \ No newline at end of file +use flake diff --git a/flake.nix b/flake.nix index 30e16ed..567bdfa 100644 --- a/flake.nix +++ b/flake.nix @@ -33,6 +33,7 @@ ''; doCheck = false; + buildInputs = with final; [ r128gain ffmpeg ]; propagatedBuildInputs = with final.python3Packages; [ mutagen @@ -59,10 +60,10 @@ devShell = pkgs.mkShell { inputsFrom = [ self.packages.${system}.musicutil ]; - buildInputs = with pkgs; [ - nixUnstable + buildInputs = with pkgs; [ nixUnstable ffmpeg r128gain ]; + propagatedBuildInputs = with pkgs.python3Packages; [ mutagen - self.packages.${system}.fold-to-ascii + pkgs.fold-to-ascii pyyaml ]; }; diff --git a/musicutil/commands/process_command.py b/musicutil/commands/process_command.py index 6cb92d4..00712b6 100644 --- a/musicutil/commands/process_command.py +++ b/musicutil/commands/process_command.py @@ -15,8 +15,7 @@ class ProcessCommandState: class ProcessCommandArgs: src: str dry_run: bool - skip_replaygain: bool - + replaygain: str def add_process_command(subparsers): process_parser = subparsers.add_parser('process') @@ -27,15 +26,18 @@ def add_process_command(subparsers): process_parser.add_argument( '--dry-run', action='store_true') process_parser.add_argument( - '--skip-replaygain', - action='store_true') + '--replaygain', + type=str, + default="skip_existing", + help="skip,skip_existing,force" + ) def get_process_args(args) -> ProcessCommandArgs: command_args = ProcessCommandArgs() command_args.src = args.src command_args.dry_run = args.dry_run - command_args.skip_replaygain = args.skip_replaygain + command_args.replaygain = args.replaygain return command_args @@ -49,9 +51,9 @@ class ProcessCommand(): self.scan_for_music() self.load_tag_information() self.rename_files() - if (not self.args.skip_replaygain) and ( - not self.args.dry_run): - self.add_replaygain_tags() + if not self.args.dry_run: + if self.args.replaygain != "skip": + self.add_replaygain_tags() def scan_for_music(self): print("Scanning For Music") @@ -101,6 +103,9 @@ class ProcessCommand(): print("Adding ReplayGain Tags") for file in self.state.files: + if self.args.replaygain == "skip_existing": + if file.tags.has_replaygain: + continue print( f"Adding ReplayGain Tags to \"{file.filename}.{file.extension}\"") diff --git a/musicutil/types.py b/musicutil/types.py index 9d225bd..5d6e6da 100644 --- a/musicutil/types.py +++ b/musicutil/types.py @@ -1,11 +1,13 @@ class Tags: title = "" artist = "" + has_replaygain = False def to_dict(self): return { "title": self.title, - "artist": self.artist + "artist": self.artist, + "has_replaygain": self.has_replaygain } def __repr__(self): diff --git a/musicutil/utils/load_tag_information.py b/musicutil/utils/load_tag_information.py index a5244cd..aafa443 100644 --- a/musicutil/utils/load_tag_information.py +++ b/musicutil/utils/load_tag_information.py @@ -4,7 +4,7 @@ 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.mp3 import MP3, EasyMP3 from mutagen.flac import FLAC @@ -12,11 +12,16 @@ def load_tag_information_mutagen(file: File) -> Tags: path = file.join_path_to() tags = Tags() if file.extension == "mp3": + easymp3 = EasyMP3(path) mp3 = MP3(path) - tags.title = mp3["title"][0] - tags.artist = mp3["artist"][0] + tags.title = easymp3["title"][0] + tags.artist = easymp3["artist"][0] + if "REPLAYGAIN_TRACK_GAIN" in mp3.keys() or "TXXX:REPLAYGAIN_TRACK_PEAK" in mp3.keys(): + tags.has_replaygain = True elif file.extension == "flac": flac = FLAC(path) + if "replaygain_track_peak" in flac.keys(): + tags.has_replaygain = True tags.title = flac["title"][0] tags.artist = flac["artist"][0] else: @@ -46,6 +51,7 @@ def load_tag_information_ffmpeg(file: File) -> Tags: artist = None try: + print(file_tags.keys()) if "title" in file_tags.keys(): title = file_tags["title"] elif "TITLE" in file_tags.keys():