run formatter

This commit is contained in:
ChaotiCryptidz 2022-02-06 16:18:14 +00:00
parent 468dcf2f00
commit 791213481f
11 changed files with 108 additions and 46 deletions

View file

@ -15,6 +15,7 @@ class CopyCommandState:
files: list[File] = []
transcoded_files: list[File] = []
class CopyCommandArgs():
src: str
dest: str
@ -24,6 +25,7 @@ class CopyCommandArgs():
skip_existing: bool
skip_replaygain: bool
def add_copy_command(subparsers):
copy_parser = subparsers.add_parser('copy')
copy_parser.add_argument(
@ -53,6 +55,7 @@ def add_copy_command(subparsers):
'--single-directory',
action='store_true')
def get_copy_args(args) -> CopyCommandArgs:
print(args)
command_args = CopyCommandArgs()
@ -65,6 +68,7 @@ def get_copy_args(args) -> CopyCommandArgs:
command_args.skip_replaygain = args.skip_replaygain
return command_args
class CopyCommand():
def __init__(self, args: CopyCommandArgs):
self.args = args
@ -137,7 +141,10 @@ class CopyCommand():
self.state.transcoded_files.append(file)
def _transcode_with_config(self, file: File, trans_config: TranscodeConfig):
def _transcode_with_config(
self,
file: File,
trans_config: TranscodeConfig):
src = file.join_path_to()
new_file = deep_copy(file)
@ -147,7 +154,8 @@ class CopyCommand():
) if self.args.single_directory else new_file.join_path_from_src()
dest_filepath = self.args.dest + "/" + dest_filepath
if (self.args.skip_existing and path_exists(dest_filepath)):
if (self.args.skip_existing and path_exists(
dest_filepath)):
print("Skipping transcoding", dest_filepath)
self.state.transcoded_files.append(new_file)
return
@ -167,7 +175,8 @@ class CopyCommand():
directories.add(file.path_from_src)
for dir in directories:
make_directories(
self.args.dest + "/" + dir, exist_ok=True)
self.args.dest + "/" + dir,
exist_ok=True)
is_transcode_config_set = self.args.custom_transcoder_config is not None
@ -183,7 +192,8 @@ class CopyCommand():
trans_config = TranscodeConfig()
trans_config.load_from_file(f)
else:
trans_config = get_transcode_config(self.args.transcode_preset)
trans_config = get_transcode_config(
self.args.transcode_preset)
print(trans_config)
@ -201,6 +211,7 @@ class CopyCommand():
print("Adding ReplayGain Tags")
for file in self.state.files:
print(f"Adding ReplayGain Tags to \"{file.filename}.{file.extension}\"")
print(
f"Adding ReplayGain Tags to \"{file.filename}.{file.extension}\"")
do_replaygain(file)

View file

@ -11,11 +11,13 @@ from os import rename as rename_file
class ProcessCommandState:
files: list[File] = []
class ProcessCommandArgs:
src: str
dry_run: bool
skip_replaygain: bool
def add_process_command(subparsers):
process_parser = subparsers.add_parser('process')
process_parser.add_argument(
@ -28,6 +30,7 @@ def add_process_command(subparsers):
'--skip-replaygain',
action='store_true')
def get_process_args(args) -> ProcessCommandArgs:
command_args = ProcessCommandArgs()
command_args.src = args.src
@ -35,6 +38,7 @@ def get_process_args(args) -> ProcessCommandArgs:
command_args.skip_replaygain = args.skip_replaygain
return command_args
class ProcessCommand():
def __init__(self, args: ProcessCommandArgs):
self.args = args
@ -45,7 +49,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):
if (not self.args.skip_replaygain) and (
not self.args.dry_run):
self.add_replaygain_tags()
def scan_for_music(self):
@ -96,6 +101,7 @@ class ProcessCommand():
print("Adding ReplayGain Tags")
for file in self.state.files:
print(f"Adding ReplayGain Tags to \"{file.filename}.{file.extension}\"")
print(
f"Adding ReplayGain Tags to \"{file.filename}.{file.extension}\"")
do_replaygain(file)

View file

@ -6,6 +6,7 @@ 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
@ -14,6 +15,7 @@ class TranscodeCommandArgs:
custom_transcoder_config_path: str
skip_replaygain: bool
def add_transcode_command(subparsers):
transcode_parser = subparsers.add_parser('transcode')
transcode_parser.add_argument(
@ -40,6 +42,7 @@ def add_transcode_command(subparsers):
'--skip-replaygain',
action='store_true')
def get_transcode_args(args) -> TranscodeCommandArgs:
command_args = TranscodeCommandArgs()
command_args.src = args.src
@ -50,6 +53,7 @@ def get_transcode_args(args) -> TranscodeCommandArgs:
command_args.skip_replaygain = args.skip_replaygain
return command_args
class TranscodeCommand:
def __init__(self, args: TranscodeCommandArgs):
self.args = args
@ -62,22 +66,24 @@ class TranscodeCommand:
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)
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), "")
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"
)
f"or run with --ignore-extension")
exit()
transcode(input_file, trans_config, self.args.dest)

View file

@ -1,9 +1,13 @@
from functools import reduce
def add_to_arr(arr: list[str], items: list[str]) -> list[str]:
def add_to_arr(
arr: list[str],
items: list[str]) -> list[str]:
for item in items:
arr.append(item)
# does not include copy
transcode_presets = {}
@ -14,7 +18,23 @@ add_to_arr(transcode_presets["mp3"], [
f"mp3-v{quality}" for quality in range(0, 10)
])
# mp3 bitrates
mp3_bitrates = [8, 16, 24, 32, 40, 48, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320]
mp3_bitrates = [
8,
16,
24,
32,
40,
48,
64,
80,
96,
112,
128,
160,
192,
224,
256,
320]
add_to_arr(transcode_presets["mp3"], [
f"mp3-{bitrate}k" for bitrate in mp3_bitrates
])
@ -74,10 +94,13 @@ transcode_presets["vorbis"].sort()
transcode_presets["speex"].sort()
transcode_presets["g726"].sort()
def print_transcode_presets():
for category in transcode_presets.keys():
print(f"Category {category}:")
for preset in transcode_presets[category]:
print(f"- {preset}")
transcode_presets_list = reduce(lambda a, b: a+b, transcode_presets.values())
transcode_presets_list = reduce(
lambda a, b: a + b, transcode_presets.values())

View file

@ -3,7 +3,10 @@ from ..meta import r128gain_path, ffmpeg_path
from subprocess import run as run_command
def do_replaygain(file: File, skip_if_existing: bool = True) -> None:
def do_replaygain(
file: File,
skip_if_existing: bool = True) -> None:
command_args = [
r128gain_path,
"-f", ffmpeg_path,

View file

@ -7,6 +7,7 @@ from json import loads as load_json_string
from mutagen.mp3 import EasyMP3 as MP3
from mutagen.flac import FLAC
def load_tag_information_mutagen(file: File) -> Tags:
path = file.join_path_to()
tags = Tags()
@ -23,7 +24,6 @@ def load_tag_information_mutagen(file: File) -> Tags:
return tags
def load_tag_information_ffmpeg(file: File) -> Tags:
path = file.join_path_to()
tags = Tags()
@ -36,7 +36,8 @@ def load_tag_information_ffmpeg(file: File) -> Tags:
path
]
ffprobe_output = run_command(command_args, capture_output=True).stdout
ffprobe_output = run_command(
command_args, capture_output=True).stdout
data = load_json_string(ffprobe_output)
file_tags = data["format"]["tags"]
@ -65,6 +66,7 @@ def load_tag_information_ffmpeg(file: File) -> Tags:
tags.artist = artist
return tags
def load_tag_information(file: File) -> Tags:
try:
tags = load_tag_information_mutagen(file)
@ -72,6 +74,7 @@ def load_tag_information(file: File) -> Tags:
except Exception as _:
tags = load_tag_information_ffmpeg(file)
return tags
except:
print(f"Could not get tags for file {file.filename}. Exiting.")
except BaseException:
print(
f"Could not get tags for file {file.filename}. Exiting.")
exit()

View file

@ -4,6 +4,7 @@ from os.path import relpath
from ..types import File
from ..meta import supported_formats
def file_from_path(path: Path, src: str) -> File:
file = File()
file.path_to = str(path.parent)
@ -13,6 +14,7 @@ def file_from_path(path: Path, src: str) -> File:
file.extension = path.suffix.replace(".", "")
return file
def scan_for_music(src: str) -> list[File]:
files: list[File] = []
for format in supported_formats:

View file

@ -1,6 +1,7 @@
from ..meta import sub_char, substitutions
from fold_to_ascii import fold
def reduce_to_ascii_and_substitute(filename: str):
filename = filename.replace("/", sub_char)
filename = filename.replace("\\", sub_char)

View file

@ -6,6 +6,7 @@ from yaml import load as load_yaml_file
from yaml import Loader as YamlLoader
from subprocess import run as run_command
class TranscodeConfig:
use_quality = False
use_bitrate = False
@ -18,7 +19,9 @@ class TranscodeConfig:
channels = ""
def load_from_file(self, file):
self.load_from_dict(load_yaml_file(file, Loader=YamlLoader))
self.load_from_dict(
load_yaml_file(
file, Loader=YamlLoader))
def load_from_dict(self, data):
if "use_quality" in data:
@ -41,6 +44,7 @@ class TranscodeConfig:
self.channels = data["channels"]
return self
def get_transcode_config(preset: str):
conf = TranscodeConfig()
if preset in preset_transcode_presets.keys():
@ -107,7 +111,11 @@ def get_transcode_config(preset: str):
print("Unknown Level")
exit()
def transcode(file: File, config: TranscodeConfig, dest: str):
def transcode(
file: File,
config: TranscodeConfig,
dest: str):
ffmpeg_command = [
ffmpeg_path,
"-y",
@ -132,7 +140,6 @@ def transcode(file: File, config: TranscodeConfig, dest: str):
ffmpeg_command.append("-ac")
ffmpeg_command.append(config.channels)
if config.use_quality:
ffmpeg_command.append("-q:a")
ffmpeg_command.append(config.quality)