8Dakar → Paris, on the whiteboard · episode 8 of 10
Clips: the Twitch API and the proof in the right place
Each moment in the travel log can show up to three clips, those short extracts cut by viewers. They are the proof of what we tell, and a door into the video.
But the right clip has to appear under the right moment. We discovered that, more than half the time, it did not. This episode tells how we measured that, then fixed it.
More than 17,000 clips downloaded in 30 days, each one dated and often titled by a human. The problem appeared at display time: 68% of moments showed the same clip as a neighbouring moment.
This episode covers collection through the Helix API, the handling of automatic titles, the audit before the fix, attachment by similarity of meaning (bge-m3) with a minimum gap rather than a threshold, and the measurement afterwards.
Independent Veden project, not affiliated with Nico, Ascend or their partners.
Move forward with the arrow → or the Next button. “Discovery / Technical”, at the top, changes the level of the text; “Listen” reads it aloud.
8Fetching
Every five minutes, one hour at a time
Every five minutes, our machine asks Twitch for the list of new clips and downloads them straight away: a clip can be deleted at any time by its author or by the channel.
A small surprise: when we asked for a whole day at once, Twitch missed a few. Asking hour by hour, we got them all.
A polling job queries the Helix API every 5 minutes (the stream's status, every minute). Over large windows, clip pagination returned duplicates and lost some: about 7% missing over 24 hours. So we query hour by hour and deduplicate.
Each new clip is downloaded immediately with Streamlink, at 480p at most: at 1080p, a clip weighs about 40 MB, unsustainable for thousands. It is dated to the minute it shows, thanks to its position in the replay; failing that, 30 seconds before its creation.
8Titles
A clip's title is not always a title
A viewer who creates a clip can give it a title, such as “fall into Lake Retba”. That is valuable: a human has summed up the moment.
But if they don't name it, Twitch gives it the stream's title. That was the case for 38% of clips. Our artificial intelligence, seeing that same title come back everywhere, thought it was reading advertising. Those titles are now ignored.
A clip that has not been renamed takes the stream's title: 38% of clips. In a slice's clip list, this repeated title looked like a slogan, and the language model saw advertising in it.
A function of a few lines decides that a title of three characters or fewer, or one that starts like the stream's usual title, is not a title. Those clips are still counted, without a title, in the request to the model; their number remains a signal, their title disappears.
8The first version
The most-viewed clip, everywhere at once
The first idea was simple: under each moment, show the three most-viewed clips of that period, give or take a minute and a half.
But two neighbouring moments overlap. The same very popular clip fell under both, then under the next one, and crowded out less-viewed but more accurate clips.
Version 1: for each moment, all the clips between its start and its end, give or take 90 seconds (a clip generally covers the 30 to 60 seconds before its creation, hence the margin), sorted by views, the top three displayed.
The flaw does not show in the code; it shows as soon as two moments are close: their windows overlap, and the most-viewed extract ends up everywhere. The code reasoned moment by moment, whereas the question is asked clip by clip: where does this one belong?
8Measure first
An audit before touching the code
Before fixing anything, we measured. Result: more than two moments out of three showed a clip already displayed under a neighbouring moment.
Worse: when a viewer had taken the trouble to name their clip well, it was more often misplaced than correctly placed.
An audit on the travel log's real data, with two questions: how many moments display a clip that a neighbour also displays? Among clips with a meaningful title, for how many does a neighbouring moment match the title better?
audit before the fix
68%
of moments (1,996 out of 2,923) shared a clip with a neighbour; for 57% of clips with a meaningful title, a neighbour was a better match.
8The meaning of titles
Comparing a title with a moment
A clip's title and a moment's description often share no words. So we use a small model that measures whether two sentences talk about the same thing, even in different words.
If the title resembles one moment clearly more than its neighbour, it points to that moment. If it resembles both equally, we don't guess: the clip's time decides.
An embedding model, bge-m3, served locally, turns titles and moments into normalised vectors; their dot product gives the cosine similarity. A single value is hard to read: two neighbouring moments have a median similarity of 0.50, and unrelated texts are not at 0.
Hence a relative criterion: the title points to a moment if it prefers it to the second candidate by at least 0.05 (for example 0.62 against 0.48). An absolute threshold would have depended on the model, the language and the length of the texts.
8The fix
One clip, one moment only
The rule is now reversed: each clip goes under one moment only. The one its title clearly points to; otherwise, the one that was under way in the middle of the clip; otherwise, the closest.
Result: some moments no longer have a clip. We prefer a moment with no clip to a moment illustrated by a neighbour's clip.
For each clip: the candidate moments are those that frame its midpoint, give or take 90 seconds. A single candidate: settled. Otherwise, a meaningful title decides if it beats the runner-up by at least 0.05; failing that, the moment under way at the clip's midpoint, otherwise the closest. The vectors are computed in a single pass, in batches.
See the code: each clip goes to a single moment
MARGE, ECART = 90, 0.05
def rattacher(clip, moments, similarite):
cands = [m for m in moments if m.debut - MARGE <= clip.milieu <= m.fin + MARGE]
if not cands:
return None
if len(cands) > 1 and clip.titre_parlant:
tries = sorted(cands, key=lambda m: -similarite(clip.titre, m.texte))
if similarite(clip.titre, tries[0].texte) - similarite(clip.titre, tries[1].texte) >= ECART:
return tries[0]
en_cours = [m for m in cands if m.debut <= clip.milieu <= m.fin]
return max(en_cours, key=lambda m: m.debut) if en_cours else \
min(cands, key=lambda m: min(abs(m.debut - clip.milieu), abs(m.fin - clip.milieu)))
# clip.milieu = the minute shown by the clip + half its duration (60 s at most)
8Afterwards
The same measurement, after the fix
Same audit, same data, after the fix: no moment shows its neighbour's clip any more. Among well-titled clips, those that would fit better under a neighbouring moment fall from more than half to about a quarter. And the travel log shows more different clips, without a single one having been added.
Almost all the most striking moments keep a clip: 326 out of 334.
Results: 0 moments sharing a clip (against 68%); 24% of clips with a meaningful title better suited to a neighbour (against 57%); 4,374 distinct clips displayed instead of 3,889, the most-viewed extract no longer taking up three places; 326 highlights out of 334 keep a clip. Cost: the travel log export goes from 9 to 55 seconds, acceptable for an hourly task.
Limits we accept: part of the 24% comes from titles too vague to point to a moment, which the rule deliberately leaves to the clock. And the audit judges with the same family of tools as the fix: it proves the fix does what it claims, not that every clip is in its place.
moments showing a neighbour's clip
68%→0%
same audit, same data, before and after.
8In summary
What to remember
- Clips are fetched from Twitch every five minutes, and downloaded straight away.
- A clip without a real title says nothing about the moment: its title is ignored.
- Each clip now goes under one moment only: the right one, or none.
- Measure before and after: from 68% of moments showing a neighbour's clip to zero.
- Helix queried hour by hour, immediate download at 480p at most.
- Discard automatic titles, which polluted the request to the model.
- Reason clip by clip, with a similarity gap rather than an absolute threshold.
- An audit before fixing, the same one after, and its limits written down in black and white.
Supporting documents to attach automatically?
Attachments, sources, extracts: the same method puts them in the right place, and proves it. Write to me: we will look together at what is feasible.
Let's talk about your project