add replaygain tag support

This commit is contained in:
ChaotiCryptidz 2022-02-06 15:51:42 +00:00
parent 2e202c23df
commit e78e69d35a
4 changed files with 54 additions and 8 deletions

View file

@ -1,3 +1,4 @@
from musicutil.utils.do_replaygain import do_replaygain
from ..types import File from ..types import File
from ..utils.scan_for_music import scan_for_music from ..utils.scan_for_music import scan_for_music
from ..utils.load_tag_information import load_tag_information from ..utils.load_tag_information import load_tag_information
@ -21,6 +22,7 @@ class CopyCommandArgs():
custom_transcoder_config: str custom_transcoder_config: str
single_directory: bool single_directory: bool
skip_existing: bool skip_existing: bool
skip_replaygain: bool
def add_copy_command(subparsers): def add_copy_command(subparsers):
copy_parser = subparsers.add_parser('copy') copy_parser = subparsers.add_parser('copy')
@ -44,11 +46,15 @@ def add_copy_command(subparsers):
copy_parser.add_argument( copy_parser.add_argument(
'--skip-existing', '--skip-existing',
action='store_true') action='store_true')
copy_parser.add_argument(
'--skip-replaygain',
action='store_true')
copy_parser.add_argument( copy_parser.add_argument(
'--single-directory', '--single-directory',
action='store_true') action='store_true')
def get_copy_args(args) -> CopyCommandArgs: def get_copy_args(args) -> CopyCommandArgs:
print(args)
command_args = CopyCommandArgs() command_args = CopyCommandArgs()
command_args.src = args.src command_args.src = args.src
command_args.dest = args.dest 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.custom_transcoder_config = args.custom_transcoder_config
command_args.single_directory = args.single_directory command_args.single_directory = args.single_directory
command_args.skip_existing = args.skip_existing command_args.skip_existing = args.skip_existing
command_args.skip_replaygain = args.skip_replaygain
return command_args return command_args
class CopyCommand(): class CopyCommand():
@ -76,6 +83,8 @@ class CopyCommand():
self.transcode_files() self.transcode_files()
if self.args.single_directory: if self.args.single_directory:
self.create_mappings() self.create_mappings()
if not self.args.skip_replaygain:
self.add_replaygain_tags()
def scan_for_music(self): def scan_for_music(self):
print("Scanning For Music") print("Scanning For Music")
@ -187,3 +196,11 @@ class CopyCommand():
f.write("\n".join([ f.write("\n".join([
f"{file.path_from_src} <- {file.filename}" for file in self.state.files 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)

View file

@ -1,3 +1,4 @@
from musicutil.utils.do_replaygain import do_replaygain
from ..types import File from ..types import File
from ..utils.scan_for_music import scan_for_music from ..utils.scan_for_music import scan_for_music
from ..utils.load_tag_information import load_tag_information from ..utils.load_tag_information import load_tag_information
@ -13,6 +14,7 @@ class ProcessCommandState:
class ProcessCommandArgs: class ProcessCommandArgs:
src: str src: str
dry_run: bool dry_run: bool
skip_replaygain: bool
def add_process_command(subparsers): def add_process_command(subparsers):
process_parser = subparsers.add_parser('process') process_parser = subparsers.add_parser('process')
@ -22,11 +24,15 @@ def add_process_command(subparsers):
help='src base music directory') help='src base music directory')
process_parser.add_argument( process_parser.add_argument(
'--dry-run', action='store_true') '--dry-run', action='store_true')
process_parser.add_argument(
'--skip-replaygain',
action='store_true')
def get_process_args(args) -> ProcessCommandArgs: def get_process_args(args) -> ProcessCommandArgs:
command_args = ProcessCommandArgs() command_args = ProcessCommandArgs()
command_args.src = args.src command_args.src = args.src
command_args.dry_run = args.dry_run command_args.dry_run = args.dry_run
command_args.skip_replaygain = args.skip_replaygain
return command_args return command_args
class ProcessCommand(): class ProcessCommand():
@ -39,6 +45,8 @@ class ProcessCommand():
self.scan_for_music() self.scan_for_music()
self.load_tag_information() self.load_tag_information()
self.rename_files() self.rename_files()
if (not self.args.skip_replaygain) and (not self.args.dry_run):
self.add_replaygain_tags()
def scan_for_music(self): def scan_for_music(self):
print("Scanning For Music") print("Scanning For Music")
@ -83,3 +91,11 @@ class ProcessCommand():
index = self.state.files.index(file) index = self.state.files.index(file)
self.state.files[index] = self._rename_file( self.state.files[index] = self._rename_file(
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)

View file

@ -1,3 +1,4 @@
from musicutil.utils.do_replaygain import do_replaygain
from ..utils.transcoder import get_transcode_config, transcode, TranscodeConfig from ..utils.transcoder import get_transcode_config, transcode, TranscodeConfig
from ..utils.scan_for_music import file_from_path from ..utils.scan_for_music import file_from_path
from ..transcode_presets import print_transcode_presets, transcode_presets from ..transcode_presets import print_transcode_presets, transcode_presets
@ -11,6 +12,7 @@ class TranscodeCommandArgs:
transcode_preset: str transcode_preset: str
ignore_extension: bool ignore_extension: bool
custom_transcoder_config_path: str custom_transcoder_config_path: str
skip_replaygain: bool
def add_transcode_command(subparsers): def add_transcode_command(subparsers):
transcode_parser = subparsers.add_parser('transcode') transcode_parser = subparsers.add_parser('transcode')
@ -34,6 +36,9 @@ def add_transcode_command(subparsers):
'--custom-transcoder-config', '--custom-transcoder-config',
type=str, type=str,
help='custom transcoder config') help='custom transcoder config')
transcode_parser.add_argument(
'--skip-replaygain',
action='store_true')
def get_transcode_args(args) -> TranscodeCommandArgs: def get_transcode_args(args) -> TranscodeCommandArgs:
command_args = TranscodeCommandArgs() command_args = TranscodeCommandArgs()
@ -42,6 +47,7 @@ def get_transcode_args(args) -> TranscodeCommandArgs:
command_args.transcode_preset = args.transcode_preset command_args.transcode_preset = args.transcode_preset
command_args.ignore_extension = args.ignore_extension command_args.ignore_extension = args.ignore_extension
command_args.custom_transcoder_config_path = args.custom_transcoder_config command_args.custom_transcoder_config_path = args.custom_transcoder_config
command_args.skip_replaygain = args.skip_replaygain
return command_args return command_args
class TranscodeCommand: class TranscodeCommand:
@ -54,10 +60,10 @@ class TranscodeCommand:
exit() exit()
print("Transcoding...") 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: 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: else:
with open(self.args.custom_transcoder_config_path, "r+") as file: with open(self.args.custom_transcoder_config_path, "r+") as file:
trans_config = TranscodeConfig() trans_config = TranscodeConfig()
@ -74,3 +80,7 @@ class TranscodeCommand:
) )
exit() exit()
transcode(input_file, trans_config, self.args.dest) transcode(input_file, trans_config, self.args.dest)
if not self.args.skip_replaygain:
print("Adding ReplayGain Tags")
do_replaygain(output_file, False)

View file

@ -3,11 +3,14 @@ from ..meta import r128gain_path, ffmpeg_path
from subprocess import run as run_command from subprocess import run as run_command
def do_replaygain(file: File, skip_if_existing: bool = True) -> None:
def do_replaygain(file: File): command_args = [
run_command([
r128gain_path, r128gain_path,
"-f", ffmpeg_path, "-f", ffmpeg_path,
"-s", "-v", "warning",
file.join_path_to() file.join_path_to()
]) ]
if skip_if_existing:
command_args.append("-s")
run_command(command_args)