From e78e69d35a321bc20236e66bb79c1ee5da846683 Mon Sep 17 00:00:00 2001 From: ChaotiCryptidz Date: Sun, 6 Feb 2022 15:51:42 +0000 Subject: [PATCH] add replaygain tag support --- musicutil/commands/copy_command.py | 17 +++++++++++++++++ musicutil/commands/process_command.py | 16 ++++++++++++++++ musicutil/commands/transcode_command.py | 16 +++++++++++++--- musicutil/utils/do_replaygain.py | 13 ++++++++----- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/musicutil/commands/copy_command.py b/musicutil/commands/copy_command.py index d467a91..5a2de80 100644 --- a/musicutil/commands/copy_command.py +++ b/musicutil/commands/copy_command.py @@ -1,3 +1,4 @@ +from musicutil.utils.do_replaygain import do_replaygain from ..types import File from ..utils.scan_for_music import scan_for_music from ..utils.load_tag_information import load_tag_information @@ -21,6 +22,7 @@ class CopyCommandArgs(): custom_transcoder_config: str single_directory: bool skip_existing: bool + skip_replaygain: bool def add_copy_command(subparsers): copy_parser = subparsers.add_parser('copy') @@ -44,11 +46,15 @@ def add_copy_command(subparsers): copy_parser.add_argument( '--skip-existing', action='store_true') + copy_parser.add_argument( + '--skip-replaygain', + action='store_true') copy_parser.add_argument( '--single-directory', action='store_true') def get_copy_args(args) -> CopyCommandArgs: + print(args) command_args = CopyCommandArgs() command_args.src = args.src command_args.dest = args.dest @@ -56,6 +62,7 @@ def get_copy_args(args) -> CopyCommandArgs: command_args.custom_transcoder_config = args.custom_transcoder_config command_args.single_directory = args.single_directory command_args.skip_existing = args.skip_existing + command_args.skip_replaygain = args.skip_replaygain return command_args class CopyCommand(): @@ -76,6 +83,8 @@ class CopyCommand(): self.transcode_files() if self.args.single_directory: self.create_mappings() + if not self.args.skip_replaygain: + self.add_replaygain_tags() def scan_for_music(self): print("Scanning For Music") @@ -187,3 +196,11 @@ class CopyCommand(): f.write("\n".join([ f"{file.path_from_src} <- {file.filename}" for file in self.state.files ])) + + def add_replaygain_tags(self): + print("Adding ReplayGain Tags") + + for file in self.state.files: + print(f"Adding ReplayGain Tags to \"{file.filename}.{file.extension}\"") + + do_replaygain(file) \ No newline at end of file diff --git a/musicutil/commands/process_command.py b/musicutil/commands/process_command.py index 809a8ee..b63024f 100644 --- a/musicutil/commands/process_command.py +++ b/musicutil/commands/process_command.py @@ -1,3 +1,4 @@ +from musicutil.utils.do_replaygain import do_replaygain from ..types import File from ..utils.scan_for_music import scan_for_music from ..utils.load_tag_information import load_tag_information @@ -13,6 +14,7 @@ class ProcessCommandState: class ProcessCommandArgs: src: str dry_run: bool + skip_replaygain: bool def add_process_command(subparsers): process_parser = subparsers.add_parser('process') @@ -22,11 +24,15 @@ def add_process_command(subparsers): help='src base music directory') process_parser.add_argument( '--dry-run', action='store_true') + process_parser.add_argument( + '--skip-replaygain', + action='store_true') 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 return command_args class ProcessCommand(): @@ -39,6 +45,8 @@ 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() def scan_for_music(self): print("Scanning For Music") @@ -83,3 +91,11 @@ class ProcessCommand(): index = self.state.files.index(file) self.state.files[index] = self._rename_file( file) + + def add_replaygain_tags(self): + print("Adding ReplayGain Tags") + + for file in self.state.files: + print(f"Adding ReplayGain Tags to \"{file.filename}.{file.extension}\"") + + do_replaygain(file) \ No newline at end of file diff --git a/musicutil/commands/transcode_command.py b/musicutil/commands/transcode_command.py index 4425d81..2d690a1 100644 --- a/musicutil/commands/transcode_command.py +++ b/musicutil/commands/transcode_command.py @@ -1,3 +1,4 @@ +from musicutil.utils.do_replaygain import do_replaygain from ..utils.transcoder import get_transcode_config, transcode, TranscodeConfig from ..utils.scan_for_music import file_from_path from ..transcode_presets import print_transcode_presets, transcode_presets @@ -11,6 +12,7 @@ class TranscodeCommandArgs: transcode_preset: str ignore_extension: bool custom_transcoder_config_path: str + skip_replaygain: bool def add_transcode_command(subparsers): transcode_parser = subparsers.add_parser('transcode') @@ -34,6 +36,9 @@ def add_transcode_command(subparsers): '--custom-transcoder-config', type=str, help='custom transcoder config') + transcode_parser.add_argument( + '--skip-replaygain', + action='store_true') def get_transcode_args(args) -> TranscodeCommandArgs: command_args = TranscodeCommandArgs() @@ -42,6 +47,7 @@ def get_transcode_args(args) -> TranscodeCommandArgs: command_args.transcode_preset = args.transcode_preset command_args.ignore_extension = args.ignore_extension command_args.custom_transcoder_config_path = args.custom_transcoder_config + command_args.skip_replaygain = args.skip_replaygain return command_args class TranscodeCommand: @@ -54,10 +60,10 @@ class TranscodeCommand: exit() print("Transcoding...") - input_file = file_from_path(Path(self.src), "") + input_file = file_from_path(Path(self.args.src), "") if self.args.custom_transcoder_config_path is None or len(self.args.custom_transcoder_config_path) == 0: - trans_config = get_transcode_config(input_file, self.args.transcode_preset) + trans_config = get_transcode_config(self.args.transcode_preset) else: with open(self.args.custom_transcoder_config_path, "r+") as file: trans_config = TranscodeConfig() @@ -73,4 +79,8 @@ class TranscodeCommand: f"or run with --ignore-extension" ) exit() - transcode(input_file, trans_config, self.args.dest) \ No newline at end of file + transcode(input_file, trans_config, self.args.dest) + + if not self.args.skip_replaygain: + print("Adding ReplayGain Tags") + do_replaygain(output_file, False) \ No newline at end of file diff --git a/musicutil/utils/do_replaygain.py b/musicutil/utils/do_replaygain.py index fba838d..71656ca 100644 --- a/musicutil/utils/do_replaygain.py +++ b/musicutil/utils/do_replaygain.py @@ -3,11 +3,14 @@ from ..meta import r128gain_path, ffmpeg_path from subprocess import run as run_command - -def do_replaygain(file: File): - run_command([ +def do_replaygain(file: File, skip_if_existing: bool = True) -> None: + command_args = [ r128gain_path, "-f", ffmpeg_path, - "-s", + "-v", "warning", file.join_path_to() - ]) \ No newline at end of file + ] + + if skip_if_existing: + command_args.append("-s") + run_command(command_args) \ No newline at end of file