vnrecode: pathlib for all paths
This commit is contained in:
parent
4e6fd332c5
commit
1c1e8a9292
5 changed files with 108 additions and 104 deletions
|
@ -1,6 +1,6 @@
|
|||
from ffmpeg import FFmpeg, FFmpegError
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
from os import path
|
||||
import pillow_avif
|
||||
|
||||
from .printer import Printer
|
||||
|
@ -11,7 +11,7 @@ from .utils import Utils
|
|||
class File:
|
||||
|
||||
@staticmethod
|
||||
def get_type(filename: str) -> str:
|
||||
def get_type(filename: Path) -> str:
|
||||
|
||||
extensions = {
|
||||
"audio": ['.aac', '.flac', '.m4a', '.mp3', '.ogg', '.opus', '.raw', '.wav', '.wma'],
|
||||
|
@ -21,7 +21,7 @@ class File:
|
|||
}
|
||||
|
||||
for file_type in extensions:
|
||||
if path.splitext(filename)[1] in extensions[file_type]:
|
||||
if filename.suffix in extensions[file_type]:
|
||||
return file_type
|
||||
return "unknown"
|
||||
|
||||
|
@ -43,61 +43,39 @@ class File:
|
|||
|
||||
class Compress:
|
||||
|
||||
def __init__(self, params: Params, printer: Printer, utils: Utils):
|
||||
self.__params = params
|
||||
self.__printer = printer
|
||||
self.__utils = utils
|
||||
def __init__(self, params_inst: Params, printer_inst: Printer, utils_inst: Utils):
|
||||
self.__params = params_inst
|
||||
self.__printer = printer_inst
|
||||
self.__utils = utils_inst
|
||||
|
||||
def audio(self, in_dir: str, file: str, out_dir: str, extension: str) -> str:
|
||||
def audio(self, input_path: Path, output_dir: Path, extension: str) -> Path:
|
||||
bit_rate = self.__params.audio_bitrate
|
||||
prefix = self.__utils.get_hash(file)
|
||||
out_file = path.join(out_dir, f'{prefix}_{path.splitext(file)[0]}.{extension}')
|
||||
prefix = self.__utils.get_hash(input_path.name)
|
||||
out_file = Path(output_dir, f'{prefix}_{input_path.stem}.{extension}')
|
||||
try:
|
||||
(FFmpeg()
|
||||
.input(path.join(in_dir, file))
|
||||
.input(input_path)
|
||||
.option("hide_banner")
|
||||
.output(out_file,{"b:a": bit_rate, "loglevel": "error"})
|
||||
.execute()
|
||||
)
|
||||
except FFmpegError as e:
|
||||
self.__utils.catch_unprocessed(path.join(in_dir, file), out_file, e)
|
||||
self.__printer.files(file, path.splitext(file)[0], extension, f"{bit_rate}")
|
||||
self.__utils.catch_unprocessed(input_path, out_file, e)
|
||||
self.__printer.files(input_path, out_file, f"{bit_rate}")
|
||||
return out_file
|
||||
|
||||
def video(self, in_dir: str, file: str, out_dir: str, extension: str) -> str:
|
||||
prefix = self.__utils.get_hash(file)
|
||||
out_file = path.join(out_dir, f'{prefix}_{path.splitext(file)[0]}.{extension}')
|
||||
if not self.__params.video_skip:
|
||||
codec = self.__params.video_codec
|
||||
crf = self.__params.video_crf
|
||||
|
||||
try:
|
||||
(FFmpeg()
|
||||
.input(path.join(in_dir, file))
|
||||
.option("hide_banner")
|
||||
.option("hwaccel", "auto")
|
||||
.output(out_file,{"codec:v": codec, "v:b": 0, "loglevel": "error"}, crf=crf)
|
||||
.execute()
|
||||
)
|
||||
self.__printer.files(file, path.splitext(file)[0], extension, codec)
|
||||
except FFmpegError as e:
|
||||
self.__utils.catch_unprocessed(path.join(in_dir, file), out_file, e)
|
||||
else:
|
||||
self.__utils.copy_unprocessed(path.join(in_dir, file), out_file)
|
||||
return out_file
|
||||
|
||||
def image(self, in_dir: str, file: str, out_dir: str, extension: str) -> str:
|
||||
def image(self, input_path: Path, output_dir: Path, extension: str) -> Path:
|
||||
quality = self.__params.image_quality
|
||||
prefix = self.__utils.get_hash(file)
|
||||
out_file = path.join(out_dir, f"{prefix}_{path.splitext(file)[0]}.{extension}")
|
||||
prefix = self.__utils.get_hash(input_path.name)
|
||||
out_file = Path(output_dir, f"{prefix}_{input_path.stem}.{extension}")
|
||||
try:
|
||||
image = Image.open(path.join(in_dir, file))
|
||||
image = Image.open(input_path)
|
||||
|
||||
if (extension == "jpg" or extension == "jpeg" or
|
||||
(extension == "webp" and not self.__params.webp_rgba)):
|
||||
if File.has_transparency(image):
|
||||
self.__printer.warning(f"{file} has transparency. Changing to fallback...")
|
||||
extension = self.__params.image_fall_ext
|
||||
self.__printer.warning(f"{input_path.name} has transparency. Changing to fallback...")
|
||||
out_file = Path(output_dir, f"{prefix}_{input_path.stem}.{self.__params.image_fall_ext}")
|
||||
|
||||
if File.has_transparency(image):
|
||||
image.convert('RGBA')
|
||||
|
@ -113,39 +91,61 @@ class Compress:
|
|||
lossless=self.__params.image_lossless,
|
||||
quality=quality,
|
||||
minimize_size=True)
|
||||
self.__printer.files(file, path.splitext(file)[0], extension, f"{quality}%")
|
||||
self.__printer.files(input_path, out_file, f"{quality}%")
|
||||
except Exception as e:
|
||||
self.__utils.catch_unprocessed(path.join(in_dir, file), out_file, e)
|
||||
self.__utils.catch_unprocessed(input_path, out_file, e)
|
||||
return out_file
|
||||
|
||||
def unknown(self, in_dir: str, file: str, out_dir: str) -> str:
|
||||
prefix = self.__utils.get_hash(file)
|
||||
out_file = path.join(out_dir, f"{prefix}_{file}")
|
||||
if self.__params.force_compress:
|
||||
self.__printer.unknown_file(file)
|
||||
def video(self, input_path: Path, output_dir: Path, extension: str) -> Path:
|
||||
prefix = self.__utils.get_hash(input_path.name)
|
||||
out_file = Path(output_dir, f'{prefix}_{input_path.stem}.{extension}')
|
||||
if not self.__params.video_skip:
|
||||
codec = self.__params.video_codec
|
||||
crf = self.__params.video_crf
|
||||
|
||||
try:
|
||||
(FFmpeg()
|
||||
.input(path.join(in_dir, file))
|
||||
.input(input_path)
|
||||
.option("hide_banner")
|
||||
.option("hwaccel", "auto")
|
||||
.output(out_file,{"codec:v": codec, "v:b": 0, "loglevel": "error"}, crf=crf)
|
||||
.execute()
|
||||
)
|
||||
self.__printer.files(input_path, out_file, codec)
|
||||
except FFmpegError as e:
|
||||
self.__utils.catch_unprocessed(input_path, out_file, e)
|
||||
else:
|
||||
self.__utils.copy_unprocessed(input_path, out_file)
|
||||
return out_file
|
||||
|
||||
def unknown(self, input_path: Path, output_dir: Path) -> Path:
|
||||
prefix = self.__utils.get_hash(input_path.name)
|
||||
out_file = Path(output_dir, f"{prefix}_{input_path.name}")
|
||||
if self.__params.force_compress:
|
||||
self.__printer.unknown_file(input_path.name)
|
||||
try:
|
||||
(FFmpeg()
|
||||
.input(input_path)
|
||||
.output(out_file)
|
||||
.execute()
|
||||
)
|
||||
except FFmpegError as e:
|
||||
self.__utils.catch_unprocessed(path.join(in_dir, file), out_file, e)
|
||||
self.__utils.catch_unprocessed(input_path, out_file, e)
|
||||
else:
|
||||
self.__utils.copy_unprocessed(path.join(in_dir, file), out_file)
|
||||
self.__utils.copy_unprocessed(input_path, out_file)
|
||||
return out_file
|
||||
|
||||
def compress(self, dir_: str, filename: str, output: str):
|
||||
match File.get_type(filename):
|
||||
def compress(self, source: Path, output: Path):
|
||||
match File.get_type(source):
|
||||
case "audio":
|
||||
out_file = self.audio(dir_, filename, output, self.__params.audio_ext)
|
||||
out_file = self.audio(source, output, self.__params.audio_ext)
|
||||
case "image":
|
||||
out_file = self.image(dir_, filename, output, self.__params.image_ext)
|
||||
out_file = self.image(source, output, self.__params.image_ext)
|
||||
case "video":
|
||||
out_file = self.video(dir_, filename, output, self.__params.video_ext)
|
||||
out_file = self.video(source, output, self.__params.video_ext)
|
||||
case "unknown":
|
||||
out_file = self.unknown(dir_, filename, output)
|
||||
out_file = self.unknown(source, output)
|
||||
|
||||
self.__utils.out_rename(out_file, filename)
|
||||
self.__utils.out_rename(out_file, source.name)
|
||||
self.__printer.bar.update()
|
||||
self.__printer.bar.next()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue