chatbot / src /video_export.py
SuhasGholkar's picture
Update src/video_export.py
272732e verified
Raw History Blame Contribute Delete
9.01 kB
from __future__ import annotations
import os, wave
from pathlib import Path
from datetime import datetime
from typing import List, Dict, Any, Optional
import numpy as np
from moviepy.editor import ImageClip, AudioFileClip, concatenate_videoclips
from PIL import Image, ImageDraw, ImageFont
# offline TTS
try:
import pyttsx3
_HAS_PYTTSX3 = True
except Exception:
_HAS_PYTTSX3 = False
# If you already have this helper in your file, keep using your version.
# It renders a PIL.Image for a single chat row.
# def _render_slide_image(row: Dict[str, Any]) -> PIL.Image.Image: ...
def _ensure_tts_engine() -> Optional["pyttsx3.Engine"]:
"""Initialize pyttsx3 (offline TTS). Returns engine or None on failure."""
if not _HAS_PYTTSX3:
return None
try:
eng = pyttsx3.init()
# Optional: tweak voice/rate/volume
# eng.setProperty("rate", 170) # ~170 wpm
# eng.setProperty("volume", 0.9)
return eng
except Exception:
return None
def _tts_to_wav(text: str, out_wav: Path, engine: "pyttsx3.Engine") -> bool:
"""Synthesize `text` to WAV at `out_wav`. Returns True on success."""
try:
engine.save_to_file(text, str(out_wav))
engine.runAndWait() # blocks until the file is written
return out_wav.exists() and out_wav.stat().st_size > 0
except Exception:
return False
def _wav_duration_seconds(path: Path) -> float:
"""Accurate duration of a WAV file in seconds."""
with wave.open(str(path), "rb") as wf:
frames = wf.getnframes()
rate = wf.getframerate() or 1
return frames / float(rate)
def _narration_for_row(row: Dict[str, Any]) -> str:
"""Compose concise narration text for a slide."""
ts = (row.get("timestamp") or "").strip()
user_msg = (row.get("user_message") or "").strip()
bot_msg = (row.get("bot_reply") or "").strip()
intent = (row.get("intent") or "").strip()
# Keep it short and clear; you can tailor this
parts = []
if ts:
parts.append(f"Conversation at {ts}.")
if user_msg:
parts.append(f"Customer said: {user_msg}")
if bot_msg:
parts.append(f"Assistant replied: {bot_msg}")
if intent:
parts.append(f"Intent: {intent}.")
text = " ".join(parts)
# Avoid overly long narration (pyttsx3 can be slow on huge text)
if len(text) > 900:
text = text[:900] + "…"
return text or "Conversation."
def export_records_to_video(
rows: List[Dict[str, Any]],
out_path: Optional[str] = None,
tts: bool = True,
min_secs_per_slide: float = 4.0,
fps: int = 30,
) -> str:
"""
Create an MP4 slideshow from chat rows.
- If `tts` is True and pyttsx3/eSpeak are available, generate per-slide narration WAVs
and set each slide duration = max(min_secs_per_slide, narration_length).
- If TTS unavailable or fails, falls back to silent slides with `min_secs_per_slide`.
Returns absolute path to the generated MP4.
"""
if not rows:
raise ValueError("No rows provided for video export.")
# Output path & folder
out_path = out_path or str(Path("/mnt/data") / f"chat_export_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.mp4")
out_dir = Path(out_path).parent
out_dir.mkdir(parents=True, exist_ok=True)
# Prepare TTS (optional)
engine = _ensure_tts_engine() if tts else None
if tts and engine is None:
print("[video_export] Offline TTS not available; exporting silent video.")
# Build slide clips (+ audio if available)
clips = []
for idx, row in enumerate(rows, start=1):
# 1) Render slide image
pil_img = _render_slide_image(row) # must return a PIL.Image
frame = np.asarray(pil_img)
# 2) Narration & duration
audio_path = None
duration = float(min_secs_per_slide)
if engine is not None:
narration = _narration_for_row(row)
audio_path = out_dir / f"slide_{idx:03d}.wav"
ok = _tts_to_wav(narration, audio_path, engine)
if ok:
try:
wav_len = _wav_duration_seconds(audio_path)
duration = max(min_secs_per_slide, wav_len)
except Exception:
# keep default duration if length probing fails
pass
else:
audio_path = None # fallback to silent
# 3) Make a clip for this slide
clip = ImageClip(frame).set_duration(duration)
# 4) Attach audio if present
if audio_path and audio_path.exists():
try:
aclip = AudioFileClip(str(audio_path))
clip = clip.set_audio(aclip)
except Exception:
pass
clips.append(clip)
# 5) Concatenate & write MP4 with audio
final = concatenate_videoclips(clips, method="compose")
out_file = Path(out_path)
# Using AAC for wide compatibility
final.write_videofile(
str(out_file),
fps=fps,
codec="libx264",
audio_codec="aac",
temp_audiofile=str(out_dir / "temp_mux_audio.m4a"),
remove_temp=True,
threads=2,
preset="medium",
verbose=False,
logger=None,
)
# Close clips to free resources
for c in clips:
try:
c.close()
except Exception:
pass
return str(out_file.resolve())
def _font_try(paths, size):
"""Try multiple font paths, fallback to PIL default."""
for p in paths:
try:
return ImageFont.truetype(p, size)
except Exception:
pass
return ImageFont.load_default()
def _wrap(draw, text, font, max_width_px):
"""Word-wrap text to fit pixel width using PIL measurement."""
if not text:
return [""]
words = text.split()
lines, cur = [], []
for w in words:
test = " ".join(cur + [w])
if draw.textlength(test, font=font) <= max_width_px:
cur.append(w)
else:
if cur:
lines.append(" ".join(cur))
cur = [w]
if cur:
lines.append(" ".join(cur))
return lines
def _draw_block(draw, x, y, text, font, fill, max_width_px, line_spacing=8):
"""Draw wrapped text block and return new y position."""
for line in _wrap(draw, text, font, max_width_px):
draw.text((x, y), line, font=font, fill=fill)
y += font.size + line_spacing
return y
def _render_slide_image(row, W=1280, H=720):
"""
Render one slide as a PIL.Image from a chat row dict:
expects keys like: id, timestamp, user_message, bot_reply, intent, sentiment, resolution, ftr_ms.
"""
# Layout & theme
margin = 60
box_w = W - margin * 2
bg = (247, 249, 252) # page background
card = (255, 255, 255) # card background
brand = (38, 110, 241) # brand blue
text_primary= (23, 37, 61) # main text
text_muted = (71, 85, 105) # muted
separator = (229, 231, 235) # border line
# Fonts
font_paths = [
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
]
font_title = _font_try(font_paths, 40)
font_label = _font_try(font_paths, 24)
font_body = _font_try(font_paths, 28)
font_footer = _font_try(font_paths, 22)
# Canvas
img = Image.new("RGB", (W, H), bg)
draw = ImageDraw.Draw(img)
# Card
draw.rounded_rectangle([margin, margin, W - margin, H - margin],
radius=24, fill=card, outline=separator, width=1)
x = margin + 40
y = margin + 30
# Title
title = f"Ecomm Chat • {row.get('timestamp','')} • ID {row.get('id','')}"
draw.text((x, y), title, font=font_title, fill=text_primary)
y += font_title.size + 20
draw.line([(x, y), (W - margin - 40, y)], fill=separator, width=2)
y += 24
# Customer message
draw.text((x, y), "Customer", font=font_label, fill=brand)
y += font_label.size + 8
y = _draw_block(draw, x, y, row.get("user_message", ""), font_body, text_primary, box_w)
y += 16
# Assistant reply
draw.text((x, y), "Assistant", font=font_label, fill=brand)
y += font_label.size + 8
y = _draw_block(draw, x, y, row.get("bot_reply", ""), font_body, text_primary, box_w)
y += 16
# Footer meta (optional)
bits = []
if row.get("intent"): bits.append(f"Intent: {row['intent']}")
if row.get("sentiment"): bits.append(f"Sentiment: {row['sentiment']}")
if row.get("resolution"): bits.append(f"Resolution: {row['resolution']}")
if row.get("ftr_ms"): bits.append(f"Latency: {row['ftr_ms']} ms")
if bits:
y2 = H - margin - 80
draw.line([(x, y2), (W - margin - 40, y2)], fill=separator, width=1)
draw.text((x, y2 + 20), " • ".join(bits), font=font_footer, fill=text_muted)
return img