From dec21aa57c96973155f3532a25bccd4b0590f0b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Wed, 25 Sep 2024 12:51:53 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=AF=B9=E6=AD=8C=E6=9B=B2=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E4=B8=AD=E7=9A=84=E5=9B=BE=E7=89=87=E7=BC=A9=E5=B0=8F?= =?UTF-8?q?=E5=88=B0300=20#190?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pdm.lock | 15 ++++++++++++++- pyproject.toml | 2 ++ xiaomusic/utils.py | 46 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/pdm.lock b/pdm.lock index 854380d..6bbd60a 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default", "dev", "lint"] strategy = ["inherit_metadata"] lock_version = "4.5.0" -content_hash = "sha256:d7209c5b89041122b16847a761d8f522ea404543d50b859fb283f9061d4e9f36" +content_hash = "sha256:077009450945896224a482ac528dde3c523253b2af822a9e46dae9d2f42267c2" [[metadata.targets]] requires_python = "==3.10.12" @@ -780,6 +780,19 @@ files = [ {file = "packaging-24.1.tar.gz", hash = "sha256:026ed72c8ed3fcce5bf8950572258698927fd1dbda10a5e981cdf0ac37f4f002"}, ] +[[package]] +name = "pillow" +version = "10.4.0" +requires_python = ">=3.8" +summary = "Python Imaging Library (Fork)" +groups = ["default"] +marker = "python_full_version == \"3.10.12\"" +files = [ + {file = "pillow-10.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e4d49b85c4348ea0b31ea63bc75a9f3857869174e2bf17e7aba02945cd218e6f"}, + {file = "pillow-10.4.0-cp310-cp310-manylinux_2_28_x86_64.whl", hash = "sha256:a985e028fc183bf12a77a8bbf36318db4238a3ded7fa9df1b9a133f1cb79f8fc"}, + {file = "pillow-10.4.0.tar.gz", hash = "sha256:166c1cd4d24309b30d61f79f4a9114b7b2313d7450912277855ff5dfd7cd4a06"}, +] + [[package]] name = "prompt-toolkit" version = "3.0.36" diff --git a/pyproject.toml b/pyproject.toml index fc87cf7..9a4dd58 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,7 @@ dependencies = [ "ga4mp>=2.0.4", "apscheduler>=3.10.4", "opencc-python-reimplemented==0.1.7", + "pillow>=10.4.0", ] requires-python = ">=3.10,<3.12" readme = "README.md" @@ -65,6 +66,7 @@ extend-immutable-calls = ["fastapi.Depends", "fastapi.params.Depends", "fastapi. [tool.pdm.scripts] lint = "ruff check ." fmt = "ruff format ." +lintfmt = {composite = ["ruff check --fix .", "ruff format ."]} [tool.commitizen] name = "cz_conventional_commits" diff --git a/xiaomusic/utils.py b/xiaomusic/utils.py index 39cf3c5..d186b1d 100644 --- a/xiaomusic/utils.py +++ b/xiaomusic/utils.py @@ -6,6 +6,7 @@ import base64 import copy import difflib import hashlib +import io import json import logging import mimetypes @@ -32,6 +33,7 @@ from mutagen.oggvorbis import OggVorbis from mutagen.wave import WAVE from mutagen.wavpack import WavPack from opencc import OpenCC +from PIL import Image from requests.utils import cookiejar_from_dict from xiaomusic.const import SUPPORT_MUSIC_TYPE @@ -449,7 +451,7 @@ def convert_file_to_mp3(input_file: str, ffmpeg_location: str, music_path: str) try: subprocess.run(command, check=True) except subprocess.CalledProcessError as e: - print(f"Error during conversion: {e}") + logging.exception(f"Error during conversion: {e}") return None relative_path = os.path.relpath(out_file_path, music_path) @@ -557,23 +559,43 @@ def _save_picture(picture_data, save_root, file_path): dir_path = os.path.join(save_root, file_hash[-6:]) os.makedirs(dir_path, exist_ok=True) - # 检测图片格式 - if picture_data[:3] == b"\xff\xd8\xff": - ext = "jpg" - elif picture_data[:8] == b"\x89PNG\r\n\x1a\n": - ext = "png" - else: - ext = "bin" # 未知格式 - # 保存图片 filename = os.path.basename(file_path) (name, _) = os.path.splitext(filename) - picture_path = os.path.join(dir_path, f"{name}.{ext}") - with open(picture_path, "wb") as img: - img.write(picture_data) + picture_path = os.path.join(dir_path, f"{name}.jpg") + + try: + _resize_save_image(picture_data, picture_path) + except Exception as e: + logging.exception(f"Error _resize_save_image: {e}") return picture_path +def _resize_save_image(image_bytes, save_path, max_size=300): + # 将 bytes 转换为 PIL Image 对象 + image = Image.open(io.BytesIO(image_bytes)) + image = image.convert("RGB") + + # 获取原始尺寸 + original_width, original_height = image.size + + # 如果图片的宽度和高度都小于 max_size,则直接保存原始图片 + if original_width <= max_size and original_height <= max_size: + image.save(save_path, format="JPEG") + return + + # 计算缩放比例,保持等比缩放 + scaling_factor = min(max_size / original_width, max_size / original_height) + + # 计算新的尺寸 + new_width = int(original_width * scaling_factor) + new_height = int(original_height * scaling_factor) + + resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS) + resized_image.save(save_path, format="JPEG") + return save_path + + def extract_audio_metadata(file_path, save_root): audio = mutagen.File(file_path) metadata = Metadata()