Endpoints
/stream
Streams one format for playback. It is built like /dl, with two differences: it answers byte ranges, so players can seek, and it sends no attachment header, so browsers play the file instead of saving it.
Take the streamUrl of a format from the /player response. Its parameters
are the same as for /dl: s, vid and key.
In a video tag
<video controls preload="metadata"
src="https://api.ytbro.net/stream?s=AWf3k9Qm…&key=YOUR_KEY&vid=aqz-KE-bpKQ">
</video>
The browser sends range requests on its own while the user plays and seeks.
YouTube serves picture and sound as separate tracks. A video streamUrl plays without sound,
an audio streamUrl without picture. For both in one player, use /dash.
For audio only, an <audio> tag with an M4A streamUrl is all you need.
Ranges
| Request header | Response |
|---|---|
| none | 200, whole file |
Range: bytes=0-999 | 206, bytes 0 to 999 |
Range: bytes=80000000- | 206, from byte 80,000,000 to the end |
Range: bytes=-1000 | 206, the last 1,000 bytes |
Range: bytes=0-499, 1000-1499 | 206, whole file |
| range starting past the end | 416 Range Not Satisfiable |
| malformed range | 416 Range Not Satisfiable |
A 206 carries Content-Range and Content-Length for the part, and every successful
response has Accept-Ranges: bytes. A range that ends past the file is cut to the file.
Several ranges in one request are answered with the whole file as one 206. A 416 has an
empty body and Content-Range: bytes */<size>, so you can read the size from it.
HTTP/2 206
content-type: video/mp4; codecs="avc1.640028"
content-length: 78614275
content-range: bytes 80000000-158614274/158614275
accept-ranges: bytes
# First kilobyte only
curl -H "Range: bytes=0-1023" -o head.bin "$STREAM_URL"const res = await fetch(format.streamUrl, { headers: { Range: 'bytes=0-1023' } });
console.log(res.status, res.headers.get('content-range')); // 206 bytes 0-1023/10271433
const head = new Uint8Array(await res.arrayBuffer());r = requests.get(fmt["streamUrl"], headers={"Range": "bytes=0-1023"}, timeout=30)
print(r.status_code, r.headers["Content-Range"]) # 206 bytes 0-1023/10271433
head = r.contentlet res = reqwest::Client::new()
.get(stream_url)
.header(reqwest::header::RANGE, "bytes=0-1023")
.send()
.await?
.error_for_status()?;
println!("{} {:?}", res.status(), res.headers().get(reqwest::header::CONTENT_RANGE));
let head = res.bytes().await?;<!-- Browsers handle ranges by themselves. -->
<audio controls src="https://api.ytbro.net/stream?s=AQm8sV3n…&key=YOUR_KEY&vid=aqz-KE-bpKQ"></audio>Cross origin
/stream sends Access-Control-Allow-Origin: * and answers preflight requests, so players
that load media through fetch (dash.js, shaka-player, hls.js style MSE players) work from
any site. Your key still protects it.
When a link goes stale
Links stay valid for about 6 hours. The exact lifetime is in expiresInSeconds of the
/player response; after that, call /player again and use the new links.
If YouTube has re-encoded the file since you called /player, a range request answers
410 Gone: bytes from two versions would not fit together. A request without a range gets
the new version. If the format is gone altogether, the answer is 404. In both cases call
/player again and use the new link.
Billing
Each request to /stream is billed at PRICE. A player usually sends more than one range
request per playback: at least one at the start and one for every seek. Each of those counts.