fix(pipeline): clip wordless footage by vision instead of failing on an empty transcript

The vision path only fired when the source had no audio track. A nursery
rhyme or a dashcam drive has audio, so it went through transcription, came
back as one segment, built one scoring window and Gemini returned no clips:
three failed jobs on 25-aug-2026, one user twice. Below 8 words or 5 words
per minute the transcript cannot drive selection, so route it like a silent
video (vision, no captions). Thresholds env-overridable.
This commit is contained in:
juancarlos.cavero 2026-08-25 01:12:50 +02:00
parent 150a2953a1
commit 2e9d6a8001
2 changed files with 75 additions and 0 deletions

26
main.py
View file

@ -1546,6 +1546,24 @@ def get_viral_clips(transcript_result, video_duration):
return None return None
# --- Speech too sparse to clip by transcript -------------------------------
# The vision path used to fire only on a missing audio TRACK. A nursery-rhyme
# video or a dashcam drive has audio, so it went through transcription, came
# back as one segment ("Uh uh"), produced one scoring window and Gemini
# returned no clips — three failed jobs on 25-aug-2026, one user twice. Speech
# is ~120-160 words/min; below these floors there is nothing to clip by words.
MIN_SPEECH_WORDS_PER_MIN = float(os.environ.get("MIN_SPEECH_WORDS_PER_MIN", "5"))
MIN_SPEECH_WORDS = int(os.environ.get("MIN_SPEECH_WORDS", "8"))
def speech_is_sparse(transcript, duration):
"""True when the transcript is too thin to drive clip selection."""
words = sum(len((seg.get("text") or "").split())
for seg in (transcript or {}).get("segments", []))
minutes = max(float(duration or 0) / 60.0, 1e-6)
return words < MIN_SPEECH_WORDS or words / minutes < MIN_SPEECH_WORDS_PER_MIN
def get_visual_clips(video_path, video_duration, language="en"): def get_visual_clips(video_path, video_duration, language="en"):
"""Clip a SILENT video by vision: Gemini watches the footage and picks the """Clip a SILENT video by vision: Gemini watches the footage and picks the
most engaging visual moments (no transcript). Returns the same most engaging visual moments (no transcript). Returns the same
@ -1756,6 +1774,14 @@ if __name__ == '__main__':
except NoAudioError as e: except NoAudioError as e:
print(f"🔇 {e} — switching to visual analysis.") print(f"🔇 {e} — switching to visual analysis.")
# Music-only or wordless footage transcribes to a handful of words.
# Clip it by what is on screen instead, like a video with no audio.
if transcript is not None and speech_is_sparse(transcript, duration):
n_words = sum(len((sg.get("text") or "").split()) for sg in transcript["segments"])
print(f"🔇 Only {n_words} word(s) of speech in {duration:.0f}s — "
f"switching to visual analysis.")
transcript = None
# 4. Gemini Analysis (transcript-driven, or vision for silent videos) # 4. Gemini Analysis (transcript-driven, or vision for silent videos)
if transcript is not None: if transcript is not None:
clips_data = get_viral_clips(transcript, duration) clips_data = get_viral_clips(transcript, duration)

View file

@ -0,0 +1,49 @@
"""Wordless footage must be clipped by vision, not fail on an empty transcript.
The vision path used to fire only on a missing audio track. Three prod jobs
on 25-aug-2026 had audio but no speech (a nursery rhyme transcribed as
"Uh uh", a dashcam drive as "Yeah."), went through the transcript path and
died with "Gemini did not return usable clips".
"""
import pytest
main = pytest.importorskip("main") # needs cv2/mediapipe, absent in minimal CI
def _t(*texts):
return {"language": "en",
"segments": [{"start": i, "end": i + 1, "text": t} for i, t in enumerate(texts)]}
class TestRealFailures:
def test_nursery_rhyme_uh_uh_is_sparse(self):
assert main.speech_is_sparse(_t("Uh uh"), 169)
def test_dashcam_yeah_is_sparse(self):
assert main.speech_is_sparse(_t("Yeah."), 621)
class TestRealSpeechIsKept:
def test_a_talk_is_not_sparse(self):
words = ["so today we are going to look at how this works"] * 60 # ~10 w/segment
assert not main.speech_is_sparse(_t(*words), 600)
def test_a_short_clip_with_a_sentence_is_not_sparse(self):
# 20s with one real sentence: nothing to gain from vision.
assert not main.speech_is_sparse(_t("welcome back everyone to another episode of the show"), 20)
def test_a_song_with_lyrics_is_not_sparse(self):
# "Wheels on the Bus" style: repetitive but plenty of words.
assert not main.speech_is_sparse(_t(*["the wheels on the bus go round and round"] * 30), 180)
class TestEdges:
def test_empty_segments_is_sparse(self):
assert main.speech_is_sparse({"segments": []}, 100)
def test_none_transcript_is_sparse(self):
assert main.speech_is_sparse(None, 100)
def test_zero_duration_does_not_divide_by_zero(self):
assert main.speech_is_sparse(_t("hi"), 0)
assert not main.speech_is_sparse(_t(*["a b c d e f g h i j"] * 3), 0)