Jonathan Earp Projects / 02
● Project 02 / 2026 ~4,100 lines of Python Runs off one command

PPipeline for EEdge TTracing and EExpression RRendering

Desmos is a graphing calculator, and this renders video inside it as equations. One command goes and finds a clip, traces it, rebuilds it in the calculator and posts the result.

4,494,696
Plays
2,594,952
Reach
294,155
Likes
99,914
Shares
01

What it does

Feed it a Family Guy clip. You get back a vertical video with a Desmos graph on top drawing the scene as line art, expression list and all, and the original clip playing underneath.

It runs off one command. autopilot.py --count 10 finds ten clips, works out which episode each is from, throws out the ones it can't caption, renders them, uploads them to YouTube and leaves the files and captions in a folder for me.

It's about 4,100 lines of Python across 10 modules. No framework, mostly because I didn't realise I'd want one until it was too annoying to add. It leans on yt-dlp, faster-whisper, playwright, opencv, numpy and ffmpeg to do the heavy parts.

01
Harvest

Pull candidate clips from search and a channel list.

02
Identify

Transcribe it, match the dialogue to an episode.

03
Dedupe

Drop anything I've already seen or posted.

04
Render

Trace to polylines, pack into expressions, screenshot.

05
Measure

Time the render, save where the clip came from.

06
Upload

Push to YouTube, then check it's actually live.

07
Stage

Write the files and captions for the manual IG post.

The next four sections go through the render path in order, each with a demo, because most of it was easier to understand once I could see it move.

02

Tracing a frame

Desmos can only draw lines, so each frame has to be turned from pixels into lines first.

Each frame gets decoded to grayscale, run through a Sobel filter, thresholded, then I walk the edge pixels one at a time to get outlines in order: a list of points you could draw without lifting the pen. Even a simple frame comes back with thousands of them, far more than Desmos will take, so the last step throws most of them away.

Try it Frame → polylines Step through the stages
Points — —

Those are the real algorithms running in your browser, on a cat I drew in code so there's nothing copyrighted in the repo. It's the same Sobel operator, border-following and Ramer–Douglas–Peucker simplification the pipeline uses.

Push the tolerance slider around and you'll see the tradeoff I spent a while stuck on. If it's too tight, the expression lists get huge and Desmos slows to a crawl. If it's too loose, the cat stops looking like a cat. I ended up around 1.5 px, which drops roughly 90% of the points and still keeps the shape.

03

Packing it into Desmos

Now I've got a pile of polylines per frame, and I need Desmos to show exactly one frame at a time.

The whole thing is list-valued parametric expressions with a single slider n choosing the frame. Except Desmos caps how long a list can be, so I can't dump every frame into one list. Frames get packed into chunks of about ten.

Which creates a new problem. With the slider on frame 14, chunks 1 and 3 still get evaluated, and if they index past the end of their list the whole graph errors and nothing renders.

Gating fixed it. Each chunk gets an expression that is 1 inside its own frame range and undefined everywhere else, and undefined doesn't error in Desmos, it just makes the curve vanish. The index is clamped alongside it so it always lands somewhere legal, so the chunk boundaries don't need any special cases.

G₁ = {1 ≤ n ≤ 10 : 1}
The gate. Equals 1 while the slider is inside this chunk. Undefined otherwise, which quietly blanks the curve instead of erroring.
J₁ = min(max(n − 0, 1), 10)
The clamped index. Always lands somewhere valid, even when n is nowhere near this chunk. This is what stops the out-of-range error.
R₁ = [O₁[J₁] … E₁[J₁]]
The slice. Pulls this frame's strokes out of the chunk's offset and end lists.
Try it One slider, three chunks Drag n across a boundary
Slider n n = 14
Chunk C₁: frames 1–10
G₁ = · J₁ = ·
·
Chunk C₂: frames 11–20
G₂ = · J₂ = ·
·
Chunk C₃: frames 21–30
G₃ = · J₃ = ·
·

Exactly one chunk is defined at a time. The other two evaluate to nothing and disappear, still doing perfectly legal arithmetic while they do it.

04

The screenshot problem

Once the graph exists, I screenshot it frame by frame in a headless Chromium and stack it over the source clip with ffmpeg. Almost all the time goes here, and it's also where the worst bug was.

I assumed the slow part was tracing. My tracer runs 8 worker processes on a 15-core machine, so the obvious move was more workers. I profiled it to work out how many to add.

One 13.8 s clip, 331 frames. Tracing is 4%. If I'd made it take literally zero time I'd have saved seven seconds out of 171.

Screenshotting was 94% of it, one frame at a time, 486 ms each, in a single browser page. So I parallelised the capture instead of the tracing and the same clip went from 171.6 s to 110.4 s.

Then the frames came out different.

The frames had been wrong the whole time

Obviously the parallel version was broken. Serial had been running for months. So I tested serial against itself: same clip twice on an idle machine, every frame diffed.

Zero of 40 frames differed. Perfectly deterministic, which I took as proof it was correct. All I'd shown was that it was consistent, and something broken the same way every time is consistent too.

The bug was in the settle logic. After moving the slider I wait for the canvas to stop changing before I screenshot it, and "stopped changing" meant 2 unchanged animation frames, about 33 ms. Desmos pauses for longer than that in the middle of an update. So I was catching the background redrawn and the character not.

Try it Where the screenshot lands Start at 2, then drag it up
Unchanged frames required stable = 2
The graph updating
What got screenshotted

To find the real answer I raised the threshold until the output stopped changing. stable=12 and stable=24 come out byte-identical, so 12 is converged and anything at or above 4 is fine.

Frames differing from the converged output, across 40 sampled frames
Settle thresholdFrames wrongVerdict
stable = 2 what I shipped 39 of 40 Months of renders with missing linework
stable = 40 of 40Correct
stable = 60 of 40Correct
stable = 120 of 40Converged, same bytes as stable = 24

Every video I'd made before this had incomplete linework on nearly every frame and nobody noticed, me included. The fix corrected the linework and made the render faster, since the parallel capture that exposed the bug was also the speed-up.

What a render costs

Render time scales with clip length at roughly 11–12×, but the spread is wide: 7.3× to 15.5× in the first batch, depending on how much the tracer reuses between frames. My first five measurements all landed between 10× and 13×, which made me think the constant was far tighter than it is.

Run 1, before the capture fix Run 2, after
20 clips against a 12× reference line. The two batches are different clips, so the gap between them isn't a clean measure of the speedup. The 1.55× comes from the same-clip test above. In practice, ten 15-second clips take about half an hour and ten 45-second clips take an hour and a half. How long I'm sat waiting comes down almost entirely to how long the clip is.
05

Version by version

None of this was designed up front. Each version came out of a measurement that disagreed with what I expected.

Try it What each upgrade actually bought Click through, or use arrow keys

Both of the big wins came from measuring things I thought I already knew. I was sure tracing was the bottleneck and that serial capture was correct, and I was wrong about both.

06

Finding clips

Rendering is only half of it. Something has to go find things worth rendering, and that half had its own set of problems.

The Shorts fix that backfired

yt-dlp --flat-playlist reports duration: None for every single YouTube Short. My length filter read:

if not vid or not dur: continue

which threw away 100% of them. Shorts are under 60 seconds by definition and I was searching for 8 to 45, so they looked like my best source, and they were being dropped while the log printed 40 results, 0 in band, which looks exactly like a source with nothing good in it.

I let unknown-duration entries through to the full metadata fetch, and the candidate pool went from 81 to 120 per run, with 46 Shorts on the first run. That looked like a clear win, but Shorts are portrait. A portrait clip renders as a tall sliver in the top half of the reel, with line art you can't read, and three of them reached the channel before I noticed. When I checked afterwards, 20 of 20 Shorts from one channel were 608×1080.

Now every clip has to be landscape, with an aspect ratio of at least 1.2, checked on the metadata and again on the downloaded file, so those Shorts all get rejected. The pool grew, but the usable pool didn't. There had been a minimum-width check the whole time, and a 608×1080 Short passes it easily.

Nothing joined to anything

Performance data was keyed by Instagram's re-encoded copy (ig_<media_id>), source data by render tag (auto_s03e09), and nothing joined them. So I couldn't answer the question I'd built the whole thing for: does the original's view count predict anything?

Duration looked like the obvious join key, but it only matched 26 of 68, and tightening the tolerance made it worse (12 matches at ±0.05 s), because Instagram's re-encode shifts the duration by more than a frame.

What worked was dialogue. Transcribe both sides, match on n-gram overlap, and require a margin over the runner-up so near-ties get recorded as ambiguous instead of guessed. 56 of 68 linked, with median overlap 0.95, median margin 0.94, zero ambiguous.

The limit that doesn't say "quota"

YouTube has two separate daily limits: an API project quota and a per-channel upload cap around 26 a day. The second comes back as HTTP 400 with the word "quota" nowhere in it. My handler checked for "quota", missed, and burned 30 doomed uploads in a row.

How long a clip can safely be

2 of 140 uploads got Content-ID blocked, and both were over 89 seconds. Nothing under 80 seconds was ever flagged. So I capped clips at 45 seconds, which costs nothing: the entire top ten by plays is under 32 seconds anyway.

07

The picker that didn't work

The original plan was a model that picks which clips to render, ranked by predicted views. I built it and it doesn't predict well enough to use. Here's why.

The retention model does hold up out of sample, with a cross-validated R² of 0.49. The problem is it's duration wearing a hat, and duration doesn't separate winners from losers: the top ten posts by plays run 8 to 32 seconds, the bottom ten 6 to 40.

Four correlations across 68 posts. Ranking clips by predicted retention would order them about as well as shuffling them.
Correlation with plays, and what happens if you try to predict them directly
RelationshiprWhat that means
Predicted retention → plays +0.02 Nothing at all, not even a weak signal
Measured retention → plays+0.40Real, but I only get it after posting
Duration → plays−0.15Noise
Duration → retention−0.74Strong, which is why the model is really just duration

Predicting plays directly is worse than useless. That model's cross-validated R² is −0.89, so I'd have done better guessing the average every time. And the part of retention that does predict plays is only measurable once the post is live, which is too late to decide what to render.

So it doesn't forecast anything. It filters on length, on shape, on whether I can caption it, and on whether I've already seen it, which is where the real work turned out to be. It records one guess, how many views the original uploader got, labelled as an untested hypothesis with the test written and currently reporting n = 0. Past that it does volume: the hit rate is about 4.9%, so ten clips is roughly a 40% shot at one that lands.
08

The same idea in Excel

Desmos can only draw lines, which is why most of this page is about tracing. Excel will fill a cell with any colour, so I tried the same thing there, and the tracing went away.

vid2excel turns a short clip into a colour animation that really runs inside a workbook, then puts it in the same 9:16 layout as the Desmos reels, real Excel on top and the clip underneath. The whole image pipeline is decode, crop, shrink to the cell grid, cut down to a fixed palette, and write the workbook. It's about 1,300 lines of Python, in its own repo.

The workbook has three sheets. Grid is 120 by 68 cells, one per pixel, and each one holds a formula that looks up its own colour index. Data has a row per cell and a column per distinct frame. Map says which column draws each frame. Type a frame number into A1 and all 8,160 formulas recalculate.

A formula can't set a fill colour, so the colour comes from conditional formatting: one rule per palette colour across the whole grid, each set to stop once it matches. The numbers in the cells are hidden with the ;;; number format.

Square cells aren't what the spec says

Excel measures column width in characters and row height in points. The documented conversion between them (ECMA-376 18.3.1.13) is a Windows 96-dpi convention, and on my Mac it gave 9 by 12 pt cells, a picture stretched 33% tall. So I wrote known widths into a sheet and measured what Excel drew.

Column width attribute vs rendered width, Excel 16.112.1 on macOS 26
Width attributeRendered
1.06 pt
2.012 pt
4.024 pt
12.072 pt

All eight widths I tried land exactly on 6 × width, straight through zero, while row height renders 1:1. Asking Excel doesn't settle it either: its own Range.Width says the cell is exactly as wide as it is tall, while it's drawing 10.4 by 12.0 device pixels. Since that constant only holds for one build, font and display, the pipeline captures one frame, measures the picture Excel actually drew, and rewrites the workbook if the cells are more than 2% off square. On my machine it measures 0.2% and leaves it alone.

Conditional formatting is the slow part

Time to change the frame and finish redrawing, 8,160 cells, worst case
Colour rulesRecalc + formattingLive fps in Excel
0 (formulas only)35 ms28.9
16136 ms7.4
32 default211 ms4.7

That's about 5.5 ms per rule, five times what the formulas cost. Real footage beats the worst case (153 ms with 32 rules), because neighbouring cells share colours and most of them stop at an early rule.

I kept 32 colours anyway. The capture waits for every frame, so rule count doesn't change the reel at all, and someone scrubbing the file by hand only loses the difference between 6.5 and 7.4 fps. The extra colours make the picture better. Mean error drops from 9.30 to 6.66 out of 255, and at 16 colours Peter loses his eyes and mouth to his skin tone.

One palette for the whole clip

A new palette per frame strobes. The colour clusters drift a little every frame, so flat areas shimmer even where nothing moved. So the palette is fitted once, with k-means in Lab colour space, over pixels sampled from every frame. On top of that, a pixel keeps last frame's colour unless another one beats it by more than a threshold τ. That separates changes that stick into the next frame (motion) from ones that flip back (flicker).

How much of each kind of change survives, by threshold (Lab units)
τMotion keptFlicker kept
0100%100%
1 default96.8%81%
294.4%71.5%
490.8%59.4%

The default sits under the smallest colour difference people can see (about 2.3 in Lab), because losing real motion is a worse mistake than leaving a little sparkle in.

The screenshot bug, again

Section 04's bug was screenshotting before the graph finished drawing. Excel has the same problem, and it's harder to catch: a window that hasn't repainted yet is perfectly still, so "wait until it's stable" passes instantly. The capture waits for the pixels to change from a snapshot taken before the update, and only then waits for them to settle. The 11-second test reel came out at 166 frames, 655 ms a frame, with none missed.

Two other things cost me a lot of time. macOS's screencapture won't write a file whose name starts with a dot, and it exits 0 anyway, which looks like a missing Screen Recording permission. And the first scripted document command after Excel launches takes about 108 seconds at 0% CPU with no dialog. Every command after that takes about 0.1 s.

09

Still broken

  • There's only about a week of data, from one account and one show. The correlations up there come from a tiny sample of one very specific kind of video, and none of it generalises.
  • The source-popularity signal is untested. The test is written and reports n = 0. I have not shown it works.
  • Instagram posting is still manual. YouTube is fully automatic. The platform that produced all 12,837 hours is the one I still upload by hand.
  • yt-dlp is a single point of failure. Any time YouTube changes something, harvest breaks. At least it breaks loudly and skips the run instead of posting garbage.
  • The more it posts, the more Content-ID hits I take. Two of 140 got blocked so far. The 45-second cap helps, but it doesn't make the problem go away.