diff --git a/docs/naming.md b/docs/naming.md index 8db6b19..8beeb6b 100644 --- a/docs/naming.md +++ b/docs/naming.md @@ -65,10 +65,44 @@ w≥1000 或 h≥600 → 720p 其余 → 480p / 360p - 编码写法固定:`TrueHD`(不写成 TRUEHD)、`DTS-HD`、`EAC3`、`AC3`、`AAC`、`FLAC`、`Opus` - 单音轨时**不加** `1Audio` +### 编码 + +``` +HEVC / H264 / AV1 / VP9 / MPEG2 / MPEG4 / VC1 / ProRes … +``` + +未知编码直接用 ffprobe 的原名大写,**不再默认成 H264**。取视频流时会跳过内嵌封面图 +(`disposition.attached_pic=1` 的 jpeg/png 流),否则会把封面当成主画面。 + +### 位深 + +``` +8bit / 10bit / 12bit +``` + +优先读 `bits_per_raw_sample`,退回解析 `pix_fmt`(`yuv420p10le` → 10bit)。 + ### 来源 -- 码率超过阈值 → `BluRay.REMUX` -- 否则 `WEB-DL` +**只认文件名里的 token**,其次才回退到码率判断: + +| 名称里出现 | 标签 | +|---|---| +| `REMUX` | `BluRay.REMUX` | +| `BluRay` / `BDRip` | `BluRay` | +| `WEBRip` | `WEBRip` | +| `WEB-DL` | `WEB-DL` | +| `HDTV` | `HDTV` | +| `DVDRip` | `DVDRip` | +| 都没有 | 码率超阈值 → `BluRay.REMUX`,否则 `WEB-DL` | + +> 实测:用流参数猜来源与文件名的一致率只有 **33%**(WEB-DL 与 WEBRip 的流特征几乎相同), +> 所以不靠猜。改名时 token 会保留在新名字里,下次整理结果不变(幂等)。 + +### 标题截断 + +按 **UTF-8 字节**截断(`MAX_MEDIA_TITLE_BYTES = 120`),不是按字符。 +CJK 一个字 3 字节,只按字符数截会撞上 255 字节的文件名上限。 ## 文件夹 diff --git a/media/src/core/utils.py b/media/src/core/utils.py index 8ed4993..e4e971a 100644 --- a/media/src/core/utils.py +++ b/media/src/core/utils.py @@ -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: diff --git a/media/src/services/mediainfo.py b/media/src/services/mediainfo.py index 27cb36b..f927a34 100644 --- a/media/src/services/mediainfo.py +++ b/media/src/services/mediainfo.py @@ -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`。 diff --git a/media/src/services/parser.py b/media/src/services/parser.py index f654632..87452f9 100644 --- a/media/src/services/parser.py +++ b/media/src/services/parser.py @@ -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('.', ' ')