ttclips 1.0.0__py3-none-any.whl
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.
- ttclips/__init__.py +5 -0
- ttclips/__main__.py +5 -0
- ttclips/binutils.py +51 -0
- ttclips/broll.py +142 -0
- ttclips/clipper.py +168 -0
- ttclips/config.py +90 -0
- ttclips/downloader.py +128 -0
- ttclips/encoder.py +431 -0
- ttclips/gemini.py +187 -0
- ttclips/generator.py +103 -0
- ttclips/monitor.py +128 -0
- ttclips/scaffold.py +64 -0
- ttclips/scheduler.py +425 -0
- ttclips/state.py +125 -0
- ttclips/templates/channels.json +75 -0
- ttclips/templates/env.example +51 -0
- ttclips/templates/posting_window.json +12 -0
- ttclips/templates/tiktok.json +16 -0
- ttclips/uploader.py +577 -0
- ttclips-1.0.0.dist-info/METADATA +235 -0
- ttclips-1.0.0.dist-info/RECORD +25 -0
- ttclips-1.0.0.dist-info/WHEEL +5 -0
- ttclips-1.0.0.dist-info/entry_points.txt +5 -0
- ttclips-1.0.0.dist-info/licenses/LICENSE +21 -0
- ttclips-1.0.0.dist-info/top_level.txt +1 -0
ttclips/__init__.py
ADDED
ttclips/__main__.py
ADDED
ttclips/binutils.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Binary resolution helpers: locate ffmpeg/ffprobe for yt-dlp + encoder.
|
|
2
|
+
|
|
3
|
+
Resolution order: explicit env override (FFMPEG_BIN/FFPROBE_BIN) -> winget
|
|
4
|
+
Gyan.FFmpeg install -> system PATH. Returns None when nothing is found so
|
|
5
|
+
callers can fail loudly with a clear message.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import glob
|
|
10
|
+
import logging
|
|
11
|
+
import os
|
|
12
|
+
import shutil
|
|
13
|
+
from functools import lru_cache
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
from ttclips.config import load_env
|
|
17
|
+
|
|
18
|
+
log = logging.getLogger(__name__)
|
|
19
|
+
|
|
20
|
+
ENV = load_env()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def find_binary(env_key: str, name: str) -> str | None:
|
|
24
|
+
"""Resolve the path of *name* (e.g. 'ffmpeg.exe'), or None if unavailable."""
|
|
25
|
+
env_val = (ENV.get(env_key) or "").strip()
|
|
26
|
+
if env_val and Path(env_val).exists():
|
|
27
|
+
return env_val
|
|
28
|
+
# winget Gyan.FFmpeg layout: <Packages>/Gyan.FFmpeg*/ffmpeg-*-full_build/bin/
|
|
29
|
+
pkg_root = Path(os.environ.get("LOCALAPPDATA", "")) / "Microsoft" / "WinGet" / "Packages"
|
|
30
|
+
for candidate in glob.glob(str(pkg_root / "Gyan.FFmpeg*" / "*" / "bin" / name)):
|
|
31
|
+
return candidate
|
|
32
|
+
return shutil.which(name)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@lru_cache(maxsize=None)
|
|
36
|
+
def ffmpeg_bin() -> str | None:
|
|
37
|
+
return find_binary("FFMPEG_BIN", "ffmpeg.exe")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@lru_cache(maxsize=None)
|
|
41
|
+
def ffprobe_bin() -> str | None:
|
|
42
|
+
return find_binary("FFPROBE_BIN", "ffprobe.exe")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@lru_cache(maxsize=None)
|
|
46
|
+
def ffmpeg_dir() -> str | None:
|
|
47
|
+
"""Directory containing ffmpeg.exe (also holds ffprobe.exe), or None."""
|
|
48
|
+
bin_path = ffmpeg_bin()
|
|
49
|
+
if not bin_path:
|
|
50
|
+
return None
|
|
51
|
+
return str(Path(bin_path).parent)
|
ttclips/broll.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"""Stock b-roll for generated clips via the free Pexels Videos API."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import logging
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
|
|
9
|
+
from ttclips.config import DATA_DIR, load_env
|
|
10
|
+
from ttclips.encoder import probe_duration
|
|
11
|
+
|
|
12
|
+
log = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
ENV = load_env()
|
|
15
|
+
|
|
16
|
+
PEXELS_API_URL = "https://api.pexels.com/v1/videos/search"
|
|
17
|
+
BROLL_DIR = DATA_DIR / "broll"
|
|
18
|
+
BROLL_DIR.mkdir(parents=True, exist_ok=True)
|
|
19
|
+
|
|
20
|
+
PEXELS_TIMEOUT = 20
|
|
21
|
+
DOWNLOAD_TIMEOUT = 90
|
|
22
|
+
MIN_CLIP_SEC = 2.0
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _key() -> str:
|
|
26
|
+
key = ENV.get("PEXELS_API_KEY", "").strip()
|
|
27
|
+
if not key:
|
|
28
|
+
log.warning("PEXELS_API_KEY not set — generated clips will fall back to the gradient background")
|
|
29
|
+
return key
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _search(keyword: str, per_page: int = 5) -> list[dict]:
|
|
33
|
+
key = _key()
|
|
34
|
+
if not key:
|
|
35
|
+
return []
|
|
36
|
+
try:
|
|
37
|
+
resp = httpx.get(
|
|
38
|
+
PEXELS_API_URL,
|
|
39
|
+
params={"query": keyword, "per_page": per_page, "orientation": "portrait"},
|
|
40
|
+
headers={"Authorization": key},
|
|
41
|
+
timeout=PEXELS_TIMEOUT,
|
|
42
|
+
)
|
|
43
|
+
resp.raise_for_status()
|
|
44
|
+
return resp.json().get("videos", []) or []
|
|
45
|
+
except Exception as e:
|
|
46
|
+
log.warning("Pexels search failed for '%s': %s", keyword, e)
|
|
47
|
+
return []
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _best_file(video: dict) -> dict | None:
|
|
51
|
+
"""Pick the frame file best suited to a 1080x1920 crop.
|
|
52
|
+
|
|
53
|
+
Preferences: portrait > landscape, hd > sd, higher resolution, <= 1920 on
|
|
54
|
+
both axes, mp4 container.
|
|
55
|
+
"""
|
|
56
|
+
files = video.get("video_files") or []
|
|
57
|
+
mp4 = [f for f in files if (f.get("file_type") or "").endswith("mp4")] or files
|
|
58
|
+
scored = []
|
|
59
|
+
for f in mp4:
|
|
60
|
+
try:
|
|
61
|
+
w = int(f.get("width") or 0)
|
|
62
|
+
h = int(f.get("height") or 0)
|
|
63
|
+
except (TypeError, ValueError):
|
|
64
|
+
continue
|
|
65
|
+
if w <= 0 or h <= 0 or w > 1920 or h > 1920:
|
|
66
|
+
continue
|
|
67
|
+
portrait = 1.0 if h >= w else 0.5
|
|
68
|
+
hd = 1 if f.get("quality") == "hd" else 0
|
|
69
|
+
scored.append((hd, portrait * min(w, h), f))
|
|
70
|
+
if not scored:
|
|
71
|
+
return None
|
|
72
|
+
scored.sort(key=lambda item: (item[0], item[1]), reverse=True)
|
|
73
|
+
return scored[0][2]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _download(url: str, dest: Path) -> Path | None:
|
|
77
|
+
if dest.exists() and dest.stat().st_size > 0:
|
|
78
|
+
return dest
|
|
79
|
+
part = dest.with_suffix(dest.suffix + ".part")
|
|
80
|
+
try:
|
|
81
|
+
with httpx.stream("GET", url, follow_redirects=True, timeout=DOWNLOAD_TIMEOUT) as resp:
|
|
82
|
+
resp.raise_for_status()
|
|
83
|
+
with part.open("wb") as fh:
|
|
84
|
+
for chunk in resp.iter_bytes(65536):
|
|
85
|
+
fh.write(chunk)
|
|
86
|
+
part.replace(dest)
|
|
87
|
+
except Exception as e:
|
|
88
|
+
part.unlink(missing_ok=True)
|
|
89
|
+
dest.unlink(missing_ok=True)
|
|
90
|
+
log.warning("B-roll download failed (%s): %s", url[:80], e)
|
|
91
|
+
return None
|
|
92
|
+
if probe_duration(dest) < MIN_CLIP_SEC:
|
|
93
|
+
log.warning("B-roll too short: %s", dest.name)
|
|
94
|
+
dest.unlink(missing_ok=True)
|
|
95
|
+
return None
|
|
96
|
+
return dest
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def pick_background(keywords: list[str], min_duration: float = 30.0, label: str = "generated") -> Path | None:
|
|
100
|
+
"""Fetch the best available b-roll clip for the given topic keywords.
|
|
101
|
+
|
|
102
|
+
Ranks search results by their documented duration (shortest clip that
|
|
103
|
+
already covers the narration wins; otherwise the longest available), then
|
|
104
|
+
downloads the best first and caches each download in ``data/broll/``.
|
|
105
|
+
Returns a path to a local MP4 or ``None`` if nothing usable could be
|
|
106
|
+
fetched (caller falls back to the gradient background).
|
|
107
|
+
"""
|
|
108
|
+
if not _key():
|
|
109
|
+
return None
|
|
110
|
+
stem = (label or "generated").replace(" ", "_").replace("/", "_").replace(":", "_")[:40]
|
|
111
|
+
candidates: list[tuple[float, dict, dict, str]] = []
|
|
112
|
+
for kw in (keywords or []):
|
|
113
|
+
if not kw or not kw.strip():
|
|
114
|
+
continue
|
|
115
|
+
for video in _search(kw):
|
|
116
|
+
vf = _best_file(video)
|
|
117
|
+
if not vf or not vf.get("link"):
|
|
118
|
+
continue
|
|
119
|
+
try:
|
|
120
|
+
dur = float(video.get("duration") or 0.0)
|
|
121
|
+
except (TypeError, ValueError):
|
|
122
|
+
dur = 0.0
|
|
123
|
+
candidates.append((dur, video, vf, kw))
|
|
124
|
+
|
|
125
|
+
def _rank(c: tuple[float, dict, dict, str]):
|
|
126
|
+
dur = c[0]
|
|
127
|
+
return (0, dur) if dur >= min_duration else (1, -dur)
|
|
128
|
+
|
|
129
|
+
candidates.sort(key=_rank)
|
|
130
|
+
|
|
131
|
+
for dur, video, vf, kw in candidates:
|
|
132
|
+
dest = BROLL_DIR / f"{stem}_{video.get('id')}_{vf.get('id')}.mp4"
|
|
133
|
+
clip = _download(vf["link"], dest)
|
|
134
|
+
if not clip:
|
|
135
|
+
continue
|
|
136
|
+
actual = probe_duration(clip)
|
|
137
|
+
log.info("B-roll '%s' -> %s (%.0fs, %sx%s)",
|
|
138
|
+
kw, clip.name, actual, vf.get("width"), vf.get("height"))
|
|
139
|
+
return clip
|
|
140
|
+
|
|
141
|
+
log.warning("No usable b-roll found for keywords: %s", keywords)
|
|
142
|
+
return None
|
ttclips/clipper.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""Clip extraction: faster-whisper transcription + Gemini highlight ranking."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import logging
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from ttclips.config import load_env
|
|
9
|
+
from ttclips.gemini import call_with_model_retry, resolve_models
|
|
10
|
+
|
|
11
|
+
log = logging.getLogger(__name__)
|
|
12
|
+
ENV = load_env()
|
|
13
|
+
WHISPER_MODEL = ENV.get("WHISPER_MODEL", "small")
|
|
14
|
+
WHISPER_DEVICE = ENV.get("WHISPER_DEVICE", "auto")
|
|
15
|
+
|
|
16
|
+
_whisper_model = None
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _get_whisper():
|
|
20
|
+
global _whisper_model
|
|
21
|
+
if _whisper_model is None:
|
|
22
|
+
from faster_whisper import WhisperModel
|
|
23
|
+
log.info("Loading faster-whisper model '%s' (device=%s) ...", WHISPER_MODEL, WHISPER_DEVICE)
|
|
24
|
+
_whisper_model = WhisperModel(WHISPER_MODEL, device=WHISPER_DEVICE, compute_type="int8")
|
|
25
|
+
return _whisper_model
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def transcribe(video_path: Path, *, word_timestamps: bool = True) -> list[dict]:
|
|
29
|
+
"""Return whisper segments as [{start, end, text, words?}] with second precision.
|
|
30
|
+
|
|
31
|
+
``words`` (when enabled) is a list of {"word", "start", "end"} used by the
|
|
32
|
+
encoder for word-synced caption burning.
|
|
33
|
+
"""
|
|
34
|
+
model = _get_whisper()
|
|
35
|
+
segments, info = model.transcribe(
|
|
36
|
+
str(video_path),
|
|
37
|
+
word_timestamps=word_timestamps,
|
|
38
|
+
vad_filter=True,
|
|
39
|
+
beam_size=1,
|
|
40
|
+
)
|
|
41
|
+
out = []
|
|
42
|
+
for seg in segments:
|
|
43
|
+
words = None
|
|
44
|
+
if word_timestamps and seg.words:
|
|
45
|
+
words = [
|
|
46
|
+
{"word": w.word, "start": round(float(w.start), 2), "end": round(float(w.end), 2)}
|
|
47
|
+
for w in seg.words
|
|
48
|
+
]
|
|
49
|
+
out.append({
|
|
50
|
+
"start": round(float(seg.start), 2),
|
|
51
|
+
"end": round(float(seg.end), 2),
|
|
52
|
+
"text": seg.text.strip(),
|
|
53
|
+
"words": words,
|
|
54
|
+
})
|
|
55
|
+
log.info("Transcribed %s -> %d segments (~%.0fs audio)", video_path.name, len(out), info.duration or 0)
|
|
56
|
+
return out
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _transcript_text(segments: list[dict], min_gap: float = 2.0) -> str:
|
|
60
|
+
"""Compact transcript with merge of tiny gaps to bound token usage."""
|
|
61
|
+
merged = []
|
|
62
|
+
for s in segments:
|
|
63
|
+
if merged and s["start"] - merged[-1]["end"] <= min_gap:
|
|
64
|
+
merged[-1]["end"] = max(merged[-1]["end"], s["end"])
|
|
65
|
+
merged[-1]["text"] += " " + s["text"]
|
|
66
|
+
else:
|
|
67
|
+
merged.append(dict(s))
|
|
68
|
+
lines = [f"[{s['start']:.1f}-{s['end']:.1f}] {s['text']}" for s in merged]
|
|
69
|
+
return "\n".join(lines)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
HIGHLIGHT_PROMPT = """You are a short-form content editor for TikTok.
|
|
73
|
+
Below is a timestamped transcript of a video. Pick the strongest moments to
|
|
74
|
+
repurpose as short vertical clips. A strong moment usually: is self-contained
|
|
75
|
+
(podcast/explainer/quote style), has a hook within the first 3 seconds, delivers
|
|
76
|
+
one clear idea, and ends cleanly.
|
|
77
|
+
|
|
78
|
+
Constraints:
|
|
79
|
+
- Each highlight must be between 20 and 70 seconds long.
|
|
80
|
+
- Start/end values MUST come from actual transcript timestamps.
|
|
81
|
+
- Highlights must not overlap with each other.
|
|
82
|
+
- Return ONLY valid JSON matching this schema:
|
|
83
|
+
{"highlights": [{"start": <float>, "end": <float>, "score": <int 0-100>,
|
|
84
|
+
"hook": "<one catchy opening sentence>", "title": "<short TikTok caption>",
|
|
85
|
+
"hashtags": ["tag1", "tag2"], "reason": "<one line why it is viral>"}]}
|
|
86
|
+
- Each highlight must include up to 4 relevant hashtags: lowercase, no "#",
|
|
87
|
+
no duplicates, matched to the clip's topic/niche.
|
|
88
|
+
- Pick at most {max_h} highlights, ranked by score descending.
|
|
89
|
+
|
|
90
|
+
Transcript:
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _sanitize_hashtags(raw, limit: int = 4) -> list[str]:
|
|
95
|
+
"""Normalize LLM hashtags: strip '#', drop empties, dedupe case-insensitively."""
|
|
96
|
+
if not isinstance(raw, list):
|
|
97
|
+
return []
|
|
98
|
+
seen: set[str] = set()
|
|
99
|
+
out: list[str] = []
|
|
100
|
+
for item in raw:
|
|
101
|
+
tag = str(item).lstrip("#").strip()
|
|
102
|
+
if not tag or tag.lower() in seen:
|
|
103
|
+
continue
|
|
104
|
+
seen.add(tag.lower())
|
|
105
|
+
out.append(tag)
|
|
106
|
+
if len(out) >= limit:
|
|
107
|
+
break
|
|
108
|
+
return out
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def rank_highlights(segments: list[dict], num_clips: int = 3) -> list[dict]:
|
|
112
|
+
"""Rank transcript moments via Gemini. Returns highlight dicts."""
|
|
113
|
+
num_clips = max(1, int(num_clips))
|
|
114
|
+
text = _transcript_text(segments)
|
|
115
|
+
# Bound token input ~ 60k chars; chunk the transcript if longer.
|
|
116
|
+
text = text[:60000]
|
|
117
|
+
prompt = HIGHLIGHT_PROMPT.replace("{max_h}", str(num_clips)) + text
|
|
118
|
+
|
|
119
|
+
try:
|
|
120
|
+
from google import genai
|
|
121
|
+
from google.genai import types
|
|
122
|
+
except ImportError:
|
|
123
|
+
raise RuntimeError("google-genai is not installed. pip install google-genai")
|
|
124
|
+
|
|
125
|
+
key = ENV.get("GEMINI_API_KEY", "")
|
|
126
|
+
if not key:
|
|
127
|
+
raise RuntimeError("GEMINI_API_KEY missing from .env — see .env.example")
|
|
128
|
+
|
|
129
|
+
client = genai.Client(api_key=key)
|
|
130
|
+
models = resolve_models()
|
|
131
|
+
resp = call_with_model_retry(
|
|
132
|
+
lambda model: client.models.generate_content(
|
|
133
|
+
model=model,
|
|
134
|
+
contents=prompt,
|
|
135
|
+
config=types.GenerateContentConfig(
|
|
136
|
+
response_mime_type="application/json",
|
|
137
|
+
temperature=0.4,
|
|
138
|
+
),
|
|
139
|
+
),
|
|
140
|
+
models,
|
|
141
|
+
)
|
|
142
|
+
raw = resp.text or "{}"
|
|
143
|
+
data = json.loads(raw)
|
|
144
|
+
highlights = data.get("highlights", [])
|
|
145
|
+
# Validate & sanitize
|
|
146
|
+
valid = []
|
|
147
|
+
for h in highlights:
|
|
148
|
+
try:
|
|
149
|
+
start = float(h.get("start"))
|
|
150
|
+
end = float(h.get("end"))
|
|
151
|
+
except (TypeError, ValueError):
|
|
152
|
+
continue
|
|
153
|
+
if end - start < 20 or end - start > 70:
|
|
154
|
+
continue
|
|
155
|
+
if start < 0 or end <= start:
|
|
156
|
+
continue
|
|
157
|
+
h["start"], h["end"] = start, end
|
|
158
|
+
h["hashtags"] = _sanitize_hashtags(h.get("hashtags"))
|
|
159
|
+
valid.append(h)
|
|
160
|
+
valid.sort(key=lambda x: x.get("score", 0), reverse=True)
|
|
161
|
+
log.info("Gemini ranked %d valid highlights", len(valid))
|
|
162
|
+
return valid[:num_clips]
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def pick_best_highlight(segments: list[dict]) -> dict | None:
|
|
166
|
+
"""Rank then return the single best highlight (None if nothing found)."""
|
|
167
|
+
highlights = rank_highlights(segments, num_clips=1)
|
|
168
|
+
return highlights[0] if highlights else None
|
ttclips/config.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""Shared paths + config loading for ttclips.
|
|
2
|
+
|
|
3
|
+
Everything is resolved from a single *home* directory:
|
|
4
|
+
|
|
5
|
+
$TTCLIPS_HOME -> current working directory
|
|
6
|
+
|
|
7
|
+
This lets an installed CLI run against any project folder (``ttclips init``)
|
|
8
|
+
while a source checkout keeps working unchanged when the process CWD is the
|
|
9
|
+
repo root (``config/`` and ``data/`` live there).
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import json
|
|
14
|
+
import os
|
|
15
|
+
import re
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def resolve_home() -> Path:
|
|
20
|
+
"""Return the ttclips home dir ($TTCLIPS_HOME or the current directory)."""
|
|
21
|
+
raw = os.environ.get("TTCLIPS_HOME") or os.getcwd()
|
|
22
|
+
home = Path(raw).expanduser()
|
|
23
|
+
try:
|
|
24
|
+
home = home.resolve()
|
|
25
|
+
except OSError: # pragma: no cover - unusual filesystem edge case
|
|
26
|
+
pass
|
|
27
|
+
return home
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
HOME = resolve_home()
|
|
31
|
+
CONFIG_DIR = HOME / "config"
|
|
32
|
+
DATA_DIR = HOME / "data"
|
|
33
|
+
DOWNLOADS_DIR = DATA_DIR / "downloads"
|
|
34
|
+
CLIPS_DIR = DATA_DIR / "clips"
|
|
35
|
+
LOGS_DIR = DATA_DIR / "logs"
|
|
36
|
+
BROLL_DIR = DATA_DIR / "broll"
|
|
37
|
+
PROBE_DIR = DATA_DIR / "probe"
|
|
38
|
+
STATE_FILE = DATA_DIR / "posted.json"
|
|
39
|
+
QUEUE_FILE = DATA_DIR / "queue.json"
|
|
40
|
+
COOKIES_FILE = DATA_DIR / "cookies.txt"
|
|
41
|
+
LOG_FILE = LOGS_DIR / "ttclips.log"
|
|
42
|
+
|
|
43
|
+
for _d in (DATA_DIR, DOWNLOADS_DIR, CLIPS_DIR, LOGS_DIR):
|
|
44
|
+
_d.mkdir(parents=True, exist_ok=True)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def load_json(path: Path, default=None):
|
|
48
|
+
if not path.exists():
|
|
49
|
+
return default if default is not None else {}
|
|
50
|
+
return json.loads(path.read_text(encoding="utf-8"))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def load_channels() -> dict:
|
|
54
|
+
return load_json(CONFIG_DIR / "channels.json", default={"channels": []})
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def load_posting_window() -> list[dict]:
|
|
58
|
+
data = load_json(CONFIG_DIR / "posting_window.json", default={"slots": []})
|
|
59
|
+
return data.get("slots", [])
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def load_tiktok_config() -> dict:
|
|
63
|
+
return load_json(CONFIG_DIR / "tiktok.json", default={})
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def load_env() -> dict:
|
|
67
|
+
"""Minimal key=value dotenv loader (no external dep needed)."""
|
|
68
|
+
env_file = HOME / ".env"
|
|
69
|
+
if not env_file.exists():
|
|
70
|
+
return {}
|
|
71
|
+
out = {}
|
|
72
|
+
for line in env_file.read_text(encoding="utf-8").splitlines():
|
|
73
|
+
line = line.strip()
|
|
74
|
+
if not line or line.startswith("#") or "=" not in line:
|
|
75
|
+
continue
|
|
76
|
+
key, _, value = line.partition("=")
|
|
77
|
+
out[key.strip()] = value.strip().strip('"').strip("'")
|
|
78
|
+
return out
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
RESERVED_NAMES = {f"COM{i}" for i in range(1, 10)} | {f"LPT{i}" for i in range(1, 10)}
|
|
82
|
+
RESERVED_NAMES.update({"CON", "PRN", "AUX", "NUL"})
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def safe_stem(name: str) -> str:
|
|
86
|
+
"""Make a string safe as a Windows filename stem (no path separators / illegal chars)."""
|
|
87
|
+
cleaned = re.sub(r'[<>:"/\\|?*\x00-\x1f]', "_", name).strip(" .")
|
|
88
|
+
if cleaned.upper() in RESERVED_NAMES:
|
|
89
|
+
cleaned = "_" + cleaned
|
|
90
|
+
return cleaned[:60] or "clip"
|
ttclips/downloader.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""yt-dlp wrapper: download YouTube videos and extract metadata."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import logging
|
|
5
|
+
import re
|
|
6
|
+
import shutil
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from ttclips.binutils import ffmpeg_dir
|
|
10
|
+
from ttclips.config import DOWNLOADS_DIR
|
|
11
|
+
|
|
12
|
+
log = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _video_id_from_url(url: str) -> str | None:
|
|
16
|
+
m = re.search(r"[?&]v=([\w-]{6,})", url)
|
|
17
|
+
if m:
|
|
18
|
+
return m.group(1)
|
|
19
|
+
m = re.search(r"youtu\.be/([\w-]{6,})", url)
|
|
20
|
+
return m.group(1) if m else None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def download(url: str, *, filename_hint: str = "") -> Path:
|
|
24
|
+
"""Download video to downloads/ and return the path to the MP4 file.
|
|
25
|
+
|
|
26
|
+
If a previously-merged ``{video_id}.mp4`` already exists it is reused,
|
|
27
|
+
so a failed post step (e.g. Gemini outage) doesn't force a re-download.
|
|
28
|
+
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
url : str
|
|
32
|
+
YouTube watch URL.
|
|
33
|
+
filename_hint : str
|
|
34
|
+
Optional label for logging (e.g. the channel name).
|
|
35
|
+
"""
|
|
36
|
+
out_dir = DOWNLOADS_DIR / filename_hint.replace("/", "_") if filename_hint else DOWNLOADS_DIR
|
|
37
|
+
out_dir.mkdir(parents=True, exist_ok=True)
|
|
38
|
+
|
|
39
|
+
vid = _video_id_from_url(url)
|
|
40
|
+
if vid:
|
|
41
|
+
cached = out_dir / f"{vid}.mp4"
|
|
42
|
+
if cached.exists() and cached.stat().st_size > 0:
|
|
43
|
+
log.info("Reusing cached download: %s", cached)
|
|
44
|
+
return cached
|
|
45
|
+
|
|
46
|
+
if shutil.which("yt-dlp") is None:
|
|
47
|
+
log.warning("yt-dlp not found on PATH; falling back to python import")
|
|
48
|
+
log.info("Downloading %s (hint: %s)", url, filename_hint)
|
|
49
|
+
|
|
50
|
+
try:
|
|
51
|
+
import yt_dlp
|
|
52
|
+
except ImportError:
|
|
53
|
+
raise RuntimeError("yt-dlp is not installed. pip install yt-dlp")
|
|
54
|
+
|
|
55
|
+
ffmpeg_loc = ffmpeg_dir()
|
|
56
|
+
if ffmpeg_loc is None:
|
|
57
|
+
raise RuntimeError(
|
|
58
|
+
"ffmpeg not found — it is required to merge the video and audio "
|
|
59
|
+
"streams. Install it (e.g. `winget install Gyan.FFmpeg`) or set "
|
|
60
|
+
"FFMPEG_BIN in .env."
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
opts: dict = {
|
|
64
|
+
"paths": {"home": str(out_dir)},
|
|
65
|
+
"outtmpl": {"default": "%(id)s.%(ext)s"},
|
|
66
|
+
"format": "bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720][ext=mp4]/best",
|
|
67
|
+
"merge_output_format": "mp4",
|
|
68
|
+
"ffmpeg_location": ffmpeg_loc,
|
|
69
|
+
"quiet": True,
|
|
70
|
+
"no_warnings": True,
|
|
71
|
+
"restrictfilenames": True,
|
|
72
|
+
"writethumbnail": False,
|
|
73
|
+
"writesubtitles": False,
|
|
74
|
+
"ignoreerrors": True,
|
|
75
|
+
"retries": 3,
|
|
76
|
+
"fragment_retries": 5,
|
|
77
|
+
"socket_timeout": 30,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
with yt_dlp.YoutubeDL(opts) as ydl:
|
|
81
|
+
info = ydl.extract_info(url, download=True)
|
|
82
|
+
if info is None:
|
|
83
|
+
raise RuntimeError(f"yt-dlp returned no info for {url}")
|
|
84
|
+
out_path = ydl.prepare_filename(info)
|
|
85
|
+
# After format postprocessing the actual file may have a different ext.
|
|
86
|
+
mp4_path = out_dir / f"{info['id']}.mp4"
|
|
87
|
+
if mp4_path.exists():
|
|
88
|
+
out_path = mp4_path
|
|
89
|
+
elif not Path(out_path).exists():
|
|
90
|
+
# Try common postprocess result
|
|
91
|
+
for ext in ("mp4", "mkv", "webm"):
|
|
92
|
+
p = out_dir / f"{info['id']}.{ext}"
|
|
93
|
+
if p.exists():
|
|
94
|
+
out_path = p
|
|
95
|
+
break
|
|
96
|
+
else:
|
|
97
|
+
out_path = Path(out_path)
|
|
98
|
+
|
|
99
|
+
out_path = Path(out_path)
|
|
100
|
+
if not out_path.exists():
|
|
101
|
+
raise RuntimeError(
|
|
102
|
+
f"Download completed but output file is missing: {out_path}. "
|
|
103
|
+
"The video+audio merge likely failed — verify ffmpeg/ffprobe "
|
|
104
|
+
"resolved correctly (see data/logs/ttclips.log)."
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
log.info("Downloaded %s -> %s", info["id"], out_path)
|
|
108
|
+
return out_path
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def get_info(url: str) -> dict | None:
|
|
112
|
+
"""Get video info dict without downloading. Returns None on error."""
|
|
113
|
+
try:
|
|
114
|
+
import yt_dlp
|
|
115
|
+
except ImportError:
|
|
116
|
+
raise RuntimeError("yt-dlp is not installed")
|
|
117
|
+
opts: dict = {
|
|
118
|
+
"quiet": True,
|
|
119
|
+
"no_warnings": True,
|
|
120
|
+
"skip_download": True,
|
|
121
|
+
"simulate": True,
|
|
122
|
+
}
|
|
123
|
+
try:
|
|
124
|
+
with yt_dlp.YoutubeDL(opts) as ydl:
|
|
125
|
+
return ydl.extract_info(url, download=False)
|
|
126
|
+
except Exception as e:
|
|
127
|
+
log.warning("get_info failed for %s: %s", url, e)
|
|
128
|
+
return None
|