nixfiles/musicutil/commands/transcode_command.py

93 lines
3 KiB
Python
Raw Normal View History

2022-02-06 15:51:42 +00:00
from musicutil.utils.do_replaygain import do_replaygain
from ..utils.transcoder import get_transcode_config, transcode, TranscodeConfig
2022-02-04 16:19:11 +00:00
from ..utils.scan_for_music import file_from_path
2022-02-06 15:20:12 +00:00
from ..transcode_presets import print_transcode_presets, transcode_presets
2022-02-04 16:19:11 +00:00
from pathlib import Path
from json import load as load_json_file
2022-02-04 16:19:11 +00:00
2022-02-06 16:18:14 +00:00
2022-02-06 15:20:12 +00:00
class TranscodeCommandArgs:
src: str
dest: str
transcode_preset: str
ignore_extension: bool
custom_transcoder_config_path: str
2022-02-06 15:51:42 +00:00
skip_replaygain: bool
2022-02-06 15:20:12 +00:00
2022-02-06 16:18:14 +00:00
2022-02-06 15:20:12 +00:00
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,
2022-02-06 16:18:14 +00:00
help='custom transcoder config')
2022-02-06 15:51:42 +00:00
transcode_parser.add_argument(
'--skip-replaygain',
action='store_true')
2022-02-06 15:20:12 +00:00
2022-02-06 16:18:14 +00:00
2022-02-06 15:20:12 +00:00
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
2022-02-06 15:51:42 +00:00
command_args.skip_replaygain = args.skip_replaygain
2022-02-06 15:20:12 +00:00
return command_args
2022-02-06 16:18:14 +00:00
2022-02-04 16:19:11 +00:00
class TranscodeCommand:
2022-02-06 15:20:12 +00:00
def __init__(self, args: TranscodeCommandArgs):
self.args = args
2022-02-04 16:19:11 +00:00
def run(self):
2022-02-06 15:20:12 +00:00
if self.args.transcode_preset == "list":
print_transcode_presets()
2022-02-04 16:19:11 +00:00
exit()
print("Transcoding...")
2022-02-06 15:51:42 +00:00
input_file = file_from_path(Path(self.args.src), "")
2022-02-06 16:18:14 +00:00
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:
2022-02-06 16:18:14 +00:00
with open(self.args.custom_transcoder_config_path, "r+") as file:
trans_config = TranscodeConfig()
2022-02-06 15:20:12 +00:00
trans_config.load_from_file(file)
2022-02-04 16:19:11 +00:00
2022-02-06 16:18:14 +00:00
output_file = file_from_path(
Path(self.args.dest), "")
2022-02-04 16:19:11 +00:00
2022-02-06 15:20:12 +00:00
if trans_config.file_extension != output_file.extension and not self.args.ignore_extension:
2022-02-04 16:19:11 +00:00
print(
2022-02-06 16:18:14 +00:00
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")
2022-02-04 16:19:11 +00:00
exit()
2022-02-06 15:51:42 +00:00
transcode(input_file, trans_config, self.args.dest)
if not self.args.skip_replaygain:
print("Adding ReplayGain Tags")
2022-02-06 16:18:14 +00:00
do_replaygain(output_file, False)