49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
from ..utils.transcoder import get_transcode_config, transcode, TranscodeConfig
|
|
from ..utils.scan_for_music import file_from_path
|
|
from ..transcode_levels import transcode_levels
|
|
|
|
from pathlib import Path
|
|
from json import load as load_json_file
|
|
|
|
class TranscodeCommand:
|
|
def __init__(
|
|
self,
|
|
src: str,
|
|
dest: str,
|
|
transcode_level: str,
|
|
ignore_extension: bool,
|
|
custom_encoder_config_path: str,
|
|
):
|
|
self.src = src
|
|
self.dest = dest
|
|
self.transcode_level = transcode_level
|
|
self.ignore_extension = ignore_extension
|
|
self.custom_encoder_config_path = custom_encoder_config_path
|
|
|
|
def run(self):
|
|
if self.transcode_level == "list":
|
|
print("Transcode Levels:", ", ".join(transcode_levels))
|
|
exit()
|
|
|
|
print("Transcoding...")
|
|
input_file = file_from_path(Path(self.src), "")
|
|
|
|
if self.custom_encoder_config_path is None or len(self.custom_encoder_config_path) == 0:
|
|
trans_config = get_transcode_config(input_file, self.transcode_level)
|
|
else:
|
|
with open(self.custom_encoder_config_path, "r+") as f:
|
|
trans_config_dict = load_json_file(f)
|
|
trans_config = TranscodeConfig()
|
|
trans_config.load_from_dict(trans_config_dict)
|
|
|
|
output_file = file_from_path(Path(self.dest), "")
|
|
|
|
if trans_config.file_extension != output_file.extension and not self.ignore_extension:
|
|
print(
|
|
f"{output_file.extension} is not the recommended "+
|
|
f"extension for transcode_level {self.transcode_level} "+
|
|
f"please change it to {trans_config.file_extension} "+
|
|
f"or run with --ignore-extension"
|
|
)
|
|
exit()
|
|
transcode(input_file, trans_config, self.dest) |