add youtubei.js pool

This commit is contained in:
localhost 2026-05-22 16:26:16 +02:00
parent ae25cb3c0d
commit 04ab14a066
3 changed files with 89 additions and 67 deletions

View File

@ -12,6 +12,7 @@ import {
} from './utils/sabr-stream-factory.js'; } from './utils/sabr-stream-factory.js';
import type { SabrPlaybackOptions } from 'googlevideo/sabr-stream'; import type { SabrPlaybackOptions } from 'googlevideo/sabr-stream';
import { getVideoStreams, downloadStream, getInfo } from './utils/companion'; import { getVideoStreams, downloadStream, getInfo } from './utils/companion';
import { createInnertubePool } from './utils/innertube-pool.js';
const ffmpeg = require('fluent-ffmpeg') const ffmpeg = require('fluent-ffmpeg')
@ -20,7 +21,7 @@ require('express-ws')(app)
ffmpeg.setFfmpegPath('/usr/local/bin/ffmpeg') ffmpeg.setFfmpegPath('/usr/local/bin/ffmpeg')
const maxRetries = 5 const maxRetries = 5
const platforms = ['IOS', 'ANDROID', 'YTSTUDIO_ANDROID', 'YTMUSIC_ANDROID'] const innertubePool = createInnertubePool(parseInt(Bun.env.INNERTUBE_POOL_SIZE || '4'))
app.get('/health', async (req, res) => { app.get('/health', async (req, res) => {
try { try {
@ -69,56 +70,51 @@ app.get('/video/:id', async (req, res) => {
}) })
app.get('/channel/:id', async (req, res) => { app.get('/channel/:id', async (req, res) => {
let error = ''
for (let retries = 0; retries < maxRetries; retries++) {
try { try {
const yt = await Innertube.create(); const info = await innertubePool.use(async (innertube) => await innertube.getChannel(req.params.id), maxRetries)
const info = await yt.getChannel(req.params.id);
if (!info) { if (!info) {
error = 'ErrorCantConnectToServiceAPI' return res.json({ error: 'ErrorCantConnectToServiceAPI' })
continue;
}
return res.json(info)
} catch (error) {
continue
}
} }
res.json({ error: error || 'ErrorUnknown' }) return res.json(info)
} catch {
return res.json({ error: 'ErrorUnknown' })
}
}) })
app.get('/videos/:id', async (req, res) => { app.get('/videos/:id', async (req, res) => {
try { try {
let videos: any[] = []; const videos = await innertubePool.use(async (innertube) => {
for (let attempt = 0; attempt < 3; attempt++) { for (let attempt = 0; attempt < 3; attempt++) {
videos = []; const videos: any[] = []
const channel = await innertube.getChannel(req.params.id)
const yt = await Innertube.create(); let json = await channel.getVideos()
const channel = await yt.getChannel(req.params.id); videos.push(...json.videos)
let json = await channel.getVideos();
videos.push(...json.videos);
while (json.has_continuation && videos.length < 60) { while (json.has_continuation && videos.length < 60) {
json = await getNextPage(json); json = await getNextPage(json)
videos.push(...json.videos); videos.push(...json.videos)
} }
if (videos.length) break; if (videos.length) {
return videos
}
} }
return res.json(videos); return []
}, maxRetries)
return res.json(videos)
} catch (e: any) { } catch (e: any) {
console.log(e); console.log(e)
if (e.message.includes('Tab "videos" not found')) { if (e.message.includes('Tab "videos" not found')) {
return res.json([]); return res.json([])
} }
return res.json(false); return res.json(false)
} }
async function getNextPage(json: any) { async function getNextPage(json: any) {
@ -206,8 +202,11 @@ app.ws('/download/:id', async (ws, req) => {
audioQuality: 'AUDIO_QUALITY_LOW', audioQuality: 'AUDIO_QUALITY_LOW',
enabledTrackTypes: EnabledTrackTypes.VIDEO_AND_AUDIO enabledTrackTypes: EnabledTrackTypes.VIDEO_AND_AUDIO
}; };
const { streamResults } = await createSabrStream(req.params.id, streamOptions); const lease = await innertubePool.acquire()
const { videoStream, audioStream, selectedFormats } = streamResults;
try {
const { streamResults } = await createSabrStream(req.params.id, streamOptions, lease.innertube)
const { videoStream, audioStream, selectedFormats } = streamResults
const videoSizeTotal = (selectedFormats.audioFormat.contentLength || 0) const videoSizeTotal = (selectedFormats.audioFormat.contentLength || 0)
+ (selectedFormats.videoFormat.contentLength || 0) + (selectedFormats.videoFormat.contentLength || 0)
@ -230,6 +229,12 @@ app.ws('/download/:id', async (ws, req) => {
videoStream.pipeTo(createStreamSink(selectedFormats.videoFormat, videoOutputStream.stream, ws, 'video')), videoStream.pipeTo(createStreamSink(selectedFormats.videoFormat, videoOutputStream.stream, ws, 'video')),
audioStream.pipeTo(createStreamSink(selectedFormats.audioFormat, audioOutputStream.stream, ws, 'audio')) audioStream.pipeTo(createStreamSink(selectedFormats.audioFormat, audioOutputStream.stream, ws, 'audio'))
]); ]);
} catch (error) {
await lease.refresh()
throw error
} finally {
lease.release()
}
} }
if (audioOutputStream == undefined || videoOutputStream == undefined) { if (audioOutputStream == undefined || videoOutputStream == undefined) {

View File

@ -1,5 +1,5 @@
import { createWriteStream, type WriteStream } from 'node:fs'; import { createWriteStream, type WriteStream } from 'node:fs';
import { Constants, Innertube, type IPlayerResponse, UniversalCache, YTNodes } from 'youtubei.js'; import { Constants, Innertube, type IPlayerResponse, UniversalCache, YTNodes, Platform, type Types } from 'youtubei.js';
import { generateWebPoToken } from './webpo-helper.js'; import { generateWebPoToken } from './webpo-helper.js';
import type { SabrFormat } from 'googlevideo/shared-types'; import type { SabrFormat } from 'googlevideo/shared-types';
@ -9,6 +9,23 @@ import { buildSabrFormat } from 'googlevideo/utils';
import * as hr from '@tsmx/human-readable' import * as hr from '@tsmx/human-readable'
Platform.shim.eval = async (data: Types.BuildScriptResult, env: Record<string, Types.VMPrimative>) => {
const properties = [];
if(env.n) {
properties.push(`n: exportedVars.nFunction("${env.n}")`)
}
if (env.sig) {
properties.push(`sig: exportedVars.sigFunction("${env.sig}")`)
}
const code = `${data.output}\nreturn { ${properties.join(', ')} }`;
return new Function(code)();
}
export interface DownloadOutput { export interface DownloadOutput {
stream: WriteStream; stream: WriteStream;
filePath: string; filePath: string;
@ -37,7 +54,7 @@ export async function makePlayerRequest(innertube: Innertube, videoId: string, r
vis: 0, vis: 0,
splay: false, splay: false,
lactMilliseconds: '-1', lactMilliseconds: '-1',
signatureTimestamp: innertube.session.player?.sts signatureTimestamp: (innertube.session.player as { sts?: number } | undefined)?.sts
} }
}, },
contentCheckOk: true, contentCheckOk: true,
@ -118,12 +135,12 @@ export function createStreamSink(format: SabrFormat, outputStream: WriteStream,
*/ */
export async function createSabrStream( export async function createSabrStream(
videoId: string, videoId: string,
options: SabrPlaybackOptions options: SabrPlaybackOptions,
innertube: Innertube
): Promise<{ ): Promise<{
innertube: Innertube; innertube: Innertube;
streamResults: StreamResults; streamResults: StreamResults;
}> { }> {
const innertube = await Innertube.create({ cache: new UniversalCache(true) });
const webPoTokenResult = await generateWebPoToken(innertube.session.context.client.visitorData || ''); const webPoTokenResult = await generateWebPoToken(innertube.session.context.client.visitorData || '');
console.log(`debugging -> ${JSON.stringify(webPoTokenResult)}, ${videoId}`) console.log(`debugging -> ${JSON.stringify(webPoTokenResult)}, ${videoId}`)
@ -132,7 +149,7 @@ export async function createSabrStream(
const videoTitle = playerResponse.video_details?.title || 'Unknown Video'; const videoTitle = playerResponse.video_details?.title || 'Unknown Video';
// Now get the streaming information. // Now get the streaming information.
const serverAbrStreamingUrl = innertube.session.player?.decipher(playerResponse.streaming_data?.server_abr_streaming_url); const serverAbrStreamingUrl = await innertube.session.player?.decipher(playerResponse.streaming_data?.server_abr_streaming_url);
const videoPlaybackUstreamerConfig = playerResponse.player_config?.media_common_config.media_ustreamer_request_config?.video_playback_ustreamer_config; const videoPlaybackUstreamerConfig = playerResponse.player_config?.media_common_config.media_ustreamer_request_config?.video_playback_ustreamer_config;
if (!videoPlaybackUstreamerConfig) throw new Error('ustreamerConfig not found'); if (!videoPlaybackUstreamerConfig) throw new Error('ustreamerConfig not found');
@ -169,7 +186,7 @@ export async function createSabrStream(
const videoPlaybackUstreamerConfig = playerResponse.player_config?.media_common_config.media_ustreamer_request_config?.video_playback_ustreamer_config; const videoPlaybackUstreamerConfig = playerResponse.player_config?.media_common_config.media_ustreamer_request_config?.video_playback_ustreamer_config;
if (serverAbrStreamingUrl && videoPlaybackUstreamerConfig) { if (serverAbrStreamingUrl && videoPlaybackUstreamerConfig) {
serverAbrStream.setStreamingURL(serverAbrStreamingUrl); serverAbrStream.setStreamingURL(await serverAbrStreamingUrl);
serverAbrStream.setUstreamerConfig(videoPlaybackUstreamerConfig); serverAbrStream.setUstreamerConfig(videoPlaybackUstreamerConfig);
} }
}); });