earshot-cli 0.8.1__tar.gz → 0.10.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/PKG-INFO +1 -1
  2. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/pyproject.toml +1 -1
  3. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/cli.py +38 -10
  4. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/config.py +31 -0
  5. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_wizard.py +58 -0
  6. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/uv.lock +1 -1
  7. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/.github/workflows/ci.yml +0 -0
  8. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/.github/workflows/publish.yml +0 -0
  9. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/.gitignore +0 -0
  10. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/LICENSE +0 -0
  11. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/README.md +0 -0
  12. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/scripts/download_models.sh +0 -0
  13. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/__init__.py +0 -0
  14. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/auth.py +0 -0
  15. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/capture.py +0 -0
  16. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/compose.py +0 -0
  17. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/deliver.py +0 -0
  18. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/extract.py +0 -0
  19. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/hardsub.py +0 -0
  20. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/live.py +0 -0
  21. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/models.py +0 -0
  22. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/subtitle.py +0 -0
  23. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/transcribe.py +0 -0
  24. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/src/earshot/transcribe_cloud.py +0 -0
  25. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/conftest.py +0 -0
  26. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_auth.py +0 -0
  27. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_compose.py +0 -0
  28. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_config.py +0 -0
  29. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_credential_lifecycle.py +0 -0
  30. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_deliver.py +0 -0
  31. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_deliver_api.py +0 -0
  32. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_do_deliver.py +0 -0
  33. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_extract.py +0 -0
  34. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_hardsub.py +0 -0
  35. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_live.py +0 -0
  36. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_me.py +0 -0
  37. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_subtitle.py +0 -0
  38. {earshot_cli-0.8.1 → earshot_cli-0.10.0}/tests/test_ua.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: earshot-cli
3
- Version: 0.8.1
3
+ Version: 0.10.0
4
4
  Summary: 媒体剪藏:视频字幕/系统声听录/本地音频/硬字幕OCR 四路摄入,端侧转写 → Markdown → 投递知识库(默认 ExoMind,可插拔)
5
5
  Project-URL: Homepage, https://github.com/helloworldtang/earshot
6
6
  Project-URL: Repository, https://github.com/helloworldtang/earshot
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "earshot-cli"
3
- version = "0.8.1"
3
+ version = "0.10.0"
4
4
  description = "媒体剪藏:视频字幕/系统声听录/本地音频/硬字幕OCR 四路摄入,端侧转写 → Markdown → 投递知识库(默认 ExoMind,可插拔)"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -24,7 +24,7 @@ from earshot.compose import (
24
24
  outbox_path,
25
25
  render_markdown,
26
26
  )
27
- from earshot.config import DEFAULT_EXOMIND_BASE_URL, Settings, load_settings
27
+ from earshot.config import DEFAULT_EXOMIND_BASE_URL, Settings, load_settings, load_state, save_state
28
28
  from earshot.deliver import DeliveryResult, deliver_exomind, deliver_exomind_api, deliver_webhook
29
29
  from earshot.extract import ExtractError, ffmpeg_available, to_wav16k, wav_duration
30
30
  from earshot.hardsub import FrameOcr, HardsubError, download_video, extract_hardsub, is_url, make_rapidocr_engine
@@ -40,10 +40,13 @@ EXIT_NO_CONTENT = 2
40
40
 
41
41
  _LEVEL_BLOCKS = "▁▂▃▄▅▆▇█"
42
42
 
43
+ # 各摄入路线的默认 tag(与来源语义对齐,不再是清一色「旁听」);手动补投无来源,用通用词
44
+ DEFAULT_TAG_BY_SOURCE = {"listen": "旁听", "video": "视频字幕", "audio": "本地转写", "ocr": "硬字幕"}
45
+
43
46
 
44
47
  def _add_common(p: argparse.ArgumentParser) -> None:
45
48
  p.add_argument("--title", help="笔记标题(默认取首句截断)")
46
- p.add_argument("--tag", action="append", default=None, help="投递标签,可多次(默认 旁听)")
49
+ p.add_argument("--tag", action="append", default=None, help="投递标签,可多次(默认按来源定)")
47
50
  p.add_argument("--note", default="", help="来源备注,如「B站·xxx第3期」,写进脚注")
48
51
  p.add_argument("--engine", choices=["local", "server"], default="local", help="转写引擎(默认 local 端侧)")
49
52
  p.add_argument(
@@ -75,7 +78,7 @@ def build_parser() -> argparse.ArgumentParser:
75
78
  p_video.add_argument("url", nargs="?", help="视频链接(B站等 yt-dlp 支持的平台;缺省时交互询问)")
76
79
  p_video.add_argument(
77
80
  "--cookies-from-browser",
78
- help="浏览器名(chrome/safari/firefox/edge),拉 AI 字幕(ai-zh)通常需要登录态",
81
+ help="浏览器名(chrome/safari/firefox/edge;none=匿名)。拉 AI 字幕(ai-zh)通常需要登录态;选择会被记住",
79
82
  )
80
83
 
81
84
  p_ocr = sub.add_parser("ocr", help="硬字幕视频 OCR 提取(字幕烧在画面里、无音轨的场景)")
@@ -154,7 +157,7 @@ def _run_pipeline(
154
157
 
155
158
  segments = add_punctuation(segments) # 无标点转写(B站AI字幕)按时间轴补齐;已有标点自动跳过
156
159
  title = derive_title(segments, args.title)
157
- tags = args.tag if args.tag else ["旁听"]
160
+ tags = args.tag if args.tag else [DEFAULT_TAG_BY_SOURCE.get(meta.source, "剪藏")]
158
161
  paragraphs = ["".join(s.text for s in group) for group in group_paragraphs(segments, chapter_breaks=chapter_breaks)]
159
162
  md = render_markdown(meta, paragraphs, title)
160
163
 
@@ -419,6 +422,28 @@ def _short_url(url: str, max_len: int = 60) -> str:
419
422
  return base
420
423
 
421
424
 
425
+ def _resolve_cookies(raw: str | None, settings: Settings) -> str | None:
426
+ """解析字幕登录态浏览器:显式旗标 > 上次的选择(落盘记忆)> 交互问一次。
427
+
428
+ 高频路径上「回车=chrome」是每回都付的税,问过一次就记住;显式旗标永远是
429
+ 最新信号,顺带更新记忆(none/n 表示匿名)。
430
+ """
431
+ if raw is not None:
432
+ cookies = None if raw.lower() in ("n", "none") else raw
433
+ save_state(settings.home, cookies_browser=cookies or "")
434
+ return cookies
435
+ remembered = load_state(settings.home).get("cookies_browser")
436
+ if remembered is not None:
437
+ print(f"→ 字幕登录态:{remembered or '匿名'}(上次的选择;--cookies-from-browser <名|none> 可改)")
438
+ return remembered or None
439
+ if _interactive():
440
+ ans = _prompt("字幕登录态浏览器(拉 AI 字幕用;回车=chrome,输 n 匿名;记住,之后不再问)", default="chrome")
441
+ cookies = None if ans.lower() in ("n", "none") else ans
442
+ save_state(settings.home, cookies_browser=cookies or "")
443
+ return cookies
444
+ return None
445
+
446
+
422
447
  def cmd_video(args: argparse.Namespace, settings: Settings) -> int:
423
448
  url = args.url
424
449
  if not url:
@@ -434,16 +459,18 @@ def cmd_video(args: argparse.Namespace, settings: Settings) -> int:
434
459
  if not url:
435
460
  print("✗ 未提供链接")
436
461
  return EXIT_FAIL
437
- cookies = args.cookies_from_browser
438
- if cookies is None and _interactive():
439
- ans = _prompt("字幕登录态浏览器(拉 AI 字幕用;回车=chrome,输 n 匿名)", default="chrome")
440
- cookies = None if ans.lower() == "n" else ans
462
+ cookies = _resolve_cookies(args.cookies_from_browser, settings)
441
463
  try:
442
464
  meta, segments = fetch_video_note(url, cookies)
443
465
  except SubtitleError as e:
444
466
  print(f"✗ {e}")
445
467
  return EXIT_FAIL
446
468
  print(f"→ {meta.title}(UP:{meta.uploader},字幕:{meta.subtitle_lang},{len(segments)} 段)")
469
+ if not segments:
470
+ # 字幕轨 0 条是正确事实(硬字幕烧在画面里),别让用户在这里空手而归——接住最常见的走错路
471
+ print("✗ 该视频无外挂字幕轨(疑似硬字幕烧录)")
472
+ print(f" → 可改走画面识别:earshot ocr {url}")
473
+ return EXIT_NO_CONTENT
447
474
  if not args.title:
448
475
  args.title = meta.title # 视频有正标题,优先于首句截断
449
476
  if not args.note:
@@ -553,7 +580,7 @@ def cmd_deliver(args: argparse.Namespace, settings: Settings) -> int:
553
580
  if not title:
554
581
  first = next((ln[2:].strip() for ln in md.splitlines() if ln.startswith("# ")), "")
555
582
  title = first or md_path.stem
556
- tags = args.tag if args.tag else ["旁听"]
583
+ tags = args.tag if args.tag else ["剪藏"] # 手动补投无来源信息,用通用 tag
557
584
  if args.deliver == "none":
558
585
  print("跳过投递(--deliver none)")
559
586
  return EXIT_OK
@@ -742,7 +769,8 @@ def main(argv: list[str] | None = None) -> int:
742
769
  if not _interactive():
743
770
  parser.print_help()
744
771
  return EXIT_FAIL
745
- choice = _prompt("earshot 模式 [1]视频字幕 [2]听录 [3]本地文件 [4]硬字幕OCR(回车=1;输入 q 退出)", default="1")
772
+ menu = "earshot 模式 [1]视频字幕 [2]听录 [3]本地音视频转写 [4]硬字幕OCR(回车=1;输入 q 退出)"
773
+ choice = _prompt(menu, default="1")
746
774
  if choice == "q":
747
775
  return EXIT_OK
748
776
  mapping = {"1": "video", "2": "listen", "3": "audio", "4": "ocr"}
@@ -18,6 +18,7 @@ EXOMIND_BASE_URL_ENV = "EXOMIND_BASE_URL"
18
18
 
19
19
  DEFAULT_EXOMIND_BASE_URL = "https://exomind-api.ai-as.cn"
20
20
  EXOMIND_CONFIG_FILENAME = ".exomind/config.json"
21
+ STATE_FILENAME = "state.json"
21
22
 
22
23
 
23
24
  @dataclass(frozen=True, slots=True)
@@ -69,6 +70,36 @@ def load_settings(
69
70
  )
70
71
 
71
72
 
73
+ def load_state(home: Path) -> dict[str, str]:
74
+ """读取本地状态文件(``~/.earshot/state.json``),不存在或损坏时返回空 dict。
75
+
76
+ 与 config 的区别:state 是 CLI 运行中产生的记忆(如上次的选择),可随时重建。
77
+
78
+ Args:
79
+ home: earshot 数据目录。
80
+ """
81
+ try:
82
+ raw = json.loads((home / STATE_FILENAME).read_text(encoding="utf-8"))
83
+ except (OSError, ValueError):
84
+ return {}
85
+ if not isinstance(raw, dict):
86
+ return {}
87
+ return {k: v for k, v in raw.items() if isinstance(v, str)}
88
+
89
+
90
+ def save_state(home: Path, **kv: str) -> None:
91
+ """合并写入本地状态文件(保留未涉及的键)。
92
+
93
+ Args:
94
+ home: earshot 数据目录。
95
+ **kv: 要写入的键值对(均为字符串)。
96
+ """
97
+ state = load_state(home)
98
+ state.update(kv)
99
+ home.mkdir(parents=True, exist_ok=True)
100
+ (home / STATE_FILENAME).write_text(json.dumps(state, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
101
+
102
+
72
103
  def load_exomind_config(path: Path | None = None) -> dict[str, str]:
73
104
  """读取 exomind CLI 的配置文件,不存在或损坏时返回空 dict。
74
105
 
@@ -1,11 +1,13 @@
1
1
  """交互向导单测:非 tty 的回退行为与裸 earshot 入口。"""
2
2
 
3
+ from datetime import datetime
3
4
  from pathlib import Path
4
5
 
5
6
  import pytest
6
7
 
7
8
  from earshot import cli
8
9
  from earshot.config import Settings
10
+ from earshot.transcribe import Segment
9
11
 
10
12
 
11
13
  def _settings() -> Settings:
@@ -79,3 +81,59 @@ def test_short_url_strips_tracking_and_truncates() -> None:
79
81
  assert cli._short_url(long_q) == "https://www.bilibili.com/video/BV16qpF64E6V/"
80
82
  assert cli._short_url("https://" + "a" * 100).endswith("…")
81
83
  assert len(cli._short_url("https://" + "a" * 100)) == 61
84
+
85
+
86
+ def test_video_no_subtitle_tracks_hints_ocr(monkeypatch: pytest.MonkeyPatch, capsys) -> None: # type: ignore[no-untyped-def]
87
+ """字幕轨 0 条(硬字幕)时不能空手而归:给出可执行的 ocr 改走指引。"""
88
+ from earshot.subtitle import VideoMeta
89
+
90
+ meta = VideoMeta(title="硬字幕视频", uploader="u", duration_s=1.0, url="https://b23.tv/x", subtitle_lang="")
91
+ monkeypatch.setattr(cli, "fetch_video_note", lambda _url, _cookies: (meta, []))
92
+ args = cli.build_parser().parse_args(["video", "https://b23.tv/x", "-y"])
93
+ rc = cli.cmd_video(args, _settings())
94
+ out = capsys.readouterr().out
95
+ assert rc == cli.EXIT_NO_CONTENT
96
+ assert "earshot ocr https://b23.tv/x" in out
97
+
98
+
99
+ def test_run_pipeline_default_tag_follows_source(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
100
+ """默认 tag 按摄入路线走(video→视频字幕),不再清一色「旁听」。"""
101
+ captured: dict[str, object] = {}
102
+ monkeypatch.setattr(cli, "_do_deliver", lambda *a: captured.update(tags=a[3]) or 0)
103
+ args = cli.build_parser().parse_args(["video", "https://x", "-y", "--out", str(tmp_path)])
104
+ meta = cli.NoteMeta(started_at=datetime.now(), duration_s=1.0, device="-", source="video", note="")
105
+ rc = cli._run_pipeline([Segment(0.0, 1.0, "内容")], meta, args, _settings())
106
+ assert rc == 0
107
+ assert captured["tags"] == ["视频字幕"]
108
+
109
+
110
+ def _settings_home(home: Path) -> Settings:
111
+ return Settings(
112
+ home=home,
113
+ model_root=home / "models",
114
+ exomind_base_url="https://api.example.com",
115
+ exomind_api_key="",
116
+ )
117
+
118
+
119
+ def test_resolve_cookies_asks_once_then_remembers(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
120
+ """交互问一次后落盘记忆:第二次起不再问,静默沿用上次选择。"""
121
+ settings = _settings_home(tmp_path)
122
+ monkeypatch.setattr(cli, "_interactive", lambda: True)
123
+ monkeypatch.setattr(cli, "_prompt", lambda *a, **k: "safari")
124
+ assert cli._resolve_cookies(None, settings) == "safari"
125
+ prompts: list[object] = []
126
+ monkeypatch.setattr(cli, "_prompt", lambda *a, **k: prompts.append(a) or "chrome")
127
+ assert cli._resolve_cookies(None, settings) == "safari" # 记忆生效
128
+ assert not prompts
129
+
130
+
131
+ def test_resolve_cookies_explicit_none_overrides_and_persists(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
132
+ """显式 --cookies-from-browser none 走匿名并覆盖记忆;之后无旗标也沿用匿名。"""
133
+ from earshot.config import load_state
134
+
135
+ settings = _settings_home(tmp_path)
136
+ monkeypatch.setattr(cli, "_interactive", lambda: True)
137
+ assert cli._resolve_cookies("none", settings) is None
138
+ assert load_state(settings.home)["cookies_browser"] == ""
139
+ assert cli._resolve_cookies(None, settings) is None # 匿名也被记住,且不再追问
@@ -331,7 +331,7 @@ wheels = [
331
331
 
332
332
  [[package]]
333
333
  name = "earshot-cli"
334
- version = "0.8.1"
334
+ version = "0.9.0"
335
335
  source = { editable = "." }
336
336
  dependencies = [
337
337
  { name = "httpx" },
File without changes
File without changes
File without changes