YouTube clone

A YouTube clone is a video site you host yourself: it accepts uploads, stores the files, keeps the metadata, and streams them back on demand. The player is the easy part — what decides whether it works, and what it costs, is the three layers underneath it.

What you are actually building

Strip away the interface and there are four pieces, and only one of them is visible:

Most tutorials start with the player because it demos well. It is also the piece browsers largely handle for you: <video> already buffers, seeks and adapts. What breaks in production is invisible — an upload that dies at 90%, a bucket that quietly becomes a public file host, a seek that re-downloads the entire file.

Range requests are not optional

When you drag the playhead to the middle of a video, the browser does not re-download what came before. It sends Range: bytes=2097152- and expects a206 Partial Content back, with Content-Range telling it where this chunk sits in the whole.

Serve video without Range support and seeking either does nothing or forces a full download first — which on a 50 MB file is the difference between instant and unusable. TubeForge parses the header, reads just that slice out of R2, and returns:

HTTP/1.1 206 Partial Content
Content-Range: bytes 2097152-5242879/5242880
Accept-Ranges: bytes

The tradeoff worth knowing: every Range request is a separate Worker invocation, and the free plan allows 100,000 per day. See the cost breakdownfor where that becomes the binding constraint.

Egress is the line item that decides everything

Storage is cheap and predictable — 10 GB of video costs pennies a month, and grows only as fast as you upload. Outbound traffic is different: it grows with every view, with no ceiling. A video nobody watches costs nothing to store; a video that goes viral can cost more in a day than the file cost all year.

Serving 50 MB video × 1,000 views (50 GB out)Egress cost
Cloudflare R2$0.00
Amazon S3 (us-east-1)≈ $4.50
Self-managed VPS bandwidth≈ $0.50 – $2.50

R2 charges no egress fee at all; the S3 figure assumes roughly $0.09/GB and the VPS range typical metered bandwidth. Providers change these numbers — checkR2 pricingand S3 pricingbefore you budget against them.

This single difference is why a video project on R2 stays free as it grows, while the same project on S3 starts generating a bill the moment anyone watches.

Four decisions, and what each one costs you

There is no free lunch in this stack, only trades. These are the four that shaped TubeForge, picked because their costs land somewhere survivable:

DecisionBuys youCosts you
R2 over S3-class storageNo egress fee, so views stop being a billLocked to one vendor's ecosystem
Worker-proxied reads over a public bucketA private bucket nobody can hotlinkEvery Range request spends request quota
D1 over a key-value storeReal SQL: sort by date, sum your usageA query per request instead of an edge read
One prefix-separated bucket over twoOne binding, one set of lifecycle rulesThe read path must validate keys strictly

The metadata store is the one people get wrong. A key-value store looks simpler until the listing page needs newest-first ordering and the upload endpoint needs a running total of bytes stored — both are one SQL clause and a lot of unglue come application code otherwise.

Six things that bite people building this

Every one of these was found the hard way, and none are visible from the player:

  1. Parsing multipart instead of streaming it. A singlemultipart/form-data upload pulls the whole file into the Worker's memory, and the ceiling is 128 MB. Split it: JSON metadata first, raw bytes second.
  2. Hitting the request body cap. The Worker request body limit is 100 MB, and video clears that quickly. Note that upgrading to a paid plan does not raise it — larger uploads have to bypass the Worker entirely via a presigned URL.
  3. Assuming a slow request is an expensive one. Workers bill CPU time, not wall time, and waiting on IO costs nothing. Streaming a 50 MB body into R2 stays well inside the free plan's 10 ms budget; decoding that same body does not.
  4. Serving video without Range. Seeking silently stops working, and you will not notice until someone complains.
  5. Letting the read path accept arbitrary keys. A proxy that takes any path becomes a general-purpose object browser. TubeForge accepts exactly two segments and rejects.. outright.
  6. Trusting metadata over reality. The upload API always records a thumbnail key, but browser-side frame capture sometimes fails and no object is written. Trusting the row gives you a grid of broken images.

What TubeForge gives you

TubeForge is an open-source reference implementation of exactly those four pieces, on Cloudflare R2 + D1 + Workers with an Astro front end. It is deliberately small enough to read in an afternoon:

Limits to know before you start

LimitValue
Per upload95 MB
Total storage9 GB
Video formatsMP4, WebM
ThumbnailsJPEG, PNG
Worker requests100,000 / day (free plan)

The 95 MB ceiling comes from the Worker request body cap of 100 MB, less headroom for headers. It is a hard boundary of the runtime, not a setting you can raise.

Questions people actually ask

Is it really free to run?

Within the free allowances, yes — 10 GB of R2 storage and 100,000 Worker requests a day cover a demo easily. What ends the free ride is request volume, not traffic: because reads are proxied, playback consumes invocations that video hosts would normally spend on nothing.

How much video fits before I pay anything?

TubeForge caps total storage at 9 GB, leaving 1 GB of the free R2 allowance untouched. At the 95 MB per-upload ceiling that is about 95 videos; at 1080p bitrates it is roughly three and a half hours of footage. Past 10 GB, storage itself is cheap — about $0.015/GB-month.

Can I upload files larger than 95 MB?

Not through this Worker. The 100 MB request body cap is the same on free and paid plans, so the only way past it is to stop sending bytes through the Worker: generate a presigned URL and let the browser upload straight to R2.

Does it transcode video?

No, and this is the biggest gap between TubeForge and a real YouTube clone. Files are stored exactly as uploaded, with no re-encoding, no adaptive bitrates and no server-side thumbnails. Adding any of those means a transcode pipeline and a fundamentally larger CPU bill.

Can I use this commercially?

The code is yours to fork. But there is no user system — deletion is guarded only by a token, and total storage is the sole thing stopping strangers from filling your quota. Treat it as a reference implementation to build on, not something to put in front of customers unchanged.

Why keep the bucket private at all?

A public bucket turns your storage into someone else's free CDN the moment a URL leaks. The cost of proxying instead is measurable — every request counts against your quota — which is exactly why the cost page walks through when to switch.

Where to go next

Fork and deploy itWhat it costs to runEstimate a file's sizeTry an upload