forked from kamilstanuch/Autocrop-vertical
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathsaasshorts.py
More file actions
1495 lines (1256 loc) · 57.4 KB
/
Copy pathsaasshorts.py
File metadata and controls
1495 lines (1256 loc) · 57.4 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
"""
SaaSShorts: AI-powered UGC video generator for SaaS products.
Generates viral TikTok/Instagram Reels content from a SaaS URL.
Pipeline:
1. Scrape & analyze SaaS website (Gemini)
2. Generate video scripts (hook → problem → solution → CTA)
3. Generate AI actor portrait (Flux Pro via fal.ai)
4. Generate voiceover (ElevenLabs TTS)
5. Generate talking head video (Kling Avatar v2 via fal.ai)
6. Generate b-roll clips (Kling v2.6 via fal.ai)
7. Composite final video with subtitles (FFmpeg)
"""
import os
import re
import json
import time
import subprocess
from ffmpeg_utils import video_encode_args, DELIVERY, mark_ai_generated
import httpx
from urllib.parse import urljoin
from typing import Optional, List, Dict, Callable
from concurrent.futures import ThreadPoolExecutor, as_completed
ELEVENLABS_API_BASE = "https://api.elevenlabs.io/v1"
FAL_QUEUE_BASE = "https://queue.fal.run"
# Default ElevenLabs voices (name → voice_id)
DEFAULT_VOICES = {
"Rachel (Female, calm)": "21m00Tcm4TlvDq8ikWAM",
"Drew (Male, confident)": "29vD33N1CtxCmqQRPOHJ",
"Bella (Female, soft)": "EXAVITQu4vr4xnSDxMaL",
"Antoni (Male, warm)": "ErXwobaYiN019PkySvjV",
"Josh (Male, deep)": "TxGEqnHWrfWFTfGW9XjX",
"Sam (Male, raspy)": "yoZ06aMxZJJ28mfd3POQ",
}
GEMINI_MODEL = os.environ.get("GEMINI_MODEL_SAAS") or os.environ.get("GEMINI_MODEL") or "gemini-3.1-flash-lite"
# ═══════════════════════════════════════════════════════════════════════
# Phase 1: Website Scraping, Web Research & Analysis
# ═══════════════════════════════════════════════════════════════════════
def research_saas_online(url: str, gemini_key: str) -> dict:
"""
Use Gemini with Google Search grounding to deeply research a SaaS product
across the internet: reviews, Reddit threads, Twitter, competitor comparisons,
pricing complaints, user testimonials, etc.
"""
from google import genai
from google.genai import types
print(f"[SaaSShorts] 🔍 Researching {url} across the web (Google Search grounding)...")
client = genai.Client(api_key=gemini_key)
# Extract domain name for search queries
domain = url.replace("https://", "").replace("http://", "").split("/")[0]
prompt = f"""You are a world-class SaaS market researcher. Research this product thoroughly using Google Search.
Product URL: {url}
Domain: {domain}
SEARCH AND INVESTIGATE:
1. What does this SaaS product do? (search their website, Product Hunt, G2, Capterra)
2. What are REAL user reviews saying? (G2, Capterra, TrustPilot, Reddit, Twitter/X)
3. What are the most common complaints and pain points users mention?
4. Who are their main competitors and how do they compare?
5. What is their pricing and do users think it's worth it?
6. What is their target market and ideal customer profile?
7. Are there any viral posts, memes, or discussions about this product?
8. What content creators or influencers have talked about them?
Return a comprehensive JSON research report:
{{
"product_name": "...",
"website_url": "{url}",
"what_it_does": "Detailed description of the product based on web research",
"target_market": "Who this product is for",
"pricing_info": "Pricing details found online (plans, costs, free tier)",
"user_sentiment": "overall positive/mixed/negative",
"real_reviews": [
{{"source": "G2/Reddit/Twitter/etc", "quote": "actual user quote or paraphrase", "sentiment": "positive/negative/neutral"}},
...
],
"common_complaints": ["complaint 1 from real users", "complaint 2", ...],
"common_praise": ["what users love 1", "what users love 2", ...],
"competitors": [
{{"name": "competitor", "comparison": "how they compare"}}
],
"viral_potential": ["angle 1 based on real discussions", "angle 2", ...],
"key_differentiators": ["what makes them unique based on research"],
"content_angles_from_web": ["angles found from existing content about this product"],
"sources_found": ["list of URLs where information was found"]
}}
Be thorough. Use REAL data from your search results, not made-up information."""
response = client.models.generate_content(
model=GEMINI_MODEL,
contents=[prompt],
config=types.GenerateContentConfig(
tools=[types.Tool(google_search=types.GoogleSearch())],
),
)
# Extract grounding sources
sources = []
try:
metadata = response.candidates[0].grounding_metadata
if metadata and metadata.grounding_chunks:
for chunk in metadata.grounding_chunks:
if chunk.web:
sources.append({"title": chunk.web.title, "url": chunk.web.uri})
if metadata and metadata.web_search_queries:
print(f"[SaaSShorts] Searches performed: {metadata.web_search_queries}")
except Exception:
pass
# Parse response text as JSON
raw = response.text
if not raw:
print("[SaaSShorts] ⚠️ Gemini returned empty response for web research")
return {"raw_research": "", "product_name": domain, "grounding_sources": sources}
text = raw.strip()
if text.startswith("```"):
text = re.sub(r"^```(?:json)?\n?", "", text)
text = re.sub(r"\n?```$", "", text)
start = text.find("{")
end = text.rfind("}")
if start != -1 and end != -1:
text = text[start : end + 1]
try:
research = json.loads(text)
except json.JSONDecodeError:
research = {"raw_research": text, "product_name": domain}
research["grounding_sources"] = sources
print(f"[SaaSShorts] ✅ Web research complete: {len(sources)} sources found")
return research
def scrape_website(url: str) -> dict:
"""Scrape a SaaS website to extract key content for analysis."""
from bs4 import BeautifulSoup
from security_utils import assert_public_url
# SSRF guard: reject non-http(s) / private / metadata hosts, and re-validate
# every redirect hop so a public URL can't 30x-bounce us to an internal host.
current = assert_public_url(url)
print(f"[SaaSShorts] 🌐 Scraping {url}...")
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
with httpx.Client(timeout=30.0, follow_redirects=False) as client:
response = None
for _ in range(5):
response = client.get(current, headers=headers)
if response.has_redirect_location:
current = assert_public_url(str(response.next_request.url))
continue
break
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
# Remove non-content elements
for tag in soup(["script", "style", "nav", "footer", "header", "noscript", "svg", "iframe"]):
tag.decompose()
# Extract metadata
meta_desc = ""
meta_tag = soup.find("meta", attrs={"name": "description"})
if meta_tag:
meta_desc = meta_tag.get("content", "")
og_desc = ""
og_tag = soup.find("meta", attrs={"property": "og:description"})
if og_tag:
og_desc = og_tag.get("content", "")
title = soup.title.string.strip() if soup.title and soup.title.string else ""
# Extract headings
headings = []
for h in soup.find_all(["h1", "h2", "h3"]):
text = h.get_text(strip=True)
if text and len(text) < 200:
headings.append(text)
# Main text content
text = soup.get_text(separator="\n", strip=True)
text = re.sub(r"\n{3,}", "\n\n", text)
text = text[:10000]
# Find subpages to scrape
base_host = httpx.URL(url).host
subpages = set()
for a in soup.find_all("a", href=True):
href = a["href"].lower()
if any(kw in href for kw in ["pricing", "features", "about", "product", "why", "how-it-works", "use-case"]):
try:
full_url = urljoin(url, a["href"])
full_host = httpx.URL(full_url).host
if base_host and full_host and base_host == full_host:
subpages.add(full_url)
except Exception:
pass
# Scrape subpages (max 3)
additional = ""
for sub_url in list(subpages)[:3]:
try:
print(f"[SaaSShorts] → Subpage: {sub_url}")
# Same SSRF guard as the main page: no auto-redirects and re-validate
# every hop, so a same-host page can't 30x-bounce us onto an internal
# host (e.g. 169.254.169.254 cloud metadata).
sub_current = assert_public_url(sub_url)
with httpx.Client(timeout=20.0, follow_redirects=False) as client:
resp = None
for _ in range(5):
resp = client.get(sub_current, headers=headers)
if resp.has_redirect_location:
sub_current = assert_public_url(str(resp.next_request.url))
continue
break
if resp is not None and resp.status_code == 200:
sub_soup = BeautifulSoup(resp.text, "html.parser")
for tag in sub_soup(["script", "style", "nav", "footer", "header", "noscript"]):
tag.decompose()
sub_text = sub_soup.get_text(separator="\n", strip=True)[:5000]
additional += f"\n\n--- {sub_url} ---\n{sub_text}"
except Exception as e:
print(f"[SaaSShorts] ⚠️ Failed: {e}")
result = {
"url": url,
"title": title,
"meta_description": meta_desc or og_desc,
"headings": headings[:20],
"main_content": text,
"additional_pages": additional[:15000],
"pages_scraped": 1 + min(len(subpages), 3),
}
print(f"[SaaSShorts] ✅ Scraped {result['pages_scraped']} pages, {len(text)} chars")
return result
def analyze_saas(scraped_data: dict, gemini_key: str, web_research: dict = None) -> dict:
"""
Deep analysis of a SaaS product combining website scraping + web research.
Uses Gemini 3 Flash for synthesis.
"""
from google import genai
from google.genai import types
print(f"[SaaSShorts] 🧠 Analyzing {scraped_data['url']} (with web research)...")
client = genai.Client(api_key=gemini_key)
# Build web research context
research_context = ""
if web_research:
research_context = f"""
=== WEB RESEARCH (from Google Search) ===
Product: {web_research.get('product_name', 'Unknown')}
What it does: {web_research.get('what_it_does', 'N/A')}
Target market: {web_research.get('target_market', 'N/A')}
Pricing: {web_research.get('pricing_info', 'N/A')}
User sentiment: {web_research.get('user_sentiment', 'N/A')}
Real user reviews:
{json.dumps(web_research.get('real_reviews', [])[:8], indent=2)}
Common complaints from real users:
{json.dumps(web_research.get('common_complaints', []), indent=2)}
What users love:
{json.dumps(web_research.get('common_praise', []), indent=2)}
Competitors:
{json.dumps(web_research.get('competitors', []), indent=2)}
Viral angles from existing content:
{json.dumps(web_research.get('viral_potential', []), indent=2)}
Key differentiators:
{json.dumps(web_research.get('key_differentiators', []), indent=2)}
Content angles found online:
{json.dumps(web_research.get('content_angles_from_web', []), indent=2)}
"""
prompt = f"""You are an expert SaaS marketing analyst and UGC content strategist. Analyze this SaaS product for creating viral UGC-style marketing videos.
You have TWO sources of information:
1. The product's OWN WEBSITE (scraped content)
2. EXTERNAL WEB RESEARCH (real reviews, Reddit, competitor analysis, user sentiment from Google Search)
Combine BOTH to create the most accurate and compelling analysis possible. Prioritize REAL user pain points and sentiments from the web research.
Website: {scraped_data['url']}
Title: {scraped_data['title']}
Meta: {scraped_data['meta_description']}
Headings: {json.dumps(scraped_data['headings'][:15])}
=== WEBSITE CONTENT ===
{scraped_data['main_content'][:6000]}
=== ADDITIONAL PAGES ===
{scraped_data['additional_pages'][:8000]}
{research_context}
Return a JSON object:
{{
"product_name": "Name of the SaaS",
"one_liner": "One-sentence description",
"target_audience": ["audience 1", "audience 2", "audience 3"],
"pain_points": [
{{"pain": "specific pain point (from real user feedback if available)", "intensity": "high/medium/low", "emotional_trigger": "frustration/fear/time-waste/money-loss/overwhelm", "source": "website/user-reviews/reddit/general"}}
],
"key_features": ["feature 1", "feature 2", "feature 3"],
"unique_selling_points": ["usp 1", "usp 2"],
"competitors": [
{{"name": "competitor", "comparison": "how they compare"}}
],
"pricing_model": "freemium/subscription/one-time/usage-based",
"pricing_details": "specific pricing info if found",
"industry": "category",
"user_sentiment_summary": "what real users think overall",
"emotional_hooks": [
"Stop wasting X hours on...",
"Your competitors are already using...",
"I wish I knew about this sooner..."
],
"transformation_story": "Before (with real pain) → After (with product) narrative",
"viral_angles": [
{{"angle": "description", "platform": "tiktok/instagram/both", "style": "ugc/educational/shock/story", "why_viral": "reason this angle works"}}
]
}}
IMPORTANT: Use REAL pain points from user reviews when available. Real frustrations make the best UGC content.
Include 5-8 pain points, 4-6 emotional hooks, and 4+ viral angles."""
response = client.models.generate_content(
model=GEMINI_MODEL,
contents=[prompt],
config=types.GenerateContentConfig(response_mime_type="application/json"),
)
raw = response.text
if not raw:
raise Exception("Gemini returned empty response for SaaS analysis")
text = raw.strip()
if text.startswith("```"):
text = re.sub(r"^```(?:json)?\n?", "", text)
text = re.sub(r"\n?```$", "", text)
start = text.find("{")
end = text.rfind("}")
if start != -1 and end != -1:
text = text[start : end + 1]
try:
analysis = json.loads(text)
except json.JSONDecodeError as e:
raise Exception(f"Failed to parse analysis JSON: {e}\nRaw: {text[:500]}")
# Attach web research sources for reference
if web_research and web_research.get("grounding_sources"):
analysis["_web_sources"] = web_research["grounding_sources"]
print(f"[SaaSShorts] ✅ Analysis: {analysis.get('product_name', '?')} ({len(analysis.get('pain_points', []))} pain points)")
return analysis
def generate_scripts(
analysis: dict,
gemini_key: str,
num_scripts: int = 3,
style: str = "ugc",
language: str = "en",
actor_gender: str = "female",
) -> list:
"""Generate video scripts based on SaaS analysis."""
from google import genai
from google.genai import types
lang_name = "Spanish" if language == "es" else "English"
print(f"[SaaSShorts] 📝 Generating {num_scripts} scripts ({style}, {lang_name})...")
client = genai.Client(api_key=gemini_key)
style_guide = {
"ugc": "Natural, authentic UGC style. Person talking to camera like sharing a discovery with a friend. Casual, genuine.",
"educational": "Educational style. Clear explanations.",
"shock": "Shock/discovery style. Surprising opener.",
"story": "Storytelling style. Mini narrative.",
"comparison": "Before/after comparison.",
}
lang_instructions = ""
if language == "es":
lang_instructions = """
LANGUAGE: ALL narrations, subtitles, captions, and hashtags MUST be in SPANISH (Spain/Latin America).
Use natural casual Spanish like a real person would speak on TikTok. Contractions, slang OK.
Examples of Spanish UGC hooks: "Tío, no me puedo creer que nadie me haya dicho esto antes...", "Si usas Excel para esto, necesitas ver esto YA", "Os voy a enseñar algo que me ha cambiado la vida..."
"""
else:
lang_instructions = """
LANGUAGE: ALL narrations, subtitles, captions, and hashtags MUST be in ENGLISH.
Use natural casual American English like a real person on TikTok. Contractions, slang OK.
Examples of English UGC hooks: "Okay so I just found this tool and...", "Stop doing this manually, there's a better way", "I can't believe nobody told me about this sooner..."
"""
prompt = f"""You are a viral short-form video scriptwriter for TikTok/Instagram Reels.
Generate {num_scripts} video scripts to promote this product/business.
{lang_instructions}
PRODUCT ANALYSIS:
{json.dumps(analysis, indent=2)}
STYLE: {style_guide.get(style, style_guide['ugc'])}
Each script MUST be 20-25 seconds total. NEVER longer than 25 seconds.
YOU MUST USE EXACTLY THIS 5-SEGMENT STRUCTURE. NO EXCEPTIONS:
1. HOOK (0-5s): type="hook", visual="actor_talking", broll_prompt=null — Avatar says a punchy hook.
2. B-ROLL 1 (5-9s): type="problem", visual="broll", broll_prompt="..." (REQUIRED) — Visual of the problem.
3. BODY (9-16s): type="solution", visual="actor_talking", broll_prompt=null — Avatar presents the solution.
4. B-ROLL 2 (16-21s): type="demo", visual="broll", broll_prompt="..." (REQUIRED) — Visual of the product.
5. CTA (21-25s): type="cta", visual="actor_talking", broll_prompt=null — Avatar says CTA with link in bio.
CRITICAL — READ CAREFULLY:
- EXACTLY 5 segments. Not 3, not 4, not 6. FIVE.
- Segments 2 and 4 MUST have visual="broll" and a non-null broll_prompt string.
- Segments 1, 3, 5 MUST have visual="actor_talking" and broll_prompt=null.
- duration_seconds MUST be between 20 and 25.
- full_narration = all narration text joined together.
Return a JSON array:
[
{{
"title": "Short internal title",
"style": "{style}",
"duration_seconds": 23,
"target_platform": "tiktok",
"hook_text": "Hook overlay text (2-5 words max)",
"segments": [
{{
"type": "hook",
"start": 0,
"end": 5,
"narration": "Punchy hook the actor says",
"visual": "actor_talking",
"broll_prompt": null,
"emotion": "excited",
"subtitle_text": "Hook phrase"
}},
{{
"type": "problem",
"start": 5,
"end": 9,
"narration": "Voiceover describing the pain point",
"visual": "broll",
"broll_prompt": "REQUIRED: visual of the problem, e.g. person frustrated at laptop, cluttered spreadsheet on screen",
"emotion": "frustrated",
"subtitle_text": "Pain phrase"
}},
{{
"type": "solution",
"start": 9,
"end": 16,
"narration": "Actor introduces the product naturally",
"visual": "actor_talking",
"broll_prompt": null,
"emotion": "confident",
"subtitle_text": "Solution phrase"
}},
{{
"type": "demo",
"start": 16,
"end": 21,
"narration": "Voiceover showing the product in action",
"visual": "broll",
"broll_prompt": "REQUIRED: visual of the product/result, e.g. clean dashboard with metrics, modern app interface",
"emotion": "excited",
"subtitle_text": "Result phrase"
}},
{{
"type": "cta",
"start": 21,
"end": 23,
"narration": "Short CTA mentioning link in bio",
"visual": "actor_talking",
"broll_prompt": null,
"emotion": "confident",
"subtitle_text": "Link in bio"
}}
],
"full_narration": "All narration text joined (only actor_talking segments)",
"actor_description": "Specific person description: age, gender, ethnicity, hair style, clothing. Casual everyday look.",
"hashtags": ["#saas", "#productivity", "#techtools"],
"caption": "Suggested Instagram/TikTok caption"
}}
]
RULES:
- EXACTLY 5 segments in order: actor, broll, actor, broll, actor
- EXACTLY 2 broll segments with detailed broll_prompt (NOT null)
- full_narration = ALL narration text (both actor and broll voiceover segments joined)
- Total duration MUST be 18-22 seconds, never more
- Keep narrations punchy, conversational, with contractions
- Actor descriptions: casual, real-person look (NOT model/influencer)
- B-roll prompts: cinematic, specific, detailed visual descriptions
- Each script should use a different pain point / angle
- Vary actor demographics across scripts
- CTA MUST always mention "link in bio" / "enlace en la bio". Examples: "Link in bio, go try it", "Check the link in my bio", "El enlace está en la bio, probadlo"
- Write ALL text in {lang_name}
- Actor gender: {actor_gender}. ALL actor_description fields MUST describe a {actor_gender} person. Use diverse ages/ethnicities across scripts.
- IMPORTANT: actor_description MUST ALWAYS be in ENGLISH regardless of script language. Only describe physical appearance: age, gender, ethnicity, hair, clothing. NO actions, NO background, NO scene description.
- Actors must look European, attractive but natural, slightly nerdy/tech vibe. Vary across: blonde, brunette, redhead. Ages 22-35.
- If female: casual summer look (tank top, camisole, simple tee). If male: casual tee or hoodie.
- Example female: "a 26 year old attractive european woman, light brown wavy hair, wearing a white tank top, natural minimal makeup, friendly face"
- Example male: "a 29 year old european man, short dark hair, light stubble, wearing a navy t-shirt, smart casual look" """
response = client.models.generate_content(
model=GEMINI_MODEL,
contents=[prompt],
config=types.GenerateContentConfig(
response_mime_type="application/json",
max_output_tokens=8192,
),
)
raw = response.text
if not raw:
raise Exception("Gemini returned empty response for script generation")
text = raw.strip()
if text.startswith("```"):
text = re.sub(r"^```(?:json)?\n?", "", text)
text = re.sub(r"\n?```$", "", text)
start = text.find("[")
end = text.rfind("]")
if start != -1 and end != -1:
text = text[start : end + 1]
try:
scripts = json.loads(text)
except json.JSONDecodeError as e:
raise Exception(f"Failed to parse scripts JSON: {e}\nRaw: {text[:500]}")
print(f"[SaaSShorts] ✅ Generated {len(scripts)} scripts")
return scripts
# ═══════════════════════════════════════════════════════════════════════
# Phase 2: Asset Generation
# ═══════════════════════════════════════════════════════════════════════
def _fal_run(model_id: str, input_data: dict, fal_key: str, timeout: int = 600) -> dict:
"""
Submit a job to fal.ai queue, poll for completion, return result.
Uses the URLs returned by the submit response (as per fal.ai docs).
"""
headers = {
"Authorization": f"Key {fal_key}",
"Content-Type": "application/json",
}
# ── Step 1: Submit to queue ──
submit_url = f"{FAL_QUEUE_BASE}/{model_id}"
print(f"[fal.ai] Submitting to {submit_url}...")
with httpx.Client(timeout=120.0) as client:
resp = client.post(submit_url, headers=headers, json=input_data)
if resp.status_code >= 400:
print(f"[fal.ai] Submit error: {resp.text[:500]}")
raise Exception(f"fal.ai error ({resp.status_code}): {resp.text[:300]}")
try:
submit_data = resp.json()
except json.JSONDecodeError:
raise Exception(f"fal.ai invalid JSON: {resp.text[:300]}")
request_id = submit_data.get("request_id")
if not request_id:
# Synchronous result (no queue)
return submit_data
# Use the URLs from the submit response (guaranteed correct per docs)
status_url = submit_data.get("status_url", f"{FAL_QUEUE_BASE}/{model_id}/requests/{request_id}/status")
response_url = submit_data.get("response_url", f"{FAL_QUEUE_BASE}/{model_id}/requests/{request_id}")
print(f"[fal.ai] Queued: {request_id}")
print(f"[fal.ai] Status URL: {status_url}")
# ── Step 2: Poll for completion ──
poll_headers = {"Authorization": f"Key {fal_key}"}
start = time.time()
while time.time() - start < timeout:
elapsed = int(time.time() - start)
try:
with httpx.Client(timeout=30.0) as client:
poll_resp = client.get(f"{status_url}?logs=1", headers=poll_headers)
status_data = poll_resp.json()
except Exception as e:
print(f"[fal.ai] Poll error (retrying): {e}")
time.sleep(5)
continue
status = status_data.get("status", "UNKNOWN")
if status == "COMPLETED":
print(f"[fal.ai] ✅ Completed in {elapsed}s! Fetching result...")
with httpx.Client(timeout=120.0) as client:
result_resp = client.get(response_url, headers=poll_headers)
return result_resp.json()
elif status in ("FAILED", "CANCELLED"):
error = status_data.get("error", "unknown error")
raise Exception(f"fal.ai job {status}: {error}")
# Log progress
queue_pos = status_data.get("queue_position", "")
pos_info = f" (pos: {queue_pos})" if queue_pos != "" else ""
print(f"[fal.ai] {model_id}: {status}{pos_info} ({elapsed}s)")
time.sleep(5)
raise Exception(f"fal.ai job timed out after {timeout}s for {model_id}")
def _fal_upload_file(file_path: str, fal_key: str) -> str:
"""Upload a local file to fal.ai CDN storage and return public URL."""
headers = {"Authorization": f"Key {fal_key}"}
filename = os.path.basename(file_path)
ext = os.path.splitext(filename)[1].lower()
content_types = {
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".mp3": "audio/mpeg",
".wav": "audio/wav",
".mp4": "video/mp4",
".webp": "image/webp",
}
content_type = content_types.get(ext, "application/octet-stream")
# Initiate upload
with httpx.Client(timeout=30.0) as client:
resp = client.post(
"https://rest.alpha.fal.ai/storage/upload/initiate",
headers={**headers, "Content-Type": "application/json"},
json={"file_name": filename, "content_type": content_type},
)
resp.raise_for_status()
upload_info = resp.json()
upload_url = upload_info["upload_url"]
file_url = upload_info["file_url"]
# Upload file content
with open(file_path, "rb") as f:
file_bytes = f.read()
with httpx.Client(timeout=120.0) as client:
resp = client.put(
upload_url,
content=file_bytes,
headers={"Content-Type": content_type},
)
resp.raise_for_status()
print(f"[fal.ai] Uploaded {filename} → {file_url}")
return file_url
def generate_actor_images(
description: str, fal_key: str, output_dir: str, title_slug: str, num_options: int = 3,
product_description: str = None,
) -> List[str]:
"""Generate multiple hyper-realistic actor portrait options using Flux 2 Pro."""
print(f"[SaaSShorts] 🎨 Generating {num_options} actor image options (Flux 2 Pro)...")
# Clean description: strip scene/actions, keep only physical appearance
clean_desc = description
for remove in ["hablando", "talking", "sentad", "sitting", "desde", "from", "con una", "with a", "detrás", "behind"]:
if remove in clean_desc.lower():
idx = clean_desc.lower().find(remove)
if idx > 10:
clean_desc = clean_desc[:idx].rstrip(" ,.")
import random
img_num = random.randint(1000, 9999)
if product_description:
prompt = f"""IMG_{img_num}.jpg Raw candid selfie of {clean_desc}, casually holding {product_description}, showing it to the camera with a natural smile. Product clearly visible in hand. Casual and real, not an ad. Low quality front camera, soft room lighting. Reddit selfie."""
else:
prompt = f"""IMG_{img_num}.jpg Raw candid selfie of {clean_desc}, sitting at their desk at home, looking at camera with a relaxed natural smile. Headphones around neck, monitor glow behind them. Not posed, casual and real. Low quality front camera, soft room lighting. Reddit selfie."""
print(f"[SaaSShorts] Prompt: {prompt[:120]}...{' (with product)' if product_description else ''}")
paths = []
# Flux 2 Pro — #1 for photorealistic faces
def _gen_one(i):
result = _fal_run(
"fal-ai/flux-2-pro",
{
"prompt": prompt,
"image_size": "portrait_4_3",
"safety_tolerance": 5,
"seed": random.randint(0, 999999),
},
fal_key,
timeout=300,
)
images = result.get("images") or result.get("output", [])
if not images:
raise Exception(f"No images in actor result: {list(result.keys())}")
img_url = images[0]["url"] if isinstance(images[0], dict) else images[0]
img_path = os.path.join(output_dir, f"{title_slug}_actor_option_{i}.png")
with httpx.Client(timeout=60.0) as client:
img_resp = client.get(img_url)
with open(img_path, "wb") as f:
f.write(img_resp.content)
print(f"[SaaSShorts] ✅ Actor option {i+1}: {img_path}")
return img_path
with ThreadPoolExecutor(max_workers=num_options) as executor:
futures = [executor.submit(_gen_one, i) for i in range(num_options)]
for future in as_completed(futures):
paths.append(future.result())
return sorted(paths)
paths = []
for i, img in enumerate(result.get("images", [])):
img_path = os.path.join(output_dir, f"{title_slug}_actor_option_{i}.png")
with httpx.Client(timeout=60.0) as client:
img_resp = client.get(img["url"])
with open(img_path, "wb") as f:
f.write(img_resp.content)
paths.append(img_path)
print(f"[SaaSShorts] ✅ Actor option {i+1}: {img_path}")
return paths
def generate_actor_image(
description: str, fal_key: str, output_path: str
) -> str:
"""Generate a single actor image using Recraft V4."""
output_dir = os.path.dirname(output_path)
title_slug = os.path.basename(output_path).replace("_actor.png", "")
paths = generate_actor_images(description, fal_key, output_dir, title_slug, num_options=1)
if paths:
import shutil
shutil.move(paths[0], output_path)
return output_path
def generate_voiceover(
text: str,
elevenlabs_key: str,
output_path: str,
voice_id: str = "21m00Tcm4TlvDq8ikWAM",
) -> str:
"""Generate voiceover audio using ElevenLabs TTS."""
print(f"[SaaSShorts] 🎙️ Generating voiceover ({len(text)} chars)...")
url = f"{ELEVENLABS_API_BASE}/text-to-speech/{voice_id}"
headers = {
"xi-api-key": elevenlabs_key,
"Content-Type": "application/json",
}
body = {
"text": text,
"model_id": "eleven_multilingual_v2",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.75,
"style": 0.4,
"use_speaker_boost": True,
},
}
with httpx.Client(timeout=120.0) as client:
resp = client.post(url, headers=headers, json=body)
if resp.status_code != 200:
raise Exception(f"ElevenLabs TTS error ({resp.status_code}): {resp.text}")
with open(output_path, "wb") as f:
f.write(resp.content)
print(f"[SaaSShorts] ✅ Voiceover: {output_path}")
return output_path
def get_elevenlabs_voices(elevenlabs_key: str) -> list:
"""Fetch available voices from ElevenLabs."""
url = f"{ELEVENLABS_API_BASE}/voices"
headers = {"xi-api-key": elevenlabs_key}
with httpx.Client(timeout=15.0) as client:
resp = client.get(url, headers=headers)
if resp.status_code != 200:
return []
data = resp.json()
voices = []
for v in data.get("voices", []):
voices.append({
"voice_id": v["voice_id"],
"name": v["name"],
"category": v.get("category", ""),
"labels": v.get("labels", {}),
"preview_url": v.get("preview_url", ""),
})
return voices
# ═══════════════════════════════════════════════════════════════════════
# Phase 3: Video Generation
# ═══════════════════════════════════════════════════════════════════════
def generate_talking_head(
image_path: str,
audio_path: str,
fal_key: str,
output_path: str,
) -> str:
"""Generate talking head video using Kling Avatar v2 Standard on fal.ai."""
print(f"[SaaSShorts] 🗣️ Generating talking head (Kling Avatar v2)...")
# Upload image and audio to fal.ai CDN
image_url = _fal_upload_file(image_path, fal_key)
audio_url = _fal_upload_file(audio_path, fal_key)
result = _fal_run(
"fal-ai/kling-video/ai-avatar/v2/standard",
{
"image_url": image_url,
"audio_url": audio_url,
"prompt": (
"Natural UGC creator talking to camera. Expressive and energetic. "
"Subtle hand gestures to emphasize points. Slight head movements and nods. "
"Occasional leaning forward for emphasis. Relaxed shoulders, casual vibe. "
"Maintain eye contact with camera. Natural blinking and micro-expressions."
),
},
fal_key,
timeout=600,
)
video_url = result["video"]["url"]
# Download video
with httpx.Client(timeout=180.0) as client:
vid_resp = client.get(video_url)
with open(output_path, "wb") as f:
f.write(vid_resp.content)
print(f"[SaaSShorts] ✅ Talking head: {output_path}")
return output_path
def generate_talking_head_lowcost(
image_path: str,
audio_path: str,
fal_key: str,
output_path: str,
) -> str:
"""
Low-cost talking head: Hailuo 2.3 Fast img2video → VEED Lipsync.
~$0.39 vs ~$1.69 for Kling Avatar v2.
"""
print(f"[SaaSShorts] 🗣️ Generating talking head (Low Cost: Hailuo + VEED Lipsync)...")
# Step 1: Generate 6s video from image using MiniMax Hailuo 2.3 Fast ($0.19)
# Cache the Hailuo clip so retries don't re-generate it
hailuo_cache_path = output_path.replace(".mp4", "_hailuo_cache.mp4")
if os.path.exists(hailuo_cache_path) and os.path.getsize(hailuo_cache_path) > 0:
print(f"[SaaSShorts] Hailuo clip cached, skipping generation.")
hailuo_video_url = _fal_upload_file(hailuo_cache_path, fal_key)
else:
image_url = _fal_upload_file(image_path, fal_key)
hailuo_result = _fal_run(
"fal-ai/minimax/hailuo-2.3-fast/standard/image-to-video",
{
"image_url": image_url,
"prompt": (
"Person talking to camera, subtle head nods and natural micro-expressions. "
"Gentle head movement, slight shoulder sway. Eye contact with camera. "
"Natural blinking. Soft ambient lighting. Smooth cinematic motion."
),
},
fal_key,
timeout=300,
)
print(f"[SaaSShorts] Hailuo response keys: {list(hailuo_result.keys())}")
if "video" in hailuo_result:
hailuo_video_url = hailuo_result["video"]["url"] if isinstance(hailuo_result["video"], dict) else hailuo_result["video"]
elif "video_url" in hailuo_result:
hailuo_video_url = hailuo_result["video_url"]
elif "output" in hailuo_result:
hailuo_video_url = hailuo_result["output"]["url"] if isinstance(hailuo_result["output"], dict) else hailuo_result["output"]
else:
raise Exception(f"No video in Hailuo result: {hailuo_result}")
# Save Hailuo clip locally for retry cache
with httpx.Client(timeout=180.0) as client:
vid_resp = client.get(hailuo_video_url)
with open(hailuo_cache_path, "wb") as f:
f.write(vid_resp.content)
print(f"[SaaSShorts] Hailuo 2.3 Fast 6s clip ready (cached for retry).")
# Step 2: Upload audio for lip-sync
audio_url = _fal_upload_file(audio_path, fal_key)
# Step 3: VEED Lipsync — high quality lip-sync with loop ($0.20 for 30s)
lipsync_result = _fal_run(
"veed/lipsync",
{
"video_url": hailuo_video_url,
"audio_url": audio_url,
},
fal_key,
timeout=900,
)
print(f"[SaaSShorts] VEED Lipsync response keys: {list(lipsync_result.keys())}")
if "video" in lipsync_result:
lipsync_video_url = lipsync_result["video"]["url"] if isinstance(lipsync_result["video"], dict) else lipsync_result["video"]
else:
raise Exception(f"No video in VEED Lipsync result: {lipsync_result}")
with httpx.Client(timeout=180.0) as client:
vid_resp = client.get(lipsync_video_url)
with open(output_path, "wb") as f:
f.write(vid_resp.content)
print(f"[SaaSShorts] ✅ Talking head (low cost): {output_path}")
return output_path
def generate_broll(
prompt: str, fal_key: str, output_path: str, duration: str = "5"
) -> str:
"""
Generate b-roll: Recraft V4 image + Ken Burns zoom effect via FFmpeg.
"""
print(f"[SaaSShorts] 🎬 Generating b-roll image + Ken Burns effect...")
dur_secs = int(duration)
img_path = output_path.replace(".mp4", "_img.png")
# Step 1: Generate a high-quality still image with Flux 2 Pro
result = _fal_run(
"fal-ai/flux-2-pro",
{
"prompt": f"{prompt}. Cinematic, shallow depth of field, professional photography.",
"image_size": "portrait_4_3",
"safety_tolerance": 5,
},
fal_key,
timeout=300,
)
# Flux 2 Pro returns images in "images" or "output" key
images = result.get("images") or result.get("output", [])
if not images:
raise Exception(f"No images in b-roll result: {list(result.keys())}")
img_url = images[0]["url"] if isinstance(images[0], dict) else images[0]
with httpx.Client(timeout=60.0) as client:
img_resp = client.get(img_url)