-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverlay_video_generator.py
More file actions
1144 lines (953 loc) · 42.7 KB
/
Copy pathoverlay_video_generator.py
File metadata and controls
1144 lines (953 loc) · 42.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
overlay_video_generator.py
Brainrot Video Generator — Overlay Mode
Pipeline:
story input → evaluate → auto-trim → TTS → hook card → captions → gameplay → compose → upload → MP4
Usage:
# Single story
python overlay_video_generator.py --story input/stories/my_story.txt
python overlay_video_generator.py --reddit https://reddit.com/r/AmItheAsshole/comments/...
python overlay_video_generator.py --paste
# Scrape & batch
python overlay_video_generator.py --scrape AITA --count 5
python overlay_video_generator.py --batch input/stories/ --count 10
# Full auto — scrapes Reddit, downloads gameplay, generates, uploads to YouTube
python overlay_video_generator.py --auto
python overlay_video_generator.py --auto --subreddits AITA,tifu,confession --count 5 --upload
"""
import os, sys, json, re, math, asyncio, subprocess, shutil, argparse, textwrap, time, urllib.request
from pathlib import Path
from dataclasses import dataclass, field
from typing import Optional
# ── Folder layout ──────────────────────────────────────────────────────────────
ROOT = Path(__file__).parent
GAMEPLAY = ROOT / "assets" / "gameplay"
MUSIC = ROOT / "assets" / "music"
FONTS = ROOT / "assets" / "fonts"
INPUT = ROOT / "input" / "stories"
VID_OUT = ROOT / "output" / "videos"
AUD_OUT = ROOT / "output" / "audio"
SUB_OUT = ROOT / "output" / "subtitles"
TEMP = ROOT / "temp"
for _d in [GAMEPLAY, MUSIC, FONTS, INPUT, VID_OUT, AUD_OUT, SUB_OUT, TEMP]:
_d.mkdir(parents=True, exist_ok=True)
# ── Data objects ───────────────────────────────────────────────────────────────
@dataclass
class Story:
title: str
raw_text: str
cleaned_text: str = ""
source: str = "manual"
url: str = ""
subreddit: str = ""
@dataclass
class EvalResult:
score: float
hook_score: float
clarity_score: float
drama_score: float
length_score: float
recommended: bool
notes: list = field(default_factory=list)
@dataclass
class CaptionChunk:
text: str
start: float
end: float
@dataclass
class RenderConfig:
resolution: tuple = (1080, 1920)
fps: int = 30
font_size: int = 72
max_words: int = 5
max_lines: int = 2
bottom_margin: int = 220
voice: str = "en-US-AriaNeural"
crf: int = 18
max_duration: int = 90
hook_duration: float = 2.5 # seconds the title card is shown
music_volume: float = 0.0 # 0.0 = off, 0.08 = subtle background
# ── 1. Story loading ───────────────────────────────────────────────────────────
def load_story_from_file(path: str) -> Story:
p = Path(path)
text = p.read_text(encoding="utf-8").strip()
title = p.stem.replace("_", " ").replace("-", " ").title()
return Story(title=title, raw_text=text, source="file")
def load_story_from_json(path: str) -> Story:
data = json.loads(Path(path).read_text(encoding="utf-8"))
return Story(
title = data.get("title", "Untitled"),
raw_text = data.get("text", data.get("body", "")),
source = data.get("source", "json"),
url = data.get("url", ""),
subreddit = data.get("subreddit", ""),
)
def load_story_from_paste() -> Story:
print("\nPaste your Reddit story. Enter END on a blank line to finish:\n")
lines = []
while True:
line = input()
if line.strip().upper() == "END":
break
lines.append(line)
text = "\n".join(lines).strip()
title = input("\nEnter a title: ").strip() or "Untitled"
return Story(title=title, raw_text=text, source="paste")
def load_story_from_reddit_url(url: str) -> Story:
"""
Fetch a Reddit post via the public JSON API — no credentials needed.
Appends .json to the URL and parses the response.
"""
clean = url.split("?")[0].rstrip("/")
json_url = clean + ".json"
req = urllib.request.Request(json_url, headers={"User-Agent": "brainrot-generator/1.0"})
with urllib.request.urlopen(req, timeout=10) as resp:
data = json.loads(resp.read())
post = data[0]["data"]["children"][0]["data"]
return Story(
title = post["title"],
raw_text = post["selftext"],
source = "reddit",
url = url,
subreddit = post["subreddit"],
)
def scrape_subreddit(subreddit: str, count: int = 10, sort: str = "top",
time_filter: str = "week") -> list[Story]:
"""
Pull top posts from a subreddit via the public JSON API — no credentials needed.
Returns up to `count` Story objects.
"""
url = f"https://www.reddit.com/r/{subreddit}/{sort}.json?limit={count}&t={time_filter}"
req = urllib.request.Request(url, headers={"User-Agent": "brainrot-generator/1.0"})
with urllib.request.urlopen(req, timeout=10) as resp:
data = json.loads(resp.read())
stories = []
for child in data["data"]["children"]:
post = child["data"]
if not post.get("selftext") or post["selftext"] in ("[removed]", "[deleted]", ""):
continue
stories.append(Story(
title = post["title"],
raw_text = post["selftext"],
source = "reddit",
url = f"https://reddit.com{post['permalink']}",
subreddit = subreddit,
))
if len(stories) >= count:
break
print(f" Fetched {len(stories)} posts from r/{subreddit}")
return stories
def clean_story_text(story: Story) -> Story:
text = story.raw_text
# Remove Edit/Update sections
text = re.sub(r'\n+(Edit|Update|EDIT|UPDATE):.*', '', text, flags=re.DOTALL)
# Markdown links
text = re.sub(r'\[([^\]]+)\]\([^)]+\)', r'\1', text)
# Bold/italic
text = re.sub(r'\*{1,3}([^*]+)\*{1,3}', r'\1', text)
# u/username and r/subreddit
text = re.sub(r'[ur]/\S+', '', text)
# Collapse whitespace
text = re.sub(r'\n{3,}', '\n\n', text)
text = re.sub(r' {2,}', ' ', text)
story.cleaned_text = text.strip()
return story
# ── 2. Auto-trim long stories ─────────────────────────────────────────────────
def trim_story_to_limit(story: Story, max_words: int = 350) -> Story:
"""
If the story exceeds max_words, cut at the last sentence boundary before
the limit so the narration ends cleanly rather than mid-sentence.
"""
words = story.cleaned_text.split()
if len(words) <= max_words:
return story
# Find the last sentence-ending punctuation before the word limit
truncated = " ".join(words[:max_words])
last_end = max(truncated.rfind(". "), truncated.rfind("! "), truncated.rfind("? "))
if last_end > len(truncated) // 2:
# Cut at the clean sentence boundary
story.cleaned_text = truncated[:last_end + 1].strip()
else:
# No good boundary found — just use the word limit as-is
story.cleaned_text = truncated.strip()
trimmed_count = len(story.cleaned_text.split())
print(f" Auto-trimmed: {len(words)} → {trimmed_count} words")
return story
# ── 3. Story evaluator ────────────────────────────────────────────────────────
def evaluate_story_rules(story: Story) -> EvalResult:
text = story.cleaned_text or story.raw_text
words = text.split()
word_count = len(words)
sentences = [s.strip() for s in re.split(r'[.!?]+', text) if s.strip()]
notes = []
# Length
if word_count < 60:
length_score = 3.0; notes.append("Too short")
elif word_count < 100:
length_score = 6.0; notes.append("Short — may feel rushed")
elif word_count <= 350:
length_score = 9.5; notes.append("Good length")
elif word_count <= 500:
length_score = 7.0; notes.append("Slightly long")
else:
length_score = 4.0; notes.append("Too long — will be auto-trimmed")
# Hook
first = sentences[0] if sentences else ""
hook_score = 5.0
if any(w in first.lower() for w in ["i", "my", "found out", "told me", "yesterday", "today"]):
hook_score += 2.0; notes.append("Strong personal hook")
if any(w in first.lower() for w in ["never", "lied", "cheated", "fired", "broke", "called"]):
hook_score += 1.5; notes.append("Dramatic opener")
if len(first.split()) < 6:
hook_score += 1.0; notes.append("Punchy first sentence")
hook_score = min(hook_score, 10.0)
# Drama
drama_keywords = [
"cheated", "lied", "betrayed", "kicked out", "fired", "broke up", "divorce",
"confronted", "screamed", "cried", "refused", "threatened", "blocked", "ghosted",
"exposed", "caught", "admitted", "confessed", "furious", "devastated", "shocked",
"hurt", "angry", "jealous", "cut off", "family", "boyfriend", "girlfriend",
"husband", "wife", "mom", "dad", "sister", "brother",
]
hits = sum(1 for kw in drama_keywords if kw in text.lower())
drama_score = min(4.0 + hits * 0.5, 10.0)
notes.append("High drama" if hits >= 6 else "Moderate drama" if hits >= 3 else "Low drama")
# Clarity
reddit_refs = len(re.findall(r'\b(OP|NTA|YTA|ESH|NAH|AITA|WIBTA|TLDR)\b', text))
clarity_score = max(4.0, 9.0 - reddit_refs * 0.5)
if reddit_refs > 3:
notes.append("Heavy Reddit jargon")
score = (hook_score * 0.25 + drama_score * 0.35 +
clarity_score * 0.20 + length_score * 0.20)
return EvalResult(
score = round(score, 1),
hook_score = round(hook_score, 1),
drama_score = round(drama_score, 1),
clarity_score = round(clarity_score, 1),
length_score = round(length_score, 1),
recommended = score >= 6.5,
notes = notes,
)
def evaluate_story_llm(story: Story) -> EvalResult:
try:
import openai
except ImportError:
return evaluate_story_rules(story)
if not os.getenv("OPENAI_API_KEY"):
return evaluate_story_rules(story)
client = openai.OpenAI()
prompt = f"""Evaluate this Reddit story for TikTok brainrot narration. Return ONLY valid JSON:
{{
"score": <0-10>, "hook_score": <0-10>, "clarity_score": <0-10>,
"drama_score": <0-10>, "length_score": <0-10>,
"recommended": <bool — true if score >= 6.5>,
"notes": [<strings>]
}}
Story: {story.cleaned_text[:2000]}"""
try:
resp = client.chat.completions.create(
model = "gpt-4o-mini",
messages = [{"role": "user", "content": prompt}],
response_format = {"type": "json_object"},
)
return EvalResult(**json.loads(resp.choices[0].message.content))
except Exception as e:
print(f" LLM eval failed ({e}), falling back to rules")
return evaluate_story_rules(story)
def evaluate_story(story: Story) -> EvalResult:
return evaluate_story_llm(story) if os.getenv("OPENAI_API_KEY") else evaluate_story_rules(story)
# ── 4. TTS generation ─────────────────────────────────────────────────────────
async def _generate_tts(text: str, output_path: Path, voice: str) -> list[dict]:
try:
import edge_tts
except ImportError:
print("edge-tts not installed — run: pip install edge-tts")
sys.exit(1)
communicate = edge_tts.Communicate(text, voice)
raw = []
with open(output_path, "wb") as fh:
async for chunk in communicate.stream():
if chunk["type"] == "audio":
fh.write(chunk["data"])
elif chunk["type"] in ("WordBoundary", "SentenceBoundary"):
start = chunk["offset"] / 10_000_000
dur = chunk.get("duration", 0) / 10_000_000
raw.append({"word": chunk.get("text", ""), "start": round(start, 3), "dur": round(dur, 3)})
boundaries = []
for i, b in enumerate(raw):
end = raw[i + 1]["start"] if i + 1 < len(raw) else b["start"] + max(b["dur"], 0.3)
boundaries.append({"word": b["word"], "start": b["start"], "end": round(end, 3)})
return boundaries
def generate_voiceover(text: str, output_path: Path, voice: str = "en-US-AriaNeural") -> list[dict]:
return asyncio.run(_generate_tts(text, output_path, voice))
def get_audio_duration(path: Path) -> float:
r = subprocess.run(
["ffprobe", "-v", "error", "-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1", str(path)],
capture_output=True, text=True,
)
return float(r.stdout.strip())
# ── 5. Caption generation ─────────────────────────────────────────────────────
def _estimate_chunks(text: str, duration: float, max_words: int, max_lines: int) -> list[CaptionChunk]:
words = text.split()
chunk_size = max_words * max_lines
total = len(words)
chunks = []
i = 0
while i < total:
group = words[i : i + chunk_size]
chunks.append(CaptionChunk(
text = " ".join(group),
start = round((i / total) * duration, 3),
end = round((min(i + len(group), total) / total) * duration, 3),
))
i += chunk_size
return chunks
def build_caption_chunks(boundaries: list[dict], text: str,
duration: float, cfg: RenderConfig) -> list[CaptionChunk]:
if boundaries:
chunks = [CaptionChunk(b["word"].strip(), b["start"], b["end"])
for b in boundaries if b["word"].strip()]
if chunks:
return chunks
return _estimate_chunks(text, duration, cfg.max_words, cfg.max_lines)
def _t(s: float) -> str:
h, rem = divmod(s, 3600)
m, sec = divmod(rem, 60)
return f"{int(h)}:{int(m):02d}:{sec:05.2f}"
def build_ass_subtitles(chunks: list[CaptionChunk], cfg: RenderConfig,
hook_duration: float = 0.0) -> str:
"""
Generate ASS subtitle file. If hook_duration > 0, captions are shifted
forward to account for the title card at the start.
"""
W, H = cfg.resolution
fs = cfg.font_size
vm = cfg.bottom_margin
header = textwrap.dedent(f"""\
[Script Info]
ScriptType: v4.00+
PlayResX: {W}
PlayResY: {H}
WrapStyle: 1
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,Arial,{fs},&H00FFFFFF,&H000000FF,&H00000000,&HA0000000,-1,0,0,0,100,100,1,0,1,4,2,2,40,40,{vm},1
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
""")
events = []
for chunk in chunks:
start = chunk.start + hook_duration
end = chunk.end + hook_duration
words = chunk.text.split()
mid = math.ceil(len(words) / 2)
text = " ".join(words[:mid]) + r"\N" + " ".join(words[mid:]) if len(words) > cfg.max_words else chunk.text
events.append(f"Dialogue: 0,{_t(start)},{_t(end)},Default,,0,0,0,,{text}")
return header + "\n".join(events) + "\n"
def write_ass_file(chunks: list[CaptionChunk], cfg: RenderConfig,
output_path: Path, hook_duration: float = 0.0) -> Path:
output_path.write_text(build_ass_subtitles(chunks, cfg, hook_duration), encoding="utf-8")
return output_path
# ── 6. Gameplay handling ──────────────────────────────────────────────────────
# Search queries rotated randomly so videos vary between runs
GAMEPLAY_SEARCHES = [
"subway surfers gameplay no commentary vertical",
"minecraft parkour gameplay satisfying vertical",
"geometry dash gameplay vertical no commentary",
"temple run gameplay vertical no commentary",
"stack ball gameplay satisfying vertical",
"helix jump gameplay satisfying vertical",
"infinite runner mobile gameplay vertical",
"satisfying minecraft build timelapse vertical",
]
def fetch_gameplay_from_youtube(query: Optional[str] = None) -> Path:
"""
Download a gameplay clip from YouTube using yt-dlp.
Picks a random search query from GAMEPLAY_SEARCHES if none given.
Caches to assets/gameplay/ so it's reused on the next run.
"""
if not shutil.which("yt-dlp"):
raise RuntimeError(
"yt-dlp not installed.\n"
"Run: pip install yt-dlp or brew install yt-dlp"
)
import random
search = query or random.choice(GAMEPLAY_SEARCHES)
slug = re.sub(r'[^a-z0-9]+', '_', search.lower())[:40]
out = GAMEPLAY / f"{slug}.mp4"
if out.exists():
print(f" Using cached gameplay: {out.name}")
return out
print(f" Downloading gameplay: \"{search}\"")
cmd = [
"yt-dlp",
f"ytsearch1:{search}", # grab the top result
"--format", "bestvideo[ext=mp4][height<=1920]+bestaudio[ext=m4a]/best[ext=mp4]/best",
"--merge-output-format", "mp4",
"--output", str(out),
"--no-playlist",
"--quiet", "--no-warnings",
"--max-filesize", "200M", # skip huge files
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0 or not out.exists():
raise RuntimeError(f"yt-dlp failed:\n{result.stderr[-500:]}")
print(f" Saved → {out.name}")
return out
def find_gameplay_video(requested: Optional[str] = None, auto_fetch: bool = True) -> Path:
"""
Return a gameplay video path. Priority:
1. Explicitly requested path
2. Cached file in assets/gameplay/
3. Auto-download from YouTube (if auto_fetch=True)
"""
if requested:
p = Path(requested)
if p.exists():
return p
raise FileNotFoundError(f"Gameplay video not found: {requested}")
candidates = list(GAMEPLAY.glob("*.mp4")) + list(GAMEPLAY.glob("*.mov"))
if candidates:
import random
return random.choice(candidates) # rotate through cached clips
if auto_fetch:
return fetch_gameplay_from_youtube()
raise FileNotFoundError(
f"No gameplay videos in {GAMEPLAY}/\n"
"Run with --auto-gameplay or drop a .mp4 there manually."
)
def find_music_file(requested: Optional[str] = None) -> Optional[Path]:
if requested:
p = Path(requested)
return p if p.exists() else None
candidates = list(MUSIC.glob("*.mp3")) + list(MUSIC.glob("*.m4a")) + list(MUSIC.glob("*.wav"))
return candidates[0] if candidates else None
def get_video_duration(path: Path) -> float:
r = subprocess.run(
["ffprobe", "-v", "error", "-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1", str(path)],
capture_output=True, text=True,
)
return float(r.stdout.strip())
# ── 7. Hook title card ────────────────────────────────────────────────────────
def render_hook_card(title: str, gameplay_path: Path, duration: float,
cfg: RenderConfig, output_path: Path) -> Path:
"""
Render a hook title card: gameplay background + large centered title text
for `duration` seconds. Uses ffmpeg drawtext filter.
"""
W, H = cfg.resolution
# Escape special characters for ffmpeg drawtext
safe_title = title.replace("'", "\\'").replace(":", "\\:").replace(",", "\\,")
# Wrap at ~25 chars
words, lines, cur = title.split(), [], []
for w in words:
cur.append(w)
if len(" ".join(cur)) > 25:
lines.append(" ".join(cur[:-1]))
cur = [w]
if cur:
lines.append(" ".join(cur))
display = r"\n".join(lines)
safe_display = display.replace("'", "\\'").replace(":", "\\:").replace(",", "\\,")
vf = (
f"scale={W}:{H}:force_original_aspect_ratio=increase,crop={W}:{H},"
f"drawtext=text='{safe_display}':fontsize=80:fontcolor=white:"
f"borderw=4:bordercolor=black:shadowx=3:shadowy=3:"
f"x=(w-text_w)/2:y=(h-text_h)/2:line_spacing=20,"
f"drawtext=text='AITA?':fontsize=48:fontcolor=yellow:"
f"borderw=3:bordercolor=black:x=(w-text_w)/2:y=(h/2)+100"
)
loop = max(1, int(math.ceil(duration / get_video_duration(gameplay_path))) + 1)
cmd = [
"ffmpeg", "-y",
"-stream_loop", str(loop),
"-i", str(gameplay_path),
"-vf", vf,
"-t", str(duration),
"-c:v", "libx264", "-crf", str(cfg.crf), "-preset", "fast",
"-an",
str(output_path),
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"Hook card render failed:\n{result.stderr[-1000:]}")
return output_path
# ── 8. Final composition ──────────────────────────────────────────────────────
def compose_overlay_video(
gameplay_path: Path,
audio_path: Path,
subs_path: Path,
output_path: Path,
audio_dur: float,
cfg: RenderConfig,
hook_path: Optional[Path] = None,
music_path: Optional[Path] = None,
) -> Path:
W, H = cfg.resolution
loop = int(math.ceil(audio_dur / get_video_duration(gameplay_path))) + 1
body_dur = min(audio_dur + 0.5, cfg.max_duration)
# ── Step A: render the narrated gameplay body
body_path = TEMP / f"body_{output_path.stem}.mp4"
vf_body = (
f"scale={W}:{H}:force_original_aspect_ratio=increase,"
f"crop={W}:{H},"
f"subtitles='{subs_path}':force_style=''"
)
# Build audio filter — mix narration with optional background music
if music_path and cfg.music_volume > 0:
audio_inputs = ["-i", str(audio_path), "-i", str(music_path)]
# Loop music and mix at low volume
af = (
f"[1:a]volume={cfg.music_volume},aloop=loop=-1:size=2e+09[music];"
f"[0:a][music]amix=inputs=2:duration=first[aout]"
)
audio_map = ["-filter_complex", af, "-map", "0:v:0", "-map", "[aout]"]
else:
audio_inputs = ["-i", str(audio_path)]
audio_map = ["-map", "0:v:0", "-map", "1:a:0"]
cmd_body = [
"ffmpeg", "-y",
"-stream_loop", str(loop), "-i", str(gameplay_path),
*audio_inputs,
"-vf", vf_body,
*audio_map,
"-c:v", "libx264", "-crf", str(cfg.crf), "-preset", "fast",
"-c:a", "aac", "-b:a", "192k",
"-t", str(body_dur),
"-movflags", "+faststart",
"-r", str(cfg.fps),
str(body_path),
]
result = subprocess.run(cmd_body, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"ffmpeg body render failed:\n{result.stderr[-2000:]}")
# ── Step B: if no hook card, body is the final output
if hook_path is None:
shutil.move(str(body_path), str(output_path))
return output_path
# ── Step C: concatenate hook card + body
list_file = TEMP / f"concat_{output_path.stem}.txt"
list_file.write_text(
f"file '{hook_path.resolve()}'\nfile '{body_path.resolve()}'\n"
)
cmd_concat = [
"ffmpeg", "-y",
"-f", "concat", "-safe", "0", "-i", str(list_file),
"-c", "copy",
"-movflags", "+faststart",
str(output_path),
]
result = subprocess.run(cmd_concat, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"ffmpeg concat failed:\n{result.stderr[-1000:]}")
# Clean up temp files
body_path.unlink(missing_ok=True)
list_file.unlink(missing_ok=True)
return output_path
# ── 9. Checks ─────────────────────────────────────────────────────────────────
def check_dependencies():
missing = []
if not shutil.which("ffmpeg"):
missing.append("ffmpeg → brew install ffmpeg")
if not shutil.which("ffprobe"):
missing.append("ffprobe → included with ffmpeg")
try:
import edge_tts # noqa
except ImportError:
missing.append("edge-tts → pip install edge-tts")
if missing:
print("Missing dependencies:")
for m in missing:
print(f" • {m}")
sys.exit(1)
# ── 10. Single-story pipeline ─────────────────────────────────────────────────
def run_pipeline(
story: Story,
gameplay_path: Optional[str] = None,
music_path: Optional[str] = None,
cfg: RenderConfig = None,
skip_eval: bool = False,
force: bool = False,
hook: bool = True,
) -> Optional[Path]:
if cfg is None:
cfg = RenderConfig()
slug = re.sub(r'[^a-z0-9]+', '_', story.title.lower())[:40].strip('_')
name = f"{slug}_{int(time.time())}"
print(f"\n{'='*60}")
print(f" Story : {story.title}")
print(f" Source: {story.source}")
print(f"{'='*60}")
# 1. Clean
print("\n[1/7] Cleaning story text...")
story = clean_story_text(story)
print(f" {len(story.cleaned_text.split())} words")
# 2. Evaluate
if not skip_eval:
print("\n[2/7] Evaluating story quality...")
result = evaluate_story(story)
print(f" Score: {result.score}/10 | Recommended: {result.recommended}")
for note in result.notes:
print(f" • {note}")
if not result.recommended and not force:
print(f" Skipping (score {result.score} < 6.5). Use --force to override.")
return None
else:
print("\n[2/7] Skipping evaluation")
# 3. Auto-trim
print("\n[3/7] Checking story length...")
story = trim_story_to_limit(story, max_words=350)
# 4. TTS
print(f"\n[4/7] Generating voiceover ({cfg.voice})...")
audio_path = AUD_OUT / f"{name}.mp3"
boundaries = generate_voiceover(story.cleaned_text, audio_path, cfg.voice)
audio_dur = get_audio_duration(audio_path)
print(f" {audio_dur:.1f}s audio | {len(boundaries)} caption boundaries")
# 5. Captions
print("\n[5/7] Building captions...")
chunks = build_caption_chunks(boundaries, story.cleaned_text, audio_dur, cfg)
subs_path = SUB_OUT / f"{name}.ass"
hook_dur = cfg.hook_duration if hook else 0.0
write_ass_file(chunks, cfg, subs_path, hook_duration=hook_dur)
print(f" {len(chunks)} chunks → {subs_path.name}")
# 6. Gameplay + optional hook card
print("\n[6/7] Preparing video assets...")
gp_path = find_gameplay_video(gameplay_path, auto_fetch=True)
music_file = find_music_file(music_path)
gp_dur = get_video_duration(gp_path)
print(f" Gameplay: {gp_path.name} ({gp_dur:.1f}s)")
if music_file:
print(f" Music: {music_file.name} @ volume {cfg.music_volume}")
else:
print(" Music: none (drop an mp3 in assets/music/ to enable)")
hook_card = None
if hook and cfg.hook_duration > 0:
print(f" Rendering hook title card ({cfg.hook_duration}s)...")
hook_card = TEMP / f"hook_{name}.mp4"
render_hook_card(story.title, gp_path, cfg.hook_duration, cfg, hook_card)
# 7. Compose
print("\n[7/7] Composing final video...")
out_path = VID_OUT / f"{name}.mp4"
compose_overlay_video(
gameplay_path = gp_path,
audio_path = audio_path,
subs_path = subs_path,
output_path = out_path,
audio_dur = audio_dur,
cfg = cfg,
hook_path = hook_card,
music_path = music_file,
)
if hook_card:
hook_card.unlink(missing_ok=True)
size_mb = out_path.stat().st_size / 1_000_000
total = min(audio_dur + hook_dur, cfg.max_duration + hook_dur)
print(f"\n{'='*60}")
print(f" Done! → {out_path}")
print(f" Size: {size_mb:.1f} MB | Duration: {total:.1f}s")
print(f"{'='*60}\n")
return out_path
# ── 11. Batch pipeline ────────────────────────────────────────────────────────
def run_batch(stories: list[Story], **kwargs) -> list[Path]:
"""
Run the pipeline over a list of stories, skipping ones that fail evaluation.
Returns list of successfully generated video paths.
"""
total = len(stories)
outputs = []
print(f"\n Batch mode: {total} stories queued\n")
for i, story in enumerate(stories, 1):
print(f"[{i}/{total}] {story.title[:60]}")
try:
out = run_pipeline(story, **kwargs)
if out:
outputs.append(out)
except Exception as e:
print(f" ERROR: {e} — skipping\n")
print(f"\n Batch complete: {len(outputs)}/{total} videos generated")
for p in outputs:
print(f" → {p}")
return outputs
# ── 12. YouTube upload ────────────────────────────────────────────────────────
YOUTUBE_SCOPES = ["https://www.googleapis.com/auth/youtube.upload"]
YOUTUBE_TOKEN = ROOT / ".youtube_token.json"
YOUTUBE_SECRETS = ROOT / "client_secrets.json"
# Subreddits known to produce good brainrot content
DEFAULT_SUBREDDITS = ["AmItheAsshole", "tifu", "confession", "offmychest", "relationship_advice"]
SHORTS_DESCRIPTION_TEMPLATE = """{title}
#{subreddit} #reddit #redditstories #storytime #shorts #brainrot #aita"""
def _get_youtube_service():
"""
Authenticate with YouTube Data API v3.
First run opens a browser for OAuth consent — token is cached after that.
Requires client_secrets.json in the project root (from Google Cloud Console).
"""
try:
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
import googleapiclient.discovery
except ImportError:
raise RuntimeError(
"YouTube upload deps missing.\n"
"Run: pip install google-api-python-client google-auth-oauthlib google-auth-httplib2"
)
if not YOUTUBE_SECRETS.exists():
raise FileNotFoundError(
f"Missing {YOUTUBE_SECRETS}\n"
"Create a project at console.cloud.google.com, enable YouTube Data API v3,\n"
"download OAuth 2.0 client credentials as client_secrets.json and place it here."
)
creds = None
if YOUTUBE_TOKEN.exists():
creds = Credentials.from_authorized_user_file(str(YOUTUBE_TOKEN), YOUTUBE_SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(str(YOUTUBE_SECRETS), YOUTUBE_SCOPES)
creds = flow.run_local_server(port=0)
YOUTUBE_TOKEN.write_text(creds.to_json())
return googleapiclient.discovery.build("youtube", "v3", credentials=creds)
def upload_to_youtube(video_path: Path, story: Story) -> Optional[str]:
"""
Upload a video to YouTube as a Short.
Returns the video URL on success, None on failure.
Vertical videos under 60s are auto-classified as Shorts by YouTube.
"""
print("\n Uploading to YouTube...")
try:
youtube = _get_youtube_service()
import googleapiclient.http
description = SHORTS_DESCRIPTION_TEMPLATE.format(
title = story.title,
subreddit = story.subreddit or "reddit",
)
body = {
"snippet": {
"title": story.title[:100],
"description": description,
"tags": ["reddit", "shorts", "brainrot", "storytime",
story.subreddit or "aita", "redditstories"],
"categoryId": "22", # People & Blogs
},
"status": {
"privacyStatus": "public",
"selfDeclaredMadeForKids": False,
},
}
media = googleapiclient.http.MediaFileUpload(
str(video_path),
mimetype = "video/mp4",
resumable = True,
chunksize = 1024 * 1024 * 5, # 5 MB chunks
)
request = youtube.videos().insert(part="snippet,status", body=body, media_body=media)
response = None
while response is None:
status, response = request.next_chunk()
if status:
pct = int(status.progress() * 100)
print(f"\r Uploading... {pct}%", end="", flush=True)
video_id = response["id"]
video_url = f"https://youtube.com/shorts/{video_id}"
print(f"\r Uploaded → {video_url} ")
return video_url
except Exception as e:
print(f" Upload failed: {e}")
return None
# ── 13. Full auto mode ────────────────────────────────────────────────────────
def run_auto(
subreddits: list[str] = None,
count: int = 3,
upload: bool = False,
cfg: RenderConfig = None,
gameplay_query: Optional[str] = None,
) -> list[dict]:
"""
Fully automated pipeline:
1. Scrape top posts from multiple subreddits
2. Score every story — skip weak ones
3. Download gameplay footage from YouTube (cached after first run)
4. Generate video for each passing story
5. Optionally upload each video to YouTube Shorts
Returns a list of result dicts with story title, video path, and upload URL.
"""
if subreddits is None:
subreddits = DEFAULT_SUBREDDITS
if cfg is None:
cfg = RenderConfig()
print(f"\n{'='*60}")
print(f" AUTO MODE")
print(f" Subreddits : {', '.join(subreddits)}")
print(f" Target : {count} videos")
print(f" Upload : {'YouTube Shorts' if upload else 'local only'}")
print(f"{'='*60}\n")
# ── Step 1: collect stories from all subreddits
all_stories = []
per_sub = max(1, (count * 3) // len(subreddits)) # over-fetch to allow for rejects
for sub in subreddits:
try:
stories = scrape_subreddit(sub, count=per_sub, sort="top", time_filter="week")
all_stories.extend(stories)
except Exception as e:
print(f" Warning: failed to scrape r/{sub} — {e}")
if not all_stories:
print(" No stories fetched. Check your internet connection.")
return []
# ── Step 2: score and rank all stories
print(f"\n Scoring {len(all_stories)} stories...")
scored = []
for story in all_stories:
story = clean_story_text(story)
result = evaluate_story(story)
if result.recommended:
scored.append((result.score, story))
scored.sort(key=lambda x: x[0], reverse=True)
selected = [s for _, s in scored[:count]]
print(f" {len(selected)}/{len(all_stories)} stories passed quality filter")
if not selected:
print(" No stories met the quality threshold.")
return []
# ── Step 3: pre-fetch one gameplay clip (shared across all videos)
print(f"\n Fetching gameplay footage...")
try:
gp_path = find_gameplay_video(auto_fetch=True)
except Exception as e:
print(f" Gameplay fetch failed: {e}")
return []
# ── Step 4 & 5: generate + optionally upload
results = []
for i, story in enumerate(selected, 1):
print(f"\n[{i}/{len(selected)}] {story.title[:70]}")
try:
video_path = run_pipeline(
story = story,
gameplay_path = str(gp_path),
cfg = cfg,
skip_eval = True, # already evaluated above
hook = True,
)