This commit is contained in:
Hell13Cat
2026-06-06 10:32:38 +03:00
parent 18a3e3d3f2
commit 7fe9b73d49
7 changed files with 36 additions and 93 deletions
+6 -5
View File
@@ -1,6 +1,7 @@
import subprocess
from PIL import Image
import os
import subprocess
from PIL import Image
EXTENSIONS_IMG = ('jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp')
EXTENSIONS_VID = ('mp4', 'webm')
@@ -15,7 +16,7 @@ def check_ffmpeg():
except (FileNotFoundError, subprocess.CalledProcessError):
return False
def video_gen(input_path_file, output_path_file, TARGET_MIN_SIZE):
def video_gen(input_path_file: str, output_path_file: str, TARGET_MIN_SIZE: int):
vf_scale = f"scale='if(gt(iw,ih),-1,{TARGET_MIN_SIZE})':'if(gt(iw,ih),{TARGET_MIN_SIZE},-1)'"
cmd = ["ffmpeg", "-y", "-i", input_path_file, "-ss", "00:00:00", "-vframes", "1", "-vf", vf_scale, "-q:v", "2", output_path_file]
if os.name == 'nt': creationflags = subprocess.CREATE_NO_WINDOW
@@ -24,7 +25,7 @@ def video_gen(input_path_file, output_path_file, TARGET_MIN_SIZE):
if result.returncode != 0:
raise Exception(f"FFmpeg error: {result.stderr.decode('utf-8', errors='ignore')}")
def image_and_gif_gen(input_path_file, output_path_file, TARGET_MIN_SIZE):
def image_and_gif_gen(input_path_file: str, output_path_file: str, TARGET_MIN_SIZE: int):
with Image.open(input_path_file) as img:
img_rgba = img.convert("RGBA")
background = Image.new("RGB", img_rgba.size, (255, 255, 255))
@@ -43,7 +44,7 @@ def image_and_gif_gen(input_path_file, output_path_file, TARGET_MIN_SIZE):
resized_img = final_img.resize((new_width, new_height), Image.Resampling.LANCZOS)
resized_img.save(output_path_file, "JPEG", quality=85)
def generate_thumb(input_path_file, THUMB_FOLDER, TARGET_MIN_SIZE):
def generate_thumb(input_path_file: str, THUMB_FOLDER: str, TARGET_MIN_SIZE: int):
ext_file = input_path_file.split(".")[-1]
if ext_file not in VALID_EXTENSIONS:
return {"status":False, "message":"Расширение не поддерживаеться!"}