The plugin contract, the model task and the tools roledesk imports, copied rather than reached through an editable install of news, which also exposes a package named pipeline. news stays the source of truth for desk. Refreshed from news/desk at a275c38a1aa0fe80a7ec7dd5781ecccf683f71c6 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
4.3 KiB
Python
121 lines
4.3 KiB
Python
"""Cut a video up and print what came out.
|
|
|
|
python -m desk.tools.video <youtube url>
|
|
python -m desk.tools.video <youtube url> --json > segments.json
|
|
|
|
Videos only. A podcast episode — where the chapter marks and the article links
|
|
live on the show's own page rather than on YouTube — goes through the layer
|
|
above:
|
|
|
|
python -m desk.sources.podcast <episode url | feed url>
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import dataclasses
|
|
import json
|
|
import logging
|
|
import sys
|
|
|
|
import desk.tools.video as video_pkg
|
|
from desk.tools.video import marks
|
|
from desk.tools.video.segment import Segment, Video
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(
|
|
prog="python -m desk.tools.video",
|
|
description="Cut a video into one item per chapter mark.",
|
|
)
|
|
parser.add_argument("url", help="a YouTube link or bare video id")
|
|
parser.add_argument("--json", action="store_true",
|
|
help="dump everything, transcripts included, as JSON")
|
|
parser.add_argument("--chars", type=int, default=160,
|
|
help="how much transcript to preview per segment (default 160)")
|
|
parser.add_argument("--no-comments", action="store_true",
|
|
help="don't fall back to chapter marks typed in the comments")
|
|
parser.add_argument("-v", "--verbose", action="store_true")
|
|
args = parser.parse_args(argv)
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO if args.verbose else logging.WARNING,
|
|
format="%(levelname)-7s %(message)s",
|
|
)
|
|
# Windows still opens a console in the system codepage, and a video title
|
|
# with a "ñ" in it — or the arrows this prints — is an unhandled
|
|
# UnicodeEncodeError half way through the report rather than a bad glyph.
|
|
if hasattr(sys.stdout, "reconfigure"):
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
try:
|
|
video, segments = video_pkg.read(args.url, comments=not args.no_comments)
|
|
except Exception as exc:
|
|
print(f"error: {exc}", file=sys.stderr)
|
|
return 1
|
|
|
|
if args.json:
|
|
json.dump(dump(video, segments), sys.stdout, indent=2, default=str)
|
|
print()
|
|
else:
|
|
report(video, segments, args.chars)
|
|
return 0
|
|
|
|
|
|
def report(video: Video, segments: list[Segment], chars: int) -> None:
|
|
"""The human-readable view. Also used by the podcast module."""
|
|
words = sum(len(s.transcript.split()) for s in segments)
|
|
length = video.duration or (segments[-1].end if segments else 0)
|
|
print(f"{video.title}")
|
|
print(f" {video.author or '—'} {video.ref.kind}:{video.ref.key}"
|
|
f" {marks.hhmmss(length)}")
|
|
print(f" {len(segments)} segments, {words:,} words of transcript")
|
|
print()
|
|
|
|
heading = None
|
|
for segment in segments:
|
|
if segment.heading != heading:
|
|
heading = segment.heading
|
|
print(f" {heading or '(unfiled)'}")
|
|
preview = segment.transcript[:chars]
|
|
if len(segment.transcript) > chars:
|
|
preview += "…"
|
|
print(f" {segment.timecode:>8} {segment.title[:78]}")
|
|
print(f" {marks.hhmmss(segment.duration)} long · {segment.url()}")
|
|
for link in segment.links:
|
|
print(f" → {link.url}")
|
|
print(f" {preview or '(no transcript under this mark)'}")
|
|
print()
|
|
|
|
|
|
def dump(video: Video, segments: list[Segment]) -> dict[str, object]:
|
|
"""The shape a stored item is built from, plus the fields a dry run wants
|
|
to eyeball. The stored half is `Segment.record()` and is defined there;
|
|
desk/sources/podcast.py turns the rest into a row."""
|
|
return {
|
|
"video": {
|
|
"ref": dataclasses.asdict(video.ref),
|
|
"title": video.title,
|
|
"url": video.url,
|
|
"author": video.author,
|
|
"published_at": video.published_at,
|
|
"duration": video.duration,
|
|
},
|
|
"segments": [
|
|
{
|
|
**s.record(),
|
|
"external_id": s.external_id,
|
|
"title": s.title,
|
|
"heading": s.heading,
|
|
"timecode": s.timecode,
|
|
"url": s.url(),
|
|
"transcript": s.transcript,
|
|
}
|
|
for s in segments
|
|
],
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|