100 lines
3.3 KiB
Python
100 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
按天统计 数据.jsonl:每天处理了多少条视频、处理的视频总时长是多少。
|
||
日期取 process_start(没有则回退 timestamp)的年月日。
|
||
|
||
用法(在项目根目录运行):
|
||
python temp/daily_stats.py [数据.jsonl路径]
|
||
默认: python temp/daily_stats.py 数据.jsonl
|
||
"""
|
||
import json
|
||
import sys
|
||
from collections import defaultdict
|
||
|
||
JSONL_PATH = sys.argv[1] if len(sys.argv) > 1 else "数据.jsonl"
|
||
|
||
|
||
def main():
|
||
try:
|
||
f = open(JSONL_PATH, encoding="utf-8")
|
||
except FileNotFoundError:
|
||
print(f"找不到文件: {JSONL_PATH}")
|
||
return
|
||
|
||
# 每天: count/success/no_subtitle/error 计数;dur=全部视频时长;dur_ok=成功视频时长
|
||
days = defaultdict(lambda: {"count": 0, "success": 0, "no_subtitle": 0,
|
||
"error": 0, "dur": 0.0, "dur_ok": 0.0})
|
||
no_date = 0
|
||
|
||
with f:
|
||
for line in f:
|
||
line = line.strip()
|
||
if not line:
|
||
continue
|
||
try:
|
||
rec = json.loads(line)
|
||
except json.JSONDecodeError:
|
||
continue
|
||
|
||
ts = rec.get("process_start") or rec.get("timestamp") or ""
|
||
day = ts[:10] if len(ts) >= 10 else None # YYYY-MM-DD
|
||
if not day:
|
||
no_date += 1
|
||
continue
|
||
|
||
d = days[day]
|
||
d["count"] += 1
|
||
|
||
status = rec.get("status")
|
||
if status == "success":
|
||
d["success"] += 1
|
||
elif status == "no_subtitle":
|
||
d["no_subtitle"] += 1
|
||
else:
|
||
d["error"] += 1
|
||
|
||
dur = rec.get("duration") or 0
|
||
try:
|
||
dur = float(dur)
|
||
d["dur"] += dur
|
||
if status == "success":
|
||
d["dur_ok"] += dur # 只累计成功出字幕的视频时长
|
||
except (TypeError, ValueError):
|
||
pass
|
||
|
||
if not days:
|
||
print("没有可统计的数据")
|
||
return
|
||
|
||
print(f"文件: {JSONL_PATH}")
|
||
print("=" * 84)
|
||
print(f"{'日期':<12}{'视频数':>7}{'成功':>7}{'无字幕':>7}{'错误':>7}"
|
||
f"{'总时长(h)':>12}{'成功时长(h)':>14}")
|
||
print("-" * 84)
|
||
|
||
tot = {"count": 0, "success": 0, "no_subtitle": 0, "error": 0, "dur": 0.0, "dur_ok": 0.0}
|
||
for day in sorted(days):
|
||
d = days[day]
|
||
print(f"{day:<12}{d['count']:>7}{d['success']:>7}{d['no_subtitle']:>7}"
|
||
f"{d['error']:>7}{d['dur']/3600:>12.1f}{d['dur_ok']/3600:>14.1f}")
|
||
for k in tot:
|
||
tot[k] += d[k]
|
||
|
||
print("-" * 84)
|
||
print(f"{'合计':<12}{tot['count']:>7}{tot['success']:>7}{tot['no_subtitle']:>7}"
|
||
f"{tot['error']:>7}{tot['dur']/3600:>12.1f}{tot['dur_ok']/3600:>14.1f}")
|
||
if no_date:
|
||
print(f"\n({no_date} 条无日期字段,已跳过)")
|
||
|
||
n_days = len(days)
|
||
if n_days:
|
||
print(f"\n共 {n_days} 天 | 日均处理 {tot['count']/n_days:.0f} 条 / "
|
||
f"{tot['dur']/3600/n_days:.1f} h | 日均成功 {tot['success']/n_days:.0f} 条 / "
|
||
f"{tot['dur_ok']/3600/n_days:.1f} h")
|
||
print(f"成功率 {tot['success']/tot['count']*100:.1f}% | "
|
||
f"成功时长占比 {tot['dur_ok']/tot['dur']*100:.1f}%" if tot['dur'] else "")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|