93 lines
3 KiB
Python
93 lines
3 KiB
Python
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
|
|
|
|
from pathlib import Path
|
|
from json import load as load_json_file
|
|
|
|
|
|
class TranscodeCommandArgs:
|
|
src: str
|
|
dest: str
|
|
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')
|
|
transcode_parser.add_argument(
|
|
'src',
|
|
type=str,
|
|
help='src base music directory')
|
|
transcode_parser.add_argument(
|
|
'dest',
|
|
type=str,
|
|
help='dest music directory')
|
|
transcode_parser.add_argument(
|
|
'--transcode-preset',
|
|
type=str,
|
|
help='transcode preset',
|
|
default="opus-96k")
|
|
transcode_parser.add_argument(
|
|
'--ignore-extension',
|
|
action='store_true')
|
|
transcode_parser.add_argument(
|
|
'--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()
|
|
command_args.src = args.src
|
|
command_args.dest = args.dest
|
|
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:
|
|
def __init__(self, args: TranscodeCommandArgs):
|
|
self.args = args
|
|
|
|
def run(self):
|
|
if self.args.transcode_preset == "list":
|
|
print_transcode_presets()
|
|
exit()
|
|
|
|
print("Transcoding...")
|
|
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(
|
|
self.args.transcode_preset)
|
|
else:
|
|
with open(self.args.custom_transcoder_config_path, "r+") as file:
|
|
trans_config = TranscodeConfig()
|
|
trans_config.load_from_file(file)
|
|
|
|
output_file = file_from_path(
|
|
Path(self.args.dest), "")
|
|
|
|
if trans_config.file_extension != output_file.extension and not self.args.ignore_extension:
|
|
print(
|
|
f"{output_file.extension} is not the recommended " +
|
|
f"extension for transcode config " +
|
|
f"please change it to {trans_config.file_extension} " +
|
|
f"or run with --ignore-extension")
|
|
exit()
|
|
transcode(input_file, trans_config, self.args.dest)
|
|
|
|
if not self.args.skip_replaygain:
|
|
print("Adding ReplayGain Tags")
|
|
do_replaygain(output_file, False)
|