diff --git a/media/src/services/preflight.py b/media/src/services/preflight.py index 2165a06..cb2d8db 100644 --- a/media/src/services/preflight.py +++ b/media/src/services/preflight.py @@ -304,32 +304,104 @@ class PreflightService: # ------------------------------------------------------------------ 报告 + HEADERS = ["path", "verdict", "codec", "height", "mbps", "dv_profile", + "audio_codecs", "sub_codecs", "actions", "reasons"] + + @classmethod + def _report_rows(cls, results: list) -> list: + order = {BLOCKED: 0, PREP: 1, READY: 2} + rows = [] + for path, r in sorted(results, key=lambda x: (order.get(x[1].verdict, 9), str(x[0]))): + i = r.info + audio = "/".join(f"{a['codec']}{a['channels']}" for a in i.get("audio", [])) + subs = "/".join(sorted({s["codec"] for s in i.get("subs", [])})) + rows.append((path, r, [ + str(path), r.verdict, i.get("codec", ""), i.get("height", ""), + i.get("mbps", ""), i.get("dv_profile") or "", + audio, subs, "/".join(r.actions), " | ".join(r.reasons), + ])) + return rows + @classmethod def write_report(cls, root: Path, results: list, only_issues: bool = False) -> Path | None: - """results: [(path, PreflightResult)],输出 root/preflight_report.csv""" + """results: [(path, PreflightResult)],输出 root/preflight_report.xlsx(无 openpyxl 时退回 csv)。""" if not config.PREFLIGHT_REPORT or not results: return None if only_issues: results = [(p, r) for p, r in results if r.verdict != READY] if not results: return None - report = root / "preflight_report.csv" - header = "path,verdict,codec,height,mbps,dv_profile,audio_codecs,sub_codecs,actions,reasons\n" - lines = [header] - for path, r in results: - i = r.info - audio = "/".join(f"{a['codec']}{a['channels']}" for a in i.get("audio", [])) - subs = "/".join(sorted({s["codec"] for s in i.get("subs", [])})) - row = [ - str(path), r.verdict, i.get("codec", ""), i.get("height", ""), - i.get("mbps", ""), i.get("dv_profile") or "", - audio, subs, "/".join(r.actions), " | ".join(r.reasons), - ] - lines.append(",".join('"' + str(c).replace('"', '""') + '"' for c in row) + "\n") - report.write_text("".join(lines), "utf-8") + + rows = cls._report_rows(results) + + try: + from openpyxl import Workbook + from openpyxl.styles import Alignment, Font, PatternFill + from openpyxl.utils import get_column_letter + except ImportError: + log.warning("⚠️ 未安装 openpyxl,报告退回 CSV(pip install openpyxl 可启用 xlsx)") + return cls._write_csv(root, rows) + + wb = Workbook() + ws = wb.active + ws.title = "preflight" + ws.append(cls.HEADERS) + + header_fill = PatternFill("solid", fgColor="305496") + for cell in ws[1]: + cell.font = Font(bold=True, color="FFFFFF") + cell.fill = header_fill + cell.alignment = Alignment(vertical="center") + + fills = { + BLOCKED: PatternFill("solid", fgColor="F8CBAD"), + PREP: PatternFill("solid", fgColor="FFE699"), + } + for path, r, values in rows: + ws.append(values) + row_idx = ws.max_row + if r.verdict in fills: + for cell in ws[row_idx]: + cell.fill = fills[r.verdict] + try: + link = ws.cell(row=row_idx, column=1) + link.hyperlink = path.as_uri() + link.style = "Hyperlink" + except (ValueError, OSError): + pass + + last_col = get_column_letter(len(cls.HEADERS)) + ws.auto_filter.ref = f"A1:{last_col}{ws.max_row}" + # verdict 列(0-based colId=1)默认只勾选 PREP/BLOCKED,并把 READY 行隐藏; + # 若整份报告全是 READY,就保持全部可见,避免打开是空表 + has_issues = any(r.verdict != READY for _, r, _ in rows) + if has_issues: + ws.auto_filter.add_filter_column(1, [PREP, BLOCKED]) + for offset, (_, r, _) in enumerate(rows): + if r.verdict == READY: + ws.row_dimensions[offset + 2].hidden = True + ws.freeze_panes = "A2" + + widths = {"A": 80, "B": 10, "C": 11, "D": 8, "E": 9, "F": 10, + "G": 26, "H": 32, "I": 22, "J": 60} + for col, width in widths.items(): + ws.column_dimensions[col].width = width + + report = root / "preflight_report.xlsx" + wb.save(report) log.info(f"📄 兼容性报告: {report}") return report + @classmethod + def _write_csv(cls, root: Path, rows: list) -> Path: + report = root / "preflight_report.csv" + lines = [",".join(cls.HEADERS) + "\n"] + for _, _, values in rows: + lines.append(",".join('"' + str(v).replace('"', '""') + '"' for v in values) + "\n") + report.write_text("".join(lines), "utf-8") + log.info(f"📄 兼容性报告(CSV): {report}") + return report + @classmethod def scan(cls, root: Path) -> list: """check 模式:只体检不动文件。""" diff --git a/requirements.txt b/requirements.txt index 5128c4d..5201112 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pysubs2 requests -watchdog \ No newline at end of file +watchdog +openpyxl