111 lines
3.9 KiB
Python
111 lines
3.9 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""Download all stored video messages, optionally reading frames and audio."""
|
|||
|
|
|
|||
|
|
import argparse
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
|
|||
|
|
from larkx.auth import LarkAuth
|
|||
|
|
from larkx.storage.models import Message
|
|||
|
|
|
|||
|
|
from snooper_client import SnifferLarkClient as LarkClient
|
|||
|
|
from snooper_storage import SnifferStorage
|
|||
|
|
from snooper_summary import DEFAULT_BASE_URL, DEFAULT_MODEL, MagpieClient, SummaryError
|
|||
|
|
from snooper_videos import (
|
|||
|
|
DEFAULT_ANALYSIS_DIR,
|
|||
|
|
DEFAULT_TRANSCRIPTION_MODEL,
|
|||
|
|
DEFAULT_VIDEO_DIR,
|
|||
|
|
enrich_videos,
|
|||
|
|
ensure_video_downloaded,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def stored_videos():
|
|||
|
|
storage = SnifferStorage()
|
|||
|
|
session = storage.Session()
|
|||
|
|
try:
|
|||
|
|
return (
|
|||
|
|
session.query(Message)
|
|||
|
|
.filter(Message.msg_type == 15)
|
|||
|
|
.order_by(Message.create_time, Message.id)
|
|||
|
|
.all()
|
|||
|
|
)
|
|||
|
|
finally:
|
|||
|
|
session.close()
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
parser = argparse.ArgumentParser(description="全量下载并读取本地库中的视频消息")
|
|||
|
|
parser.add_argument("--download-only", action="store_true", help="只下载,不调用模型")
|
|||
|
|
parser.add_argument("--no-audio", action="store_true", help="不转写视频音轨")
|
|||
|
|
parser.add_argument("--video-frames", type=int, default=12, help="每个视频最多抽帧数")
|
|||
|
|
parser.add_argument("--video-dir", default=str(DEFAULT_VIDEO_DIR))
|
|||
|
|
parser.add_argument("--analysis-dir", default=str(DEFAULT_ANALYSIS_DIR))
|
|||
|
|
parser.add_argument("--transcription-model", default=DEFAULT_TRANSCRIPTION_MODEL)
|
|||
|
|
parser.add_argument("--model", default=os.environ.get("MAGPIE_MODEL", DEFAULT_MODEL))
|
|||
|
|
parser.add_argument("--base-url", default=os.environ.get("MAGPIE_BASE_URL", DEFAULT_BASE_URL))
|
|||
|
|
parser.add_argument("--timeout", type=int, default=600)
|
|||
|
|
args = parser.parse_args()
|
|||
|
|
if args.video_frames < 1:
|
|||
|
|
print("--video-frames 必须大于等于 1", file=sys.stderr)
|
|||
|
|
return 2
|
|||
|
|
|
|||
|
|
ai_client = None
|
|||
|
|
if not args.download_only:
|
|||
|
|
try:
|
|||
|
|
ai_client = MagpieClient(
|
|||
|
|
model=args.model,
|
|||
|
|
base_url=args.base_url,
|
|||
|
|
api_key=os.environ.get("MAGPIE_API_KEY"),
|
|||
|
|
timeout=args.timeout,
|
|||
|
|
)
|
|||
|
|
ai_client.assert_model_available()
|
|||
|
|
except SummaryError as error:
|
|||
|
|
print(f"模型预检失败: {error}", file=sys.stderr)
|
|||
|
|
return 1
|
|||
|
|
|
|||
|
|
rows = stored_videos()
|
|||
|
|
print(f"本地库共有 {len(rows)} 条视频消息", flush=True)
|
|||
|
|
client = LarkClient(LarkAuth())
|
|||
|
|
if args.download_only:
|
|||
|
|
downloaded = 0
|
|||
|
|
failed = 0
|
|||
|
|
for index, row in enumerate(rows, 1):
|
|||
|
|
try:
|
|||
|
|
_, was_downloaded = ensure_video_downloaded(
|
|||
|
|
client, row, args.video_dir
|
|||
|
|
)
|
|||
|
|
downloaded += int(was_downloaded)
|
|||
|
|
except Exception: # noqa: BLE001 - continue downloading remaining videos
|
|||
|
|
failed += 1
|
|||
|
|
if index % 10 == 0 or index == len(rows):
|
|||
|
|
print(
|
|||
|
|
f"进度 {index}/{len(rows)}: 新下载 {downloaded},失败 {failed}",
|
|||
|
|
flush=True,
|
|||
|
|
)
|
|||
|
|
print(f"完成: 新下载 {downloaded} 个,失败 {failed} 个")
|
|||
|
|
return 1 if failed else 0
|
|||
|
|
|
|||
|
|
stats = enrich_videos(
|
|||
|
|
client,
|
|||
|
|
ai_client,
|
|||
|
|
rows,
|
|||
|
|
video_dir=args.video_dir,
|
|||
|
|
analysis_dir=args.analysis_dir,
|
|||
|
|
max_frames=args.video_frames,
|
|||
|
|
transcribe_audio=not args.no_audio,
|
|||
|
|
transcription_model=args.transcription_model,
|
|||
|
|
)
|
|||
|
|
print(
|
|||
|
|
"完成: "
|
|||
|
|
f"视频 {stats['enriched']}/{stats['candidates']},失败 {stats['failed']},"
|
|||
|
|
f"新下载 {stats['downloaded']},缓存命中 {stats['cached']},"
|
|||
|
|
f"音轨转写 {stats['audio_transcribed']}/{stats['audio']},"
|
|||
|
|
f"音轨失败 {stats['audio_failed']}"
|
|||
|
|
)
|
|||
|
|
return 1 if stats["failed"] or stats["audio_failed"] else 0
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
sys.exit(main())
|