perf: 对歌曲信息中的图片缩小到300 #190

This commit is contained in:
涵曦 2024-09-25 12:51:53 +08:00
parent baf9a83e50
commit dec21aa57c
3 changed files with 50 additions and 13 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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()