mirror of
https://github.com/imputnet/cobalt.git
synced 2025-06-28 09:28:29 +00:00
api/stream/internal: stream vk videos in chunks
This commit is contained in:
parent
599ec9dd92
commit
44f4ea32c6
@ -6,6 +6,8 @@ import { handleHlsPlaylist, isHlsResponse, probeInternalHLSTunnel } from "./inte
|
|||||||
const CHUNK_SIZE = BigInt(8e6); // 8 MB
|
const CHUNK_SIZE = BigInt(8e6); // 8 MB
|
||||||
const min = (a, b) => a < b ? a : b;
|
const min = (a, b) => a < b ? a : b;
|
||||||
|
|
||||||
|
const serviceNeedsChunks = ["youtube", "vk"];
|
||||||
|
|
||||||
async function* readChunks(streamInfo, size) {
|
async function* readChunks(streamInfo, size) {
|
||||||
let read = 0n, chunksSinceTransplant = 0;
|
let read = 0n, chunksSinceTransplant = 0;
|
||||||
while (read < size) {
|
while (read < size) {
|
||||||
@ -15,7 +17,7 @@ async function* readChunks(streamInfo, size) {
|
|||||||
|
|
||||||
const chunk = await request(streamInfo.url, {
|
const chunk = await request(streamInfo.url, {
|
||||||
headers: {
|
headers: {
|
||||||
...getHeaders('youtube'),
|
...getHeaders(streamInfo.service),
|
||||||
Range: `bytes=${read}-${read + CHUNK_SIZE}`
|
Range: `bytes=${read}-${read + CHUNK_SIZE}`
|
||||||
},
|
},
|
||||||
dispatcher: streamInfo.dispatcher,
|
dispatcher: streamInfo.dispatcher,
|
||||||
@ -48,7 +50,7 @@ async function* readChunks(streamInfo, size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleYoutubeStream(streamInfo, res) {
|
async function handleChunkedStream(streamInfo, res) {
|
||||||
const { signal } = streamInfo.controller;
|
const { signal } = streamInfo.controller;
|
||||||
const cleanup = () => (res.end(), closeRequest(streamInfo.controller));
|
const cleanup = () => (res.end(), closeRequest(streamInfo.controller));
|
||||||
|
|
||||||
@ -56,7 +58,7 @@ async function handleYoutubeStream(streamInfo, res) {
|
|||||||
let req, attempts = 3;
|
let req, attempts = 3;
|
||||||
while (attempts--) {
|
while (attempts--) {
|
||||||
req = await fetch(streamInfo.url, {
|
req = await fetch(streamInfo.url, {
|
||||||
headers: getHeaders('youtube'),
|
headers: getHeaders(streamInfo.service),
|
||||||
method: 'HEAD',
|
method: 'HEAD',
|
||||||
dispatcher: streamInfo.dispatcher,
|
dispatcher: streamInfo.dispatcher,
|
||||||
signal
|
signal
|
||||||
@ -146,8 +148,8 @@ export function internalStream(streamInfo, res) {
|
|||||||
streamInfo.headers.delete('icy-metadata');
|
streamInfo.headers.delete('icy-metadata');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (streamInfo.service === 'youtube' && !streamInfo.isHLS) {
|
if (serviceNeedsChunks.includes(streamInfo.service) && !streamInfo.isHLS) {
|
||||||
return handleYoutubeStream(streamInfo, res);
|
return handleChunkedStream(streamInfo, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
return handleGenericStream(streamInfo, res);
|
return handleGenericStream(streamInfo, res);
|
||||||
|
Loading…
Reference in New Issue
Block a user