feat(phase-4): UI/UX improvements for Podcast Maker Write phase

Frontend Changes:
- Add scene numbering badge (1/N) next to scene titles
- Add inline status chips (Complete, Audio, Image, Voice, Why Script)
- Professional AI-like gradient styling for all chips with shadows
- Remove Script Editor header and 'Why This Script Format?' collapsible
- Move Voice and Why Script info to per-scene chips
- Make scene section mobile-responsive (responsive layout, button sizing)
- Rename 'B-Roll Charts' to 'Podcast Charts' with accordion (collapsed by default)
- Add sceneIndex prop to SceneEditor for scene numbering
- Enhanced accessibility with keyboard navigation and focus states

Backend Changes:
- Audio handler improvements
- B-roll handler enhancements
- Script handler updates
- B-roll composer and service improvements
- Removed temporary broll_temp files

Technical:
- Full mobile responsiveness for scene cards
- Gradient chip styling: vibrant colors with white text and shadows
- Non-breaking approval/generation flow preserved
- TypeScript compatibility maintained
This commit is contained in:
ajaysi
2026-04-24 15:44:09 +05:30
parent 8b79099b15
commit ba94ee30bc
16 changed files with 977 additions and 2126 deletions

View File

@@ -1,666 +0,0 @@
# Programmatic B-Roll Composer
A layered video composition pipeline that assembles AI-generated images, programmatic data charts, Pillow text overlays, and circular-masked avatar videos into a single output MP4. Driven by structured JSON from an LLM, exposed via a FastAPI server.
---
## Table of Contents
1. [Architecture overview](#1-architecture-overview)
2. [File structure](#2-file-structure)
3. [Installation](#3-installation)
4. [Core concepts](#4-core-concepts)
- 4.1 [The Insight dataclass](#41-the-insight-dataclass)
- 4.2 [The SceneAssets dataclass](#42-the-sceneassets-dataclass)
- 4.3 [The layer stack](#43-the-layer-stack)
- 4.4 [The JSON bridge](#44-the-json-bridge)
5. [Asset generators](#5-asset-generators)
- 5.1 [Bar chart — make_bar_chart](#51-bar-chart--make_bar_chart)
- 5.2 [Line trend — make_line_trend](#52-line-trend--make_line_trend)
- 5.3 [Bullet overlay — make_bullet_overlay](#53-bullet-overlay--make_bullet_overlay)
- 5.4 [Insight card — make_insight_card](#54-insight-card--make_insight_card)
6. [Video effects](#6-video-effects)
- 6.1 [Circular avatar mask — apply_circle_mask](#61-circular-avatar-mask--apply_circle_mask)
- 6.2 [Ken Burns zoom — ken_burns](#62-ken-burns-zoom--ken_burns)
7. [Scene builders](#7-scene-builders)
- 7.1 [Data scene — build_data_scene](#71-data-scene--build_data_scene)
- 7.2 [Bullet scene — build_bullet_scene](#72-bullet-scene--build_bullet_scene)
- 7.3 [Full avatar scene — build_full_avatar_scene](#73-full-avatar-scene--build_full_avatar_scene)
8. [Scene dispatcher — dispatch_scene](#8-scene-dispatcher--dispatch_scene)
9. [Crossfade transitions](#9-crossfade-transitions)
- 9.1 [How crossfade_concat works](#91-how-crossfade_concat-works)
- 9.2 [The set_duration gotcha](#92-the-set_duration-gotcha)
10. [Master compositor — compose_video](#10-master-compositor--compose_video)
11. [FastAPI server](#11-fastapi-server)
- 11.1 [Request models](#111-request-models)
- 11.2 [Job lifecycle](#112-job-lifecycle)
- 11.3 [API endpoints](#113-api-endpoints)
12. [Running the project](#12-running-the-project)
- 12.1 [Smoke test (no media files needed)](#121-smoke-test-no-media-files-needed)
- 12.2 [Full video composition](#122-full-video-composition)
- 12.3 [API server](#123-api-server)
13. [Calling the API](#13-calling-the-api)
14. [Production notes](#14-production-notes)
15. [Extending the pipeline](#15-extending-the-pipeline)
---
## 1. Architecture overview
The pipeline follows a **Layered Composition** model. Rather than generating video in one pass, it assembles independent visual layers — each produced by the cheapest appropriate tool — into a single timeline using MoviePy as the compositor.
```
LLM JSON output
dispatch_scene() ← routes visual_cue → builder function
├─ build_data_scene()
│ ├─ ImageClip (background) ← AI-generated image
│ ├─ ImageClip (chart PNG) ← Matplotlib, transparent bg
│ ├─ ImageClip (insight card) ← Pillow RGBA
│ └─ VideoFileClip (avatar) ← circular numpy mask
├─ build_bullet_scene()
│ ├─ ImageClip (background)
│ ├─ ImageClip (bullet overlay) ← Pillow RGBA
│ └─ VideoFileClip (avatar)
└─ build_full_avatar_scene()
└─ VideoFileClip (full-screen)
crossfade_concat() ← dissolve between scenes
write_videofile() ← H.264 MP4 via ffmpeg
```
The key design decision: charts and text are **never** rendered by a generative model. Matplotlib produces pixel-perfect data graphics from real numbers; Pillow renders crisp, deterministic text. Only the background and the talking-head avatar come from AI generation, minimising both cost and hallucination risk.
---
## 2. File structure
```
.
├── broll_composer.py # Core library — all composition logic
├── api_server.py # FastAPI wrapper — HTTP interface to the pipeline
└── requirements.txt # Python dependencies
```
`broll_composer.py` has no FastAPI dependency and can be imported and called directly from scripts, notebooks, or other web frameworks.
---
## 3. Installation
```bash
# System dependency — must be on PATH
apt-get install ffmpeg
# Python packages
pip install -r requirements.txt
```
**requirements.txt**
```
moviepy==1.0.3
Pillow>=10.0
matplotlib>=3.8
numpy>=1.26
fastapi>=0.111
uvicorn[standard]>=0.29
python-multipart>=0.0.9
```
MoviePy 1.0.3 is pinned because 2.x introduced breaking API changes to `CompositeVideoClip` and the effects interface. The rest can float within the specified lower bounds.
---
## 4. Core concepts
### 4.1 The Insight dataclass
Every scene is driven by a single `Insight` object. This is the contract between the LLM and the composition pipeline:
```python
@dataclass
class Insight:
key_insight: str # Headline text rendered on the insight card
supporting_stat: str # Sub-headline rendered below the headline
visual_cue: str # Selects which scene builder to use (see §8)
audio_tone: str # Passed through for downstream TTS / audio selection
chart_data: dict # Data payload consumed by chart generators (see §5)
duration: float # Scene length in seconds, default 10.0
```
The `audio_tone` field is not used by the video pipeline itself — it is metadata for whatever system generates or selects the voiceover audio track for the scene.
### 4.2 The SceneAssets dataclass
`SceneAssets` carries file paths to the media assets for a given scene:
```python
@dataclass
class SceneAssets:
background_img: str # Required — path to JPEG or PNG background
chart_img: Optional[str] # Populated by dispatch_scene after chart generation
avatar_video: Optional[str] # Optional — path to MP4 avatar clip
bullet_img: Optional[str] # Reserved for pre-rendered bullet overlays
```
`chart_img` starts as `None` and is written to by `dispatch_scene` after it generates the Matplotlib PNG, so the scene builders receive a fully-populated `SceneAssets` by the time they run.
### 4.3 The layer stack
Every scene is a `CompositeVideoClip` — a MoviePy object that renders multiple clips on a shared canvas by alpha-compositing them bottom-to-top. The layer order is consistent across all scene types:
| Z-order | Layer | Source | Notes |
|---------|-------|--------|-------|
| 0 (bottom) | Background | AI image + Ken Burns | Darkened to make overlays legible |
| 1 | Chart or bullet overlay | Matplotlib or Pillow PNG | Transparent background; fades in |
| 2 | Insight card | Pillow RGBA | Positioned at y=820 (near bottom) |
| 3 (top) | Avatar circle | MP4 + numpy mask | Bottom-right corner |
### 4.4 The JSON bridge
The LLM is prompted to return a structured JSON object — not prose — so the pipeline can consume it without parsing ambiguity:
```json
{
"key_insight": "AI tools reduced content cycles by 40%",
"supporting_stat": "HubSpot 2026 report — 12% lift in CTR",
"visual_cue": "bar_chart_comparison",
"audio_tone": "authoritative_and_surprising",
"duration": 10.0,
"chart_data": {
"labels": ["Content Velocity", "CTR", "Engagement", "Cost/Lead"],
"before": [30, 22, 18, 60],
"after": [72, 34, 41, 38]
}
}
```
`pipeline_from_json()` is the single-call entry point that accepts this JSON string, constructs the dataclasses, runs `dispatch_scene`, and writes the output MP4.
---
## 5. Asset generators
These functions produce static image files (PNG with alpha transparency) that are loaded as `ImageClip` objects in the scene builders. They are completely independent of MoviePy and can be called and previewed without assembling any video.
### 5.1 Bar chart — `make_bar_chart`
```python
make_bar_chart(data: dict, out_path: str, title: str = "") -> str
```
Produces a side-by-side "before vs after" bar chart using Matplotlib. The critical detail is the renderer configuration and save parameters:
```python
matplotlib.use("Agg") # Non-interactive backend — no display required
fig, ax = plt.subplots(figsize=(8, 4.5), facecolor="none")
ax.set_facecolor("none") # Transparent axes background
fig.savefig(out_path, dpi=150, transparent=True, bbox_inches="tight")
```
Setting both `facecolor="none"` on the figure and `transparent=True` on `savefig` is necessary because they control different things: the figure background and the PNG alpha channel respectively. Without both, a white box appears behind the chart when it is composited over the video background.
**Expected `data` shape:**
```python
{
"labels": ["Category A", "Category B"], # X-axis labels
"before": [30, 22], # Bar heights (left bars)
"after": [72, 34] # Bar heights (right bars)
}
```
### 5.2 Line trend — `make_line_trend`
```python
make_line_trend(data: dict, out_path: str, title: str = "") -> str
```
Produces a time-series line chart with a translucent fill under the curve (`alpha=0.12`). Suited for growth trends, adoption curves, and any metric tracked over sequential time periods.
**Expected `data` shape:**
```python
{
"x": [2021, 2022, 2023, 2024, 2025], # X-axis values (numeric or strings)
"y": [10, 18, 30, 45, 72] # Y-axis values
}
```
### 5.3 Bullet overlay — `make_bullet_overlay`
```python
make_bullet_overlay(lines: list[str], out_path: str,
width: int = 900, font_size: int = 32) -> str
```
Renders a list of bullet-point strings onto a semi-transparent dark rounded rectangle using Pillow. The image height is computed dynamically from the number of lines:
```python
img_h = padding * 2 + len(lines) * line_h + 12
```
The fill colour `(10, 10, 10, 185)` gives roughly 73% opacity — dark enough for text legibility over any background, light enough that the background remains visible. The bullet character (`•`) is prepended in Python rather than in the font, so no special Unicode font support is required.
Font loading tries the DejaVu Sans Bold path common on Debian/Ubuntu systems, falling back to Pillow's built-in bitmap font if the TTF is absent.
### 5.4 Insight card — `make_insight_card`
```python
make_insight_card(insight: str, stat: str, out_path: str,
width: int = 960, height: int = 200) -> str
```
Renders a two-line card: a large bold headline (`font_size=34`) and a smaller supporting stat line (`font_size=20`). A solid red rectangle (`#E63946`) is drawn as a left-edge accent bar — a visual device borrowed from print editorial design that gives the card a distinct identity when overlaid on varied backgrounds.
The card uses `fill=(10, 10, 10, 200)` — approximately 78% opacity — slightly more opaque than the bullet overlay because the headline text is denser.
---
## 6. Video effects
### 6.1 Circular avatar mask — `apply_circle_mask`
```python
apply_circle_mask(clip: VideoFileClip, diameter: int) -> VideoFileClip
```
Takes an MP4 avatar clip and returns it with a circular alpha mask applied, so only the circle region is visible when the clip is composited over other layers.
The mask is built using NumPy's `ogrid`, which creates coordinate arrays without materialising a full mesh:
```python
Y, X = np.ogrid[:h, :w]
cx, cy = w / 2, h / 2
mask_arr = ((X - cx)**2 + (Y - cy)**2 <= (min(w, h) / 2)**2).astype(float)
```
This produces a 2D float array (values 0.0 or 1.0) where all pixels within the inscribed circle are 1 (opaque) and all pixels outside are 0 (transparent). MoviePy requires mask arrays in this float format — it does not accept uint8 or boolean arrays directly.
The mask array is wrapped in an `ImageClip` with `ismask=True` and the duration is set to match the source clip before calling `clip.set_mask()`.
**Why not use imagemagick or a pre-made circular PNG?** The numpy approach has no subprocess dependency, works for any input resolution, and the mask is computed once and reused for every frame without disk I/O.
### 6.2 Ken Burns zoom — `ken_burns`
```python
ken_burns(clip: ImageClip, zoom_ratio: float = 0.08) -> ImageClip
```
Applies a slow continuous zoom-in to a static image clip, creating the illusion of camera movement. This prevents the background from looking visually "dead" during the scene.
The implementation uses `clip.fl()`, MoviePy's frame-level transform function, which receives both `get_frame` (a callable that returns the frame array at time `t`) and the current time `t`:
```python
def zoom_frame(get_frame, t):
frame = get_frame(t)
frac = 1 + zoom_ratio * (t / clip.duration) # grows from 1.0 to 1+zoom_ratio
h, w = frame.shape[:2]
new_h, new_w = int(h / frac), int(w / frac) # shrink crop window
y1 = (h - new_h) // 2 # center the crop
x1 = (w - new_w) // 2
cropped = frame[y1:y1 + new_h, x1:x1 + new_w]
return np.array(Image.fromarray(cropped).resize((w, h), Image.LANCZOS))
```
At `t=0`, `frac=1.0` so the crop is the full frame. At `t=duration`, `frac=1+zoom_ratio` so the crop is slightly smaller, and upscaling it back to full resolution creates the zoom effect. `zoom_ratio=0.08` means an 8% zoom over the full duration — perceptible but not distracting.
`apply_to=["mask"]` passes the same transform to the mask channel if one is present, keeping the mask geometrically in sync with the image.
---
## 7. Scene builders
Scene builders assemble the layers for a given `visual_cue` type into a `CompositeVideoClip`. Each builder follows the same pattern: build layers bottom-to-top, append to a list, return `CompositeVideoClip(layers, size=bg.size).set_duration(d)`.
The explicit `.set_duration(d)` on the return value is mandatory — see [§9.2](#92-the-set_duration-gotcha) for why.
### 7.1 Data scene — `build_data_scene`
Used for `visual_cue` values `bar_chart_comparison` and `line_trend`. The most information-dense layout:
- **Background**: full-canvas `ImageClip`, Ken Burns zoom at 8%, brightness reduced by 40 units via `vfx.lum_contrast(0, -40)`.
- **Chart**: resized to 700px wide, centred horizontally, positioned 180px from the top. Fades in over 0.6s starting at `t=0.5` and fades out over 0.4s at the end.
- **Insight card**: centred horizontally at y=820 (approximately the lower fifth of a 1080p frame). Fades in over 0.5s.
- **Avatar**: circular-masked at 240px diameter, positioned 40px from the bottom-right corner (`bg.w - 280, bg.h - 280`).
### 7.2 Bullet scene — `build_bullet_scene`
Used for `visual_cue` value `bullet_points`. A simpler layout suited to lists of supporting facts:
- **Background**: Ken Burns at 5% zoom (slower than the data scene — more contemplative pacing), brightness reduced by 50 units.
- **Bullet overlay**: rendered by `make_bullet_overlay`, centred both horizontally and vertically, fades in over 0.7s.
- **Avatar**: circular-masked at 200px diameter (slightly smaller than in the data scene), positioned 40px from the bottom-right corner.
If `bullet_lines` is not provided by the caller, the builder falls back to using `insight.key_insight` and `insight.supporting_stat` as two bullet points.
### 7.3 Full avatar scene — `build_full_avatar_scene`
Used for `visual_cue` value `full_avatar`. The "Hook" scene — designed to open a piece with a direct-to-camera delivery that grabs attention before the data arrives. No overlays; the avatar fills the entire frame:
```python
avatar = VideoFileClip(assets.avatar_video).subclip(0, d)
return avatar.resize(height=1080).set_duration(d)
```
This is the only scene type that does not use a `CompositeVideoClip` — it returns a `VideoFileClip` directly. The explicit `.set_duration(d)` is still applied (see §9.2).
---
## 8. Scene dispatcher — `dispatch_scene`
```python
dispatch_scene(insight: Insight, assets: SceneAssets,
bullet_lines: Optional[list[str]] = None) -> CompositeVideoClip
```
The dispatcher is the JSON bridge's execution layer. It reads `insight.visual_cue` and routes to the correct builder, generating any intermediate assets (charts) along the way:
```
visual_cue value Action
─────────────────────────────────────────────────────
"full_avatar" → build_full_avatar_scene()
"bar_chart_comparison" → make_bar_chart() → build_data_scene()
"line_trend" → make_line_trend() → build_data_scene()
"bullet_points" → build_bullet_scene()
<anything else> → build_data_scene() with no chart (fallback)
```
Chart PNGs are written to `/tmp/chart.png`. This is intentionally a fixed path — each call overwrites the previous chart, which is fine because `dispatch_scene` is called sequentially per scene. If scenes are ever parallelised, use a `job_id`-prefixed temp path instead.
---
## 9. Crossfade transitions
### 9.1 How `crossfade_concat` works
```python
def crossfade_concat(scenes: list, fade_dur: float = 0.5) -> CompositeVideoClip:
faded = []
for i, clip in enumerate(scenes):
c = clip
if i > 0:
c = c.fx(vfx.crossfadein, fade_dur)
faded.append(c)
return concatenate_videoclips(faded, padding=-fade_dur, method="compose")
```
`vfx.crossfadein` makes a clip's opacity ramp from 0 to 1 over `fade_dur` seconds from its start point. This handles the incoming side of the dissolve.
`padding=-fade_dur` is the critical parameter. By default, `concatenate_videoclips` places each clip immediately after the previous one ends. A negative padding shifts each clip left by `fade_dur` seconds, so it starts while the previous clip is still playing. The overlap window is exactly `fade_dur` seconds, which matches the duration of the `crossfadein` effect — this is what produces a dissolve rather than a hard cut or a gap.
`method="compose"` tells MoviePy to use `CompositeVideoClip` internally for the overlapping portions rather than trying to blend frames at the pixel level, which is how the alpha ramp from `crossfadein` is correctly respected.
The default `fade_dur` of `0.5s` is appropriate for fast-paced content. Increase to `0.81.0s` for a more cinematic feel. The total output duration is `sum(scene.duration for scene in scenes) - (len(scenes) - 1) * fade_dur`.
### 9.2 The `set_duration` gotcha
`CompositeVideoClip` infers its total duration by scanning the durations of all constituent clips. When sub-clips have `set_start` offsets — such as the chart clip which starts at `t=0.5` and has a duration of `d - 1.5`, and the insight card which starts at `t=0.5` with a duration of `d - 1.0` — MoviePy computes the composite's duration as `max(clip.start + clip.duration for clip in layers)`.
In most cases this yields a value slightly larger than `d` due to floating-point arithmetic on the offset calculations, or occasionally slightly smaller if a sub-clip ends fractionally before the background. Either error causes `crossfade_concat`'s `padding=-fade_dur` overlap to be miscalculated, typically producing a black flash frame at each scene boundary.
The fix is to explicitly call `.set_duration(d)` on every scene builder's return value, overriding the inferred value with the authoritative duration from the `Insight`:
```python
return CompositeVideoClip(layers, size=bg.size).set_duration(d)
```
This must be applied to all three builders, including `build_full_avatar_scene`, because a `resize()` call on a `VideoFileClip` creates a new clip object whose duration is re-derived from the source — it does not inherit the `subclip(0, d)` duration reliably on all platforms.
---
## 10. Master compositor — `compose_video`
```python
def compose_video(scenes: list, output_path: str = "output.mp4",
fps: int = 24, fade_dur: float = 0.5) -> str
```
The final assembly step. Calls `crossfade_concat` to produce the dissolved timeline, then writes to an H.264 MP4 via MoviePy's `write_videofile`:
```python
final.write_videofile(
output_path,
fps=fps,
codec="libx264",
audio_codec="aac",
threads=4,
preset="fast",
logger=None,
)
```
`preset="fast"` is a reasonable default for a production pipeline — it is significantly faster than `slow` or `medium` with only a marginal quality difference at typical web streaming bitrates. Change to `slow` for archive-quality output. `logger=None` suppresses the verbose ffmpeg progress output; remove it during debugging.
`threads=4` maps to ffmpeg's `-threads` flag. Increase if the host has more cores available. This affects the encoding step only — MoviePy's frame rendering is single-threaded.
---
## 11. FastAPI server
`api_server.py` wraps the composition pipeline behind an HTTP API, enabling it to be called from any frontend, automation script, or orchestration system.
### 11.1 Request models
**`InsightPayload`** — mirrors the `Insight` dataclass with Pydantic validation:
| Field | Type | Constraints | Description |
|-------|------|-------------|-------------|
| `key_insight` | str | required | Headline text |
| `supporting_stat` | str | required | Sub-headline text |
| `visual_cue` | str | required | Scene template selector |
| `audio_tone` | str | required | Downstream audio metadata |
| `duration` | float | 3.060.0 | Scene length in seconds |
| `chart_data` | dict | optional | Data payload for chart generators |
| `bullet_lines` | list[str] | optional | Explicit bullet text (overrides defaults) |
**`ComposeRequest`** — the top-level request body:
| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `insights` | list[InsightPayload] | required | Ordered list of scenes |
| `fps` | int | 24 | Output frame rate (1260) |
| `fade_dur` | float | 0.5 | Crossfade duration in seconds (0.02.0) |
**`JobStatus`** — the response model for job tracking:
| Field | Values | Description |
|-------|--------|-------------|
| `job_id` | UUID hex string | Unique identifier for polling |
| `status` | `queued`, `processing`, `done`, `error` | Current state |
| `output_url` | `/download/{job_id}` or null | Available when `status == "done"` |
| `error` | string or null | Error message when `status == "error"` |
### 11.2 Job lifecycle
Video composition is CPU-intensive and typically takes 30120 seconds for a multi-scene piece. The API uses FastAPI's `BackgroundTasks` to run composition asynchronously so the HTTP response is immediate:
```
POST /compose
├─ Validates payload, saves uploaded files to /tmp/broll_jobs/{job_id}/
├─ Creates JobStatus(status="queued")
├─ Registers BackgroundTask → _compose_worker()
└─ Returns 202 Accepted with job_id
_compose_worker() (background)
├─ Sets status = "processing"
├─ Runs _sync_compose() in a thread pool (loop.run_in_executor)
│ └─ Iterates insights → dispatch_scene() → compose_video()
├─ On success: status = "done", output_url = "/download/{job_id}"
└─ On error: status = "error", error = str(exc)
GET /status/{job_id} ← poll until status == "done" or "error"
GET /download/{job_id} ← returns MP4 file
```
`loop.run_in_executor(None, _sync_compose)` is important: MoviePy's frame rendering and ffmpeg's encoding are blocking operations. Running them directly in an `async` function would block the entire event loop. `run_in_executor` offloads the work to a thread pool, keeping the server responsive to other requests during composition.
The job store is currently a plain Python dict (`_jobs`). This is appropriate for a single-worker development server. Replace with Redis (using `aioredis` or `redis-py`) for multi-worker or multi-instance deployments.
### 11.3 API endpoints
| Method | Path | Description |
|--------|------|-------------|
| `POST` | `/compose` | Start a composition job (multipart form) |
| `GET` | `/status/{job_id}` | Poll job status |
| `GET` | `/download/{job_id}` | Download finished MP4 |
| `POST` | `/preview/chart` | Generate and return a chart PNG (no video) |
| `GET` | `/health` | Liveness check |
Interactive documentation is available at `http://localhost:8000/docs` once the server is running (FastAPI's built-in Swagger UI).
---
## 12. Running the project
### 12.1 Smoke test (no media files needed)
The smoke test validates all asset generators — chart PNGs, bullet overlays, and insight cards — without requiring any background images or avatar videos:
```bash
python broll_composer.py
```
Expected output:
```
Chart saved → /tmp/demo_chart.png
Bullets saved → /tmp/demo_bullets.png
Insight card saved → /tmp/demo_card.png
Sample Insight JSON: { ... }
All asset generation tests passed.
To run full video composition, supply real background_img and avatar_video paths.
```
Inspect the PNG files in `/tmp/` to visually verify chart rendering before running the full pipeline.
### 12.2 Full video composition
```python
from broll_composer import pipeline_from_json
insight_json = """{
"key_insight": "AI reduced production time by 40%",
"supporting_stat": "HubSpot 2026: 12% CTR lift",
"visual_cue": "bar_chart_comparison",
"audio_tone": "authoritative_and_surprising",
"duration": 10.0,
"chart_data": {
"labels": ["Content Velocity", "CTR", "Engagement", "Cost/Lead"],
"before": [30, 22, 18, 60],
"after": [72, 34, 41, 38]
}
}"""
output_path = pipeline_from_json(
insight_json,
background_img="path/to/background.jpg",
avatar_video="path/to/avatar.mp4", # optional
)
print(f"Video written to {output_path}")
```
### 12.3 API server
```bash
uvicorn api_server:app --host 0.0.0.0 --port 8000
```
For development with auto-reload:
```bash
uvicorn api_server:app --reload
```
---
## 13. Calling the API
The `/compose` endpoint accepts `multipart/form-data` with three parts: `payload` (JSON string), `background` (image file), and optionally `avatar` (video file).
```bash
curl -X POST http://localhost:8000/compose \
-F 'payload={
"insights": [{
"key_insight": "AI reduced production time by 40%",
"supporting_stat": "HubSpot 2026: 12% CTR lift",
"visual_cue": "bar_chart_comparison",
"audio_tone": "authoritative_and_surprising",
"duration": 10.0,
"chart_data": {
"labels": ["Velocity","CTR","Engagement","Cost/Lead"],
"before": [30, 22, 18, 60],
"after": [72, 34, 41, 38]
}
}],
"fps": 24,
"fade_dur": 0.5
}' \
-F 'background=@./bg.jpg' \
-F 'avatar=@./avatar.mp4'
```
This returns a `JobStatus` with a `job_id`. Poll for completion:
```bash
curl http://localhost:8000/status/{job_id}
# → {"job_id": "...", "status": "done", "output_url": "/download/..."}
```
Download the finished video:
```bash
curl -O http://localhost:8000/download/{job_id}
```
Preview a chart without video assembly:
```bash
curl -X POST "http://localhost:8000/preview/chart?title=My+Chart&chart_type=bar_chart_comparison" \
-H "Content-Type: application/json" \
-d '{"labels":["A","B"],"before":[30,22],"after":[72,34]}' \
--output preview.png
```
---
## 14. Production notes
**Concurrency**: FastAPI's `BackgroundTasks` runs in the same process as the web server. Under concurrent load, multiple composition jobs will share the same thread pool, which can cause memory pressure (each MoviePy frame rendering buffers several seconds of uncompressed video). For production, move composition to a dedicated worker queue (Celery + Redis, or ARQ) and have the API server dispatch jobs to it rather than running them in-process.
**Temp file isolation**: Chart PNGs and insight card PNGs are written to fixed paths under `/tmp/`. This is safe for sequential processing but will cause race conditions if jobs are parallelised. Prefix all temp file paths with the `job_id` to isolate them:
```python
chart_path = f"/tmp/{job_id}_chart.png"
```
**Memory**: MoviePy loads entire video clips into memory for compositing. For scenes longer than ~30 seconds with a high-resolution avatar, memory use can reach several GB. If this is a concern, render scenes individually and use ffmpeg's `concat` demuxer to join them in a second pass rather than compositing them all in Python.
**ffmpeg version**: MoviePy 1.0.3 delegates encoding to ffmpeg. Versions prior to 4.x may not support all `preset` values or the `aac` codec without additional flags. The pipeline is tested against ffmpeg 5.x and 6.x.
**File cleanup**: Completed job files accumulate in `/tmp/broll_jobs/`. Add a cleanup background task or cron job that deletes job directories older than a configurable TTL (e.g. 1 hour).
---
## 15. Extending the pipeline
**Adding a new scene template**: add a builder function following the `build_*_scene` naming convention, then add a `visual_cue` string → function mapping in `dispatch_scene`. No other changes are needed.
**Adding a new chart type**: add a `make_*` function that writes a transparent PNG, then handle the new `visual_cue` in `dispatch_scene` by calling it before passing `assets` to a builder.
**Supporting multiple backgrounds per script**: `SceneAssets` currently takes a single `background_img`. To vary the background per scene, add a `background_img` field to `InsightPayload` in the API model and pass it through to `SceneAssets` in the compose worker.
**Audio**: the pipeline produces silent video. Attach a voiceover by loading it as a MoviePy `AudioFileClip`, setting its start time to align with each scene, and passing the composite audio to `final.set_audio()` before calling `write_videofile`.

View File

@@ -1,229 +0,0 @@
"""
FastAPI wrapper for the B-Roll Composer pipeline.
POST /compose → triggers scene assembly, returns video download URL.
"""
from __future__ import annotations
import os
import uuid
import json
import asyncio
from pathlib import Path
from typing import Optional, List
from fastapi import FastAPI, UploadFile, File, Form, BackgroundTasks, HTTPException
from fastapi.responses import FileResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from broll_composer import (
Insight, SceneAssets, dispatch_scene, compose_video,
make_bar_chart, make_line_trend, make_bullet_overlay,
)
# ---------------------------------------------------------------------------
# App setup
# ---------------------------------------------------------------------------
app = FastAPI(
title="B-Roll Composer API",
description="Programmatic video composition: Background + Chart + Avatar Circle",
version="1.0.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
WORK_DIR = Path("/tmp/broll_jobs")
WORK_DIR.mkdir(exist_ok=True)
# ---------------------------------------------------------------------------
# Request / Response models
# ---------------------------------------------------------------------------
class InsightPayload(BaseModel):
key_insight: str = Field(..., example="AI tools reduced content cycles by 40% in 2025.")
supporting_stat: str = Field(..., example="HubSpot 2026 report cites a 12% lift in CTR.")
visual_cue: str = Field(
...,
example="bar_chart_comparison",
description="bar_chart_comparison | line_trend | bullet_points | full_avatar",
)
audio_tone: str = Field(..., example="authoritative_and_surprising")
duration: float = Field(default=10.0, ge=3.0, le=60.0)
chart_data: dict = Field(default_factory=dict)
bullet_lines: Optional[List[str]] = None
class ComposeRequest(BaseModel):
insights: List[InsightPayload]
fps: int = Field(default=24, ge=12, le=60)
fade_dur: float = Field(default=0.5, ge=0.0, le=2.0,
description="Crossfade duration in seconds between scenes")
class JobStatus(BaseModel):
job_id: str
status: str # queued | processing | done | error
output_url: Optional[str] = None
error: Optional[str] = None
# ---------------------------------------------------------------------------
# In-memory job store (replace with Redis in production)
# ---------------------------------------------------------------------------
_jobs: dict[str, JobStatus] = {}
# ---------------------------------------------------------------------------
# Background task: composition worker
# ---------------------------------------------------------------------------
async def _compose_worker(
job_id: str,
request: ComposeRequest,
bg_path: str,
avatar_path: Optional[str],
):
job = _jobs[job_id]
job.status = "processing"
try:
loop = asyncio.get_running_loop()
out_path = str(WORK_DIR / f"{job_id}.mp4")
def _sync_compose():
scenes = []
for i, payload in enumerate(request.insights):
insight = Insight(
key_insight=payload.key_insight,
supporting_stat=payload.supporting_stat,
visual_cue=payload.visual_cue,
audio_tone=payload.audio_tone,
chart_data=payload.chart_data,
duration=payload.duration,
)
assets = SceneAssets(
background_img=bg_path,
avatar_video=avatar_path,
)
scene = dispatch_scene(insight, assets, payload.bullet_lines)
scenes.append(scene)
compose_video(scenes, output_path=out_path, fps=request.fps,
fade_dur=request.fade_dur)
return out_path
await loop.run_in_executor(None, _sync_compose)
job.status = "done"
job.output_url = f"/download/{job_id}"
except Exception as exc:
job.status = "error"
job.error = str(exc)
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
@app.post("/compose", response_model=JobStatus, status_code=202)
async def start_compose(
background_tasks: BackgroundTasks,
payload: str = Form(..., description="JSON string matching ComposeRequest schema"),
background: UploadFile = File(..., description="Background image (JPEG/PNG)"),
avatar: Optional[UploadFile] = File(None, description="Avatar video (MP4) — optional"),
):
"""
Kick off a video composition job.
- **payload**: JSON body (ComposeRequest)
- **background**: background image file
- **avatar**: optional avatar video file
Returns a job_id — poll GET /status/{job_id} for progress.
"""
try:
request = ComposeRequest(**json.loads(payload))
except Exception as e:
raise HTTPException(status_code=422, detail=f"Invalid payload: {e}")
job_id = uuid.uuid4().hex
# Save uploads
job_dir = WORK_DIR / job_id
job_dir.mkdir(exist_ok=True)
bg_path = str(job_dir / background.filename)
with open(bg_path, "wb") as f:
f.write(await background.read())
avatar_path = None
if avatar:
avatar_path = str(job_dir / avatar.filename)
with open(avatar_path, "wb") as f:
f.write(await avatar.read())
# Register job
job = JobStatus(job_id=job_id, status="queued")
_jobs[job_id] = job
# Launch background worker
background_tasks.add_task(
_compose_worker, job_id, request, bg_path, avatar_path
)
return job
@app.get("/status/{job_id}", response_model=JobStatus)
async def get_status(job_id: str):
"""Poll composition job status."""
job = _jobs.get(job_id)
if not job:
raise HTTPException(status_code=404, detail="Job not found")
return job
@app.get("/download/{job_id}")
async def download_video(job_id: str):
"""Download the finished video."""
job = _jobs.get(job_id)
if not job:
raise HTTPException(status_code=404, detail="Job not found")
if job.status != "done":
raise HTTPException(status_code=409, detail=f"Job status: {job.status}")
out_path = WORK_DIR / f"{job_id}.mp4"
if not out_path.exists():
raise HTTPException(status_code=404, detail="Output file missing")
return FileResponse(
path=str(out_path),
media_type="video/mp4",
filename=f"broll_{job_id}.mp4",
)
@app.post("/preview/chart")
async def preview_chart(
chart_data: dict,
title: str = "",
chart_type: str = "bar_chart_comparison",
):
"""Generate and return a chart PNG for preview (no video assembly)."""
out = str(WORK_DIR / f"preview_{uuid.uuid4().hex}.png")
if chart_type == "bar_chart_comparison":
make_bar_chart(chart_data, out, title)
else:
make_line_trend(chart_data, out, title)
return FileResponse(path=out, media_type="image/png")
@app.get("/health")
async def health():
return {"status": "ok"}

View File

@@ -1,456 +0,0 @@
"""
Programmatic B-Roll Composer
Layered composition pipeline: Background + Chart + Avatar Circle + Text Overlays
"""
import json
import numpy as np
from pathlib import Path
from dataclasses import dataclass, field
from typing import Optional
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from PIL import Image, ImageDraw, ImageFont
from moviepy.editor import (
VideoFileClip, ImageClip, CompositeVideoClip,
TextClip, ColorClip, concatenate_videoclips,
)
import moviepy.video.fx.all as vfx
# ---------------------------------------------------------------------------
# Crossfade concat (Option 1: crossfadein + negative padding)
# ---------------------------------------------------------------------------
def crossfade_concat(scenes: list, fade_dur: float = 0.5) -> CompositeVideoClip:
"""
Concatenate scenes with a dissolve transition between each pair.
Each clip (except the first) gets a crossfadein effect.
padding=-fade_dur overlaps consecutive clips so the fade actually fires
instead of creating a black gap. set_duration on every scene is
mandatory — CompositeVideoClip.duration can be ambiguous without it,
which makes the overlap math wrong.
"""
faded = []
for i, clip in enumerate(scenes):
c = clip
if i > 0:
c = c.fx(vfx.crossfadein, fade_dur)
faded.append(c)
return concatenate_videoclips(faded, padding=-fade_dur, method="compose")
# ---------------------------------------------------------------------------
# Data structures
# ---------------------------------------------------------------------------
@dataclass
class Insight:
key_insight: str
supporting_stat: str
visual_cue: str # bar_chart_comparison | line_trend | bullet_points | full_avatar
audio_tone: str
chart_data: dict = field(default_factory=dict)
duration: float = 10.0
@dataclass
class SceneAssets:
background_img: str
chart_img: Optional[str] = None
avatar_video: Optional[str] = None
bullet_img: Optional[str] = None
# ---------------------------------------------------------------------------
# Chart generator (Matplotlib → PNG with transparency)
# ---------------------------------------------------------------------------
CHART_STYLE = {
"bg": "#0D0D0D",
"bar_before": "#2E4057",
"bar_after": "#E63946",
"text": "#F1F1EF",
"grid": "#2A2A2A",
"accent": "#E63946",
}
def make_bar_chart(data: dict, out_path: str, title: str = "") -> str:
"""Render a side-by-side comparison bar chart. Returns output path."""
labels = data.get("labels", [])
before = data.get("before", [])
after = data.get("after", [])
fig, ax = plt.subplots(figsize=(8, 4.5), facecolor="none")
ax.set_facecolor("none")
x = np.arange(len(labels))
w = 0.35
bars_b = ax.bar(x - w / 2, before, w, color=CHART_STYLE["bar_before"],
label="Before", zorder=3, edgecolor="none")
bars_a = ax.bar(x + w / 2, after, w, color=CHART_STYLE["bar_after"],
label="After", zorder=3, edgecolor="none")
ax.set_xticks(x)
ax.set_xticklabels(labels, color=CHART_STYLE["text"], fontsize=11)
ax.tick_params(axis="y", colors=CHART_STYLE["text"])
ax.spines[:].set_visible(False)
ax.yaxis.grid(True, color=CHART_STYLE["grid"], linewidth=0.6, zorder=0)
ax.set_axisbelow(True)
# Value labels on bars
for bar in [*bars_b, *bars_a]:
h = bar.get_height()
ax.text(bar.get_x() + bar.get_width() / 2, h + 0.5, f"{h:.0f}%",
ha="center", va="bottom", color=CHART_STYLE["text"], fontsize=9,
fontweight="bold")
legend = ax.legend(frameon=False, labelcolor=CHART_STYLE["text"],
fontsize=10, loc="upper left")
if title:
ax.set_title(title, color=CHART_STYLE["text"], fontsize=13,
fontweight="bold", pad=12)
fig.tight_layout(pad=0.5)
fig.savefig(out_path, dpi=150, transparent=True, bbox_inches="tight")
plt.close(fig)
return out_path
def make_line_trend(data: dict, out_path: str, title: str = "") -> str:
"""Render a trend line chart. Returns output path."""
x_vals = data.get("x", [])
y_vals = data.get("y", [])
fig, ax = plt.subplots(figsize=(8, 4.5), facecolor="none")
ax.set_facecolor("none")
ax.plot(x_vals, y_vals, color=CHART_STYLE["accent"],
linewidth=2.5, marker="o", markersize=7, zorder=3)
ax.fill_between(x_vals, y_vals, alpha=0.12, color=CHART_STYLE["accent"])
ax.spines[:].set_visible(False)
ax.tick_params(colors=CHART_STYLE["text"])
ax.yaxis.grid(True, color=CHART_STYLE["grid"], linewidth=0.6, zorder=0)
if title:
ax.set_title(title, color=CHART_STYLE["text"], fontsize=13,
fontweight="bold", pad=12)
fig.tight_layout(pad=0.5)
fig.savefig(out_path, dpi=150, transparent=True, bbox_inches="tight")
plt.close(fig)
return out_path
# ---------------------------------------------------------------------------
# Text / Bullet overlay (Pillow → PNG)
# ---------------------------------------------------------------------------
def make_bullet_overlay(lines: list[str], out_path: str,
width: int = 900, font_size: int = 32) -> str:
"""Render bullet points on a semi-transparent dark pill. Returns path."""
padding = 32
line_h = font_size + 16
img_h = padding * 2 + len(lines) * line_h + 12
img = Image.new("RGBA", (width, img_h), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
# Semi-transparent background pill
draw.rounded_rectangle([0, 0, width - 1, img_h - 1],
radius=18, fill=(10, 10, 10, 185))
try:
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
font_size)
except OSError:
font = ImageFont.load_default()
y = padding
for line in lines:
draw.text((padding + 18, y), f"{line}", font=font, fill=(241, 241, 239, 255))
y += line_h
img.save(out_path, format="PNG")
return out_path
def make_insight_card(insight: str, stat: str, out_path: str,
width: int = 960, height: int = 200) -> str:
"""Render a bold insight card (headline + supporting stat). Returns path."""
img = Image.new("RGBA", (width, height), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
draw.rounded_rectangle([0, 0, width - 1, height - 1],
radius=14, fill=(10, 10, 10, 200))
# Red accent bar
draw.rectangle([28, 24, 36, height - 24], fill=(230, 57, 70, 255))
try:
font_lg = ImageFont.truetype(
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 34)
font_sm = ImageFont.truetype(
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
except OSError:
font_lg = font_sm = ImageFont.load_default()
draw.text((58, 36), insight, font=font_lg, fill=(241, 241, 239, 255))
draw.text((58, 90), stat, font=font_sm, fill=(180, 180, 178, 230))
img.save(out_path, format="PNG")
return out_path
# ---------------------------------------------------------------------------
# Circular avatar mask
# ---------------------------------------------------------------------------
def apply_circle_mask(clip: VideoFileClip, diameter: int) -> VideoFileClip:
"""Resize clip and apply a circular alpha mask."""
clip = clip.resize(height=diameter)
w, h = clip.size
# Build a circular mask array (1 = opaque, 0 = transparent)
Y, X = np.ogrid[:h, :w]
cx, cy = w / 2, h / 2
mask_arr = ((X - cx) ** 2 + (Y - cy) ** 2 <= (min(w, h) / 2) ** 2).astype(float)
mask_clip = ImageClip(mask_arr, ismask=True).set_duration(clip.duration)
return clip.set_mask(mask_clip)
# ---------------------------------------------------------------------------
# Ken Burns zoom effect
# ---------------------------------------------------------------------------
def ken_burns(clip: ImageClip, zoom_ratio: float = 0.08) -> ImageClip:
"""Apply a slow zoom-in over the clip duration."""
def zoom_frame(get_frame, t):
frame = get_frame(t)
frac = 1 + zoom_ratio * (t / clip.duration)
h, w = frame.shape[:2]
new_h, new_w = int(h / frac), int(w / frac)
y1 = (h - new_h) // 2
x1 = (w - new_w) // 2
cropped = frame[y1:y1 + new_h, x1:x1 + new_w]
return np.array(Image.fromarray(cropped).resize((w, h), Image.LANCZOS))
return clip.fl(zoom_frame, apply_to=["mask"])
# ---------------------------------------------------------------------------
# Scene builders (one per visual_cue type)
# ---------------------------------------------------------------------------
def build_data_scene(assets: SceneAssets, insight: Insight) -> CompositeVideoClip:
"""
Layout: Background (Ken Burns) + Chart (fade-in) + Avatar circle (corner) + Insight card
"""
d = insight.duration
layers = []
# 1. Background
bg = (ImageClip(assets.background_img)
.set_duration(d)
.resize(height=1080))
bg = ken_burns(bg)
bg = bg.fx(vfx.lum_contrast, 0, -40) # darken 40 units
layers.append(bg)
# 2. Programmatic chart
if assets.chart_img:
chart = (ImageClip(assets.chart_img)
.set_duration(d - 1.5)
.set_start(0.5)
.resize(width=700)
.set_position(("center", 180))
.fx(vfx.fadein, 0.6)
.fx(vfx.fadeout, 0.4))
layers.append(chart)
# 3. Insight card at bottom
card_path = "/tmp/insight_card.png"
make_insight_card(insight.key_insight, insight.supporting_stat, card_path)
card = (ImageClip(card_path)
.set_duration(d - 1)
.set_start(0.5)
.set_position(("center", 820))
.fx(vfx.fadein, 0.5))
layers.append(card)
# 4. Avatar circle (bottom-right corner)
if assets.avatar_video:
avatar_raw = VideoFileClip(assets.avatar_video).subclip(0, d)
avatar = apply_circle_mask(avatar_raw, diameter=240)
avatar = avatar.set_position((bg.w - 280, bg.h - 280))
layers.append(avatar)
# set_duration is required: CompositeVideoClip infers duration from its
# constituent clips, which can be ambiguous when sub-clips have set_start
# offsets. Without this, crossfade_concat's overlap math goes wrong.
return CompositeVideoClip(layers, size=bg.size).set_duration(d)
def build_bullet_scene(assets: SceneAssets, insight: Insight,
bullets: list[str]) -> CompositeVideoClip:
"""
Layout: AI image (Ken Burns) + Bullet overlay + Avatar circle
"""
d = insight.duration
layers = []
bg = (ImageClip(assets.background_img)
.set_duration(d)
.resize(height=1080))
bg = ken_burns(bg, zoom_ratio=0.05)
bg = bg.fx(vfx.lum_contrast, 0, -50)
layers.append(bg)
bullet_path = "/tmp/bullets.png"
make_bullet_overlay(bullets, bullet_path, width=860)
bullets_clip = (ImageClip(bullet_path)
.set_duration(d - 1)
.set_start(0.5)
.set_position(("center", "center"))
.fx(vfx.fadein, 0.7))
layers.append(bullets_clip)
if assets.avatar_video:
avatar_raw = VideoFileClip(assets.avatar_video).subclip(0, d)
avatar = apply_circle_mask(avatar_raw, diameter=200)
avatar = avatar.set_position((bg.w - 240, bg.h - 240))
layers.append(avatar)
return CompositeVideoClip(layers, size=bg.size).set_duration(d)
def build_full_avatar_scene(assets: SceneAssets, insight: Insight) -> VideoFileClip:
"""Full-screen avatar — the expensive 'Hook' scene. No overlay."""
d = insight.duration
avatar = VideoFileClip(assets.avatar_video).subclip(0, d)
return avatar.resize(height=1080).set_duration(d)
# ---------------------------------------------------------------------------
# Scene dispatcher — maps visual_cue → builder
# ---------------------------------------------------------------------------
def dispatch_scene(insight: Insight, assets: SceneAssets,
bullet_lines: Optional[list[str]] = None) -> CompositeVideoClip:
cue = insight.visual_cue
if cue == "full_avatar":
return build_full_avatar_scene(assets, insight)
elif cue in ("bar_chart_comparison", "line_trend"):
chart_path = "/tmp/chart.png"
if cue == "bar_chart_comparison":
make_bar_chart(insight.chart_data, chart_path,
title=insight.key_insight)
else:
make_line_trend(insight.chart_data, chart_path,
title=insight.key_insight)
assets.chart_img = chart_path
return build_data_scene(assets, insight)
elif cue == "bullet_points":
lines = bullet_lines or [insight.key_insight, insight.supporting_stat]
return build_bullet_scene(assets, insight, lines)
else:
# Fallback: data scene without chart
return build_data_scene(assets, insight)
# ---------------------------------------------------------------------------
# Master compositor — assembles all scenes into one video
# ---------------------------------------------------------------------------
def compose_video(scenes: list, output_path: str = "output.mp4",
fps: int = 24, fade_dur: float = 0.5) -> str:
"""Concatenate scenes with crossfade transitions and write final video file."""
final = crossfade_concat(scenes, fade_dur=fade_dur)
final.write_videofile(
output_path,
fps=fps,
codec="libx264",
audio_codec="aac",
threads=4,
preset="fast",
logger=None,
)
return output_path
# ---------------------------------------------------------------------------
# JSON bridge — LLM insight → assets + scene
# ---------------------------------------------------------------------------
def pipeline_from_json(insight_json: str,
background_img: str,
avatar_video: Optional[str] = None) -> str:
"""
Full pipeline:
1. Parse LLM insight JSON
2. Generate chart / overlay assets
3. Build scene
4. Write video
Returns path to output video.
"""
data = json.loads(insight_json)
insight = Insight(**{k: data[k] for k in Insight.__dataclass_fields__ if k in data})
assets = SceneAssets(background_img=background_img, avatar_video=avatar_video)
scene = dispatch_scene(insight, assets,
bullet_lines=data.get("bullet_lines"))
out = f"/tmp/scene_{insight.visual_cue}.mp4"
compose_video([scene], output_path=out)
return out
# ---------------------------------------------------------------------------
# Demo / smoke-test (no real media files needed for chart generation)
# ---------------------------------------------------------------------------
if __name__ == "__main__":
# --- Test 1: Chart PNG generation only ---
sample_bar_data = {
"labels": ["Content Velocity", "CTR", "Engagement", "Cost/Lead"],
"before": [30, 22, 18, 60],
"after": [72, 34, 41, 38],
}
chart_out = make_bar_chart(
sample_bar_data,
"/tmp/demo_chart.png",
title="AI Tools Impact: Before vs After (2025)",
)
print(f"Chart saved → {chart_out}")
# --- Test 2: Bullet overlay PNG ---
bullets = [
"AI reduced content cycles by 40% in 2025",
"HubSpot: 12% lift in CTR with AI-assisted copy",
"Video production cost down 3x with hybrid pipeline",
]
bullet_out = make_bullet_overlay(bullets, "/tmp/demo_bullets.png")
print(f"Bullets saved → {bullet_out}")
# --- Test 3: Insight card PNG ---
card_out = make_insight_card(
"AI tools reduced content cycles by 40%",
"HubSpot 2026 report — 12% lift in CTR",
"/tmp/demo_card.png",
)
print(f"Insight card saved → {card_out}")
# --- Test 4: JSON bridge (chart only, no video files required) ---
sample_json = json.dumps({
"key_insight": "AI reduced production time by 40%",
"supporting_stat": "HubSpot 2026: 12% CTR lift",
"visual_cue": "bar_chart_comparison",
"audio_tone": "authoritative_and_surprising",
"duration": 8.0,
"chart_data": sample_bar_data,
})
print("\nSample Insight JSON:\n", sample_json)
print("\nAll asset generation tests passed.")
print("To run full video composition, supply real background_img and avatar_video paths.")

View File

@@ -280,10 +280,11 @@ async def generate_podcast_audio(
if voice_sample_url:
from services.llm_providers.main_audio_generation import qwen3_voice_clone, cosyvoice_voice_clone
from utils.media_utils import detect_audio_format
engine = (request.voice_clone_engine or "qwen3").lower()
logger.warning(f"[Podcast] 🔊 Voice clone path: engine={engine}, scene='{request.scene_title}', voice_sample_url={voice_sample_url[:80]}...")
# Download voice sample from URL (with caching)
logger.warning(f"[Podcast] Fetching voice sample from: {voice_sample_url}")
try:
@@ -294,6 +295,11 @@ async def generate_podcast_audio(
logger.warning(f"[Podcast] Voice sample fetch result: {len(voice_sample_bytes) if voice_sample_bytes else 0} bytes")
if not voice_sample_bytes:
raise HTTPException(status_code=400, detail=f"Could not fetch voice sample from {voice_sample_url}")
# Detect actual audio format from bytes (may differ from file extension)
detected_fmt, detected_mime = detect_audio_format(voice_sample_bytes)
logger.warning(f"[Podcast] 🔊 Detected voice sample format: {detected_fmt} ({detected_mime}), {len(voice_sample_bytes)} bytes")
voice_mime_type = detected_mime or "audio/wav"
scene_text = request.text.strip()
if len(scene_text) > 4000:
@@ -329,6 +335,7 @@ async def generate_podcast_audio(
audio_bytes=voice_sample_bytes,
text=scene_text,
user_id=user_id,
audio_mime_type=voice_mime_type,
),
)
audio_bytes = result_obj.preview_audio_bytes
@@ -341,6 +348,7 @@ async def generate_podcast_audio(
audio_bytes=voice_sample_bytes,
text=scene_text,
user_id=user_id,
audio_mime_type=voice_mime_type,
),
)
audio_bytes = result_obj.preview_audio_bytes
@@ -419,9 +427,14 @@ async def generate_podcast_audio(
result["audio_url"] = f"/api/podcast/audio/{audio_filename}"
logger.warning(f"[Podcast] Audio generated - path: {result.get('audio_path')}, url: {result.get('audio_url')}")
except HTTPException:
raise
except Exception as exc:
logger.error(f"[Podcast] ❌ Audio generation failed: {exc}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Audio generation failed: {exc}")
exc_type = type(exc).__name__
exc_msg = str(exc)[:500]
logger.error(f"[Podcast] Audio generation failed ({exc_type}): {exc_msg}")
logger.error(f"[Podcast] Audio generation traceback:", exc_info=True)
raise HTTPException(status_code=500, detail=f"Audio generation failed ({exc_type}): {exc_msg}")
# Save to asset library (podcast module)
try:

View File

@@ -14,7 +14,7 @@ from pydantic import BaseModel, Field
from pathlib import Path
import uuid
from middleware.auth_middleware import get_current_user
from middleware.auth_middleware import get_current_user, get_current_user_with_query_token
from api.story_writer.utils.auth import require_authenticated_user
from api.story_writer.task_manager import task_manager
from api.podcast.utils import _resolve_podcast_media_file
@@ -23,7 +23,7 @@ from utils.media_utils import resolve_media_path
from loguru import logger
router = APIRouter()
router = APIRouter(prefix="/broll", tags=["B-Roll"])
def _resolve_broll_background_image_path(background_image_url: str) -> str:
@@ -148,7 +148,7 @@ class BrollSceneRequest(BaseModel):
key_insight: str
supporting_stat: str
chart_data: Optional[Dict[str, Any]] = None
visual_cue: str = Field(default="bar_chart_comparison", description="bar_chart_comparison | bullet_points")
visual_cue: str = Field(default="bar_comparison", description="bar_comparison | bar_horizontal | line_trend | pie | stacked_bar | bullet_points | full_avatar")
duration: float = Field(default=10.0, ge=3.0, le=60.0)
background_image_url: str
avatar_video_url: Optional[str] = None
@@ -216,7 +216,9 @@ async def generate_chart_preview(
)
preview_filename = Path(preview_path).name
preview_url = f"/api/podcast/preview/{chart_id}/{preview_filename}"
preview_url = f"/api/podcast/broll/preview/{chart_id}/{preview_filename}"
logger.warning(f"[Broll] Chart preview generated: chart_id={chart_id}, path={preview_path}, url={preview_url}")
return ChartPreviewResponse(
preview_url=preview_url,
@@ -249,7 +251,7 @@ async def generate_broll_scene(
try:
# Validate visual_cue
valid_cues = ["bar_chart_comparison", "bullet_points", "full_avatar"]
valid_cues = ["bar_comparison", "bar_chart_comparison", "bar_horizontal", "line_trend", "pie", "stacked_bar", "bullet_points", "full_avatar"]
if request.visual_cue not in valid_cues:
raise HTTPException(
status_code=400,
@@ -333,37 +335,39 @@ async def compose_broll_videos(
async def serve_chart_preview(
chart_id: str,
filename: str,
user_id: Optional[str] = None,
current_user: Dict[str, Any] = Depends(get_current_user_with_query_token),
):
"""
Serve chart preview PNG files.
- user_id passed as query param for multi-tenant workspace resolution
- endpoint is public (no auth) to allow direct image loading in browser
Uses authentication via Authorization header or token query parameter,
matching the pattern used by /api/podcast/images/ for browser <img> tags.
"""
from api.podcast.constants import get_podcast_media_dir
user_id = require_authenticated_user(current_user)
# Validate filename to prevent directory traversal
if ".." in filename or "/" in filename or "\\" in filename:
raise HTTPException(status_code=400, detail="Invalid filename")
logger.warning(f"[Broll] serve_chart_preview: chart_id={chart_id}, filename={filename}, user_id={user_id}")
broll_service = get_broll_service(user_id=user_id)
expected_filename = broll_service.get_chart_preview_filename(chart_id)
if filename != expected_filename:
raise HTTPException(status_code=404, detail="Chart preview not found")
# Use expected_filename to get the correct path
file_path = broll_service.get_output_path(expected_filename)
charts_dir = get_podcast_media_dir("chart", user_id)
file_path = charts_dir / filename
logger.warning(f"[Broll] serve_chart_preview: resolved path={file_path}, exists={file_path.exists()}")
if not file_path.exists():
raise HTTPException(status_code=404, detail="Chart preview not found")
# Security: ensure resolved path is within charts_dir
if not str(file_path.resolve()).startswith(str(charts_dir.resolve())):
raise HTTPException(status_code=403, detail="Access denied")
return FileResponse(
path=str(file_path),
media_type="image/png",
filename=expected_filename,
filename=filename,
)

View File

@@ -141,16 +141,18 @@ async def generate_podcast_script(
if numeric_pairs:
labels = [p[0] for p in numeric_pairs]
values = [p[1] for p in numeric_pairs]
sources = [f.get("url", f.get("source", "")) for f in research_fact_cards[:12] if f.get("url") or f.get("source")]
return {
"type": "bar_comparison",
"title": scene_title,
"labels": labels,
"values": values,
"takeaway": "Data points sourced from research facts used in this scene.",
"source": sources[0] if sources else "",
}
return {
"type": "bullet",
"type": "bullet_points",
"title": scene_title,
"bullet_points": ["Key point 1", "Key point 2", "Key point 3"],
"takeaway": "Narration summary for this scene.",
@@ -233,11 +235,15 @@ Return JSON with scenes array. Each scene:
- ttsHints: optional list from [pause_300ms, pause_700ms, smile, serious_tone, emphasize_data]
- Plain text only, no markdown
- chart_data: object for B-roll mapping (required in audio_only)
- type: bar_comparison|line_trend|bullet_points
- type: bar_comparison|bar_horizontal|line_trend|pie|stacked_bar|bullet_points
- title: short chart title
- labels: list
- values: list (same length as labels)
- values: list (same length as labels, required for bar/line/pie)
- before/after: parallel lists of numbers (for bar_comparison only)
- segments: list of {{name, values}} (for stacked_bar only)
- bullet_points: list of strings (for bullet_points only)
- takeaway: one sentence tying chart to narration
- source: URL or citation for the data (e.g. "Research fact #3" or a URL from the research context)
COST OPTIMIZATION:
- 5-6 scenes max for {request.duration_minutes} min episode