fix(naming): 修编码/位深/封面图/字节截断,来源改为读文件名
- 视频流选择跳过 attached_pic:内嵌封面(jpeg/png)不再被当成主画面, 否则编码与分辨率都会读错 - 编码不再「非 HEVC 即 H264」:补全 HEVC/H264/AV1/VP9/MPEG2/MPEG4/VC1/ProRes, 未知编码用 ffprobe 原名(实测 mpeg4 文件此前被标成 H264) - 位深区分 8bit/10bit/12bit(此前 12bit 会被标成 10bit) - 来源改为「读文件名 token」优先:REMUX/BluRay/BDRip/WEBRip/WEB-DL/HDTV/DVDRip, 没有 token 才回退码率判断。实测流参数猜来源与文件名一致率仅 33%, WEB-DL 与 WEBRip 无法从流特征区分 - media_title 改为按 UTF-8 字节截断(120 字节),避免 CJK 长标题撞 255 字节上限 - docs/naming.md 补充编码/位深/来源/截断规则
This commit is contained in:
+17
-2
@@ -12,7 +12,8 @@ class StringUtil:
|
||||
|
||||
# media_title 允许的最大字符数(留足技术标签,避免触碰文件系统 255 字节上限)
|
||||
MAX_MEDIA_TITLE = 120
|
||||
|
||||
# 字节上限:文件名总长受 255 字节限制,CJK 一个字 3 字节,不能只按字符截
|
||||
MAX_MEDIA_TITLE_BYTES = 120
|
||||
# 前后带空格的破折号(- – — −)整体视为分隔符 → 点
|
||||
# 例:Mission: Impossible - The Final Reckoning → Mission.Impossible.The.Final.Reckoning
|
||||
_SPACED_DASH = re.compile(r"\s+[-–—−]\s+")
|
||||
@@ -43,6 +44,19 @@ class StringUtil:
|
||||
s = re.sub(r"\.{2,}", ".", s)
|
||||
return s.strip(". -")
|
||||
|
||||
@staticmethod
|
||||
def truncate_bytes(text: str, limit: int) -> str:
|
||||
"""按 UTF-8 字节截断(不切坏多字节字符),末尾多余的点去掉。
|
||||
|
||||
文件系统上限是 255 字节,CJK 一个字占 3 字节,只按字符数截会超限。
|
||||
"""
|
||||
if not text:
|
||||
return ""
|
||||
raw = text.encode("utf-8")
|
||||
if len(raw) <= limit:
|
||||
return text
|
||||
return raw[:limit].decode("utf-8", errors="ignore").rstrip(". ")
|
||||
|
||||
@staticmethod
|
||||
def media_title(title: str, fallback: str = "") -> str:
|
||||
"""生成可直接用作文件名的标题:白名单清洗 + 超长截断。
|
||||
@@ -52,7 +66,8 @@ class StringUtil:
|
||||
for candidate in (title, fallback):
|
||||
s = StringUtil.sanitize_title(candidate)
|
||||
if s:
|
||||
return s[:StringUtil.MAX_MEDIA_TITLE].strip(".")
|
||||
s = s[:StringUtil.MAX_MEDIA_TITLE]
|
||||
return StringUtil.truncate_bytes(s, StringUtil.MAX_MEDIA_TITLE_BYTES).strip('.')
|
||||
return ""
|
||||
|
||||
class FFprobeUtil:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
# src/services/mediainfo.py
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
from core import config
|
||||
from core.utils import FFprobeUtil
|
||||
@@ -62,7 +63,7 @@ class MediaInfoService:
|
||||
size_bytes = f_path.stat().st_size
|
||||
mbps = (size_bytes * 8) / duration / 1_000_000
|
||||
|
||||
v_stream = next((s for s in data.get('streams', []) if s['codec_type'] == 'video'), {})
|
||||
v_stream = MediaInfoService._main_video_stream(data.get('streams', []))
|
||||
|
||||
w = int(v_stream.get('width') or 0)
|
||||
h = int(v_stream.get('height') or 1080)
|
||||
@@ -73,12 +74,8 @@ class MediaInfoService:
|
||||
if mbps > remux_threshold:
|
||||
tags['is_remux'] = True
|
||||
|
||||
codec_name = v_stream.get('codec_name', '')
|
||||
if '265' in codec_name or 'hevc' in codec_name:
|
||||
tags['v_codec'] = "HEVC"
|
||||
|
||||
if '10' in v_stream.get('pix_fmt', '') or '12' in v_stream.get('pix_fmt', ''):
|
||||
tags['bits'] = "10bit"
|
||||
tags['v_codec'] = MediaInfoService.video_codec_name(v_stream.get('codec_name', ''))
|
||||
tags['bits'] = MediaInfoService.bit_depth(v_stream)
|
||||
|
||||
tags['a_info'] = MediaInfoService.describe_audio(data.get('streams', []))
|
||||
|
||||
@@ -124,6 +121,73 @@ class MediaInfoService:
|
||||
return "Vorbis"
|
||||
return raw.upper() or "AAC"
|
||||
|
||||
# 视频编码 → 标签写法(未知编码直接大写原名,绝不默认成 H264)
|
||||
VIDEO_CODEC_NAMES = {
|
||||
"hevc": "HEVC", "h265": "HEVC",
|
||||
"h264": "H264", "avc": "H264",
|
||||
"av1": "AV1", "vp9": "VP9", "vp8": "VP8",
|
||||
"mpeg2video": "MPEG2", "mpeg1video": "MPEG1", "mpeg4": "MPEG4",
|
||||
"vc1": "VC1", "prores": "ProRes", "theora": "Theora",
|
||||
}
|
||||
|
||||
# 来源:发布名里的 token 最可靠,流参数分不出 WEB-DL 与 WEBRip
|
||||
SOURCE_PATTERNS = [
|
||||
(r"remux", "BluRay.REMUX"),
|
||||
(r"blu-?ray|bd-?rip|bdrip", "BluRay"),
|
||||
(r"web-?rip", "WEBRip"),
|
||||
(r"web-?dl", "WEB-DL"),
|
||||
(r"hdtv", "HDTV"),
|
||||
(r"dvdrip|dvd-?rip", "DVDRip"),
|
||||
(r"hdrip", "HDRip"),
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def _main_video_stream(streams: list) -> dict:
|
||||
"""取真正的视频流:跳过内嵌封面图(attached_pic)。"""
|
||||
for stream in streams:
|
||||
if stream.get("codec_type") != "video":
|
||||
continue
|
||||
if (stream.get("disposition") or {}).get("attached_pic"):
|
||||
continue
|
||||
return stream
|
||||
return {}
|
||||
|
||||
@staticmethod
|
||||
def video_codec_name(codec_name: str) -> str:
|
||||
raw = (codec_name or "").lower()
|
||||
return MediaInfoService.VIDEO_CODEC_NAMES.get(raw, raw.upper() or "H264")
|
||||
|
||||
@staticmethod
|
||||
def bit_depth(stream: dict) -> str:
|
||||
"""8bit / 10bit / 12bit。优先读 bits_per_raw_sample,退回解析 pix_fmt。"""
|
||||
bits = 0
|
||||
try:
|
||||
bits = int(stream.get("bits_per_raw_sample") or 0)
|
||||
except (TypeError, ValueError):
|
||||
bits = 0
|
||||
if not bits:
|
||||
match = re.search(r"p(\d{2})(?:le|be)?$", (stream.get("pix_fmt") or ""))
|
||||
if match:
|
||||
bits = int(match.group(1))
|
||||
if bits >= 12:
|
||||
return "12bit"
|
||||
if bits >= 10:
|
||||
return "10bit"
|
||||
return "8bit"
|
||||
|
||||
@staticmethod
|
||||
def detect_source(name: str, tags: dict) -> str:
|
||||
"""来源标签:文件名里有 token 就用它,没有才回退到码率判断。
|
||||
|
||||
实测流参数无法区分 WEB-DL 与 WEBRip(一致率仅 33%),
|
||||
所以老文件改成 WEB-DL 后再回头猜只会更糟——只认名字。
|
||||
"""
|
||||
low = (name or "").lower()
|
||||
for pattern, label in MediaInfoService.SOURCE_PATTERNS:
|
||||
if re.search(pattern, low):
|
||||
return label
|
||||
return "BluRay.REMUX" if tags.get("is_remux") else "WEB-DL"
|
||||
|
||||
@staticmethod
|
||||
def describe_audio(streams: list) -> str:
|
||||
"""把音轨信息压成一个标签:`TrueHD.5.1` / `DTS.5.1.2Audio`。
|
||||
|
||||
@@ -188,7 +188,7 @@ class ParserService:
|
||||
|
||||
tid = f"[tmdbid-{tmdb['tmdb_id']}]" if (tmdb and config.INCLUDE_TMDB_ID) else ""
|
||||
|
||||
source = "BluRay.REMUX" if tags['is_remux'] else "WEB-DL"
|
||||
source = MediaInfoService.detect_source(f_path.name, tags)
|
||||
tech = f"{tags['res']}.{source}.{tags['v_codec']}.{tags['bits']}.{tags['a_info']}"
|
||||
|
||||
folder_title = title.replace('.', ' ')
|
||||
|
||||
Reference in New Issue
Block a user