Skip to content

Commit

Permalink
chore: fix SABR example and update protos
Browse files Browse the repository at this point in the history
+ SABR now requires PoTokens.
  • Loading branch information
LuanRT committed Dec 23, 2024
1 parent 7a106dc commit d683f12
Show file tree
Hide file tree
Showing 14 changed files with 792 additions and 230 deletions.
8 changes: 6 additions & 2 deletions examples/downloader/ffmpeg-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { WriteStream } from 'node:fs';
import { createWriteStream, unlink } from 'node:fs';
import { Innertube, UniversalCache } from 'youtubei.js';
import GoogleVideo, { type Format } from '../../dist/src/index.js';
import { generateWebPoToken } from './utils.js';

const progressBars = new cliProgress.MultiBar({
stopOnComplete: true,
Expand All @@ -13,6 +14,7 @@ const progressBars = new cliProgress.MultiBar({
const formatProgressBars = new Map<string, cliProgress.SingleBar>();

const innertube = await Innertube.create({ cache: new UniversalCache(true) });
const webPoTokenResult = await generateWebPoToken(innertube.session.context.client.visitorData || '');
const info = await innertube.getBasicInfo('wRNnMQEKo7o');

console.info(`
Expand All @@ -21,6 +23,7 @@ console.info(`
Views: ${info.basic_info.view_count}
Author: ${info.basic_info.author}
Video ID: ${info.basic_info.id}
WebPoToken: ${webPoTokenResult.poToken}
`);

const durationMs = (info.basic_info?.duration ?? 0) * 1000;
Expand Down Expand Up @@ -59,6 +62,7 @@ if (!serverAbrStreamingUrl)

const serverAbrStream = new GoogleVideo.ServerAbrStream({
fetch: innertube.session.http.fetch_function,
poToken: webPoTokenResult.poToken,
serverAbrStreamingUrl,
videoPlaybackUstreamerConfig: videoPlaybackUstreamerConfig,
durationMs
Expand Down Expand Up @@ -121,8 +125,8 @@ await serverAbrStream.init({
audioFormats: [ selectedAudioFormat ],
videoFormats: [ selectedVideoFormat ],
clientAbrState: {
startTimeMs: 0,
mediaType: 0 // 0 = BOTH, 1 = AUDIO (video-only is no longer supported by YouTube)
playerTimeMs: 0,
enabledTrackTypesBitfield: 0 // 0 = BOTH, 1 = AUDIO (video-only is no longer supported by YouTube)
}
});

Expand Down
13 changes: 8 additions & 5 deletions examples/downloader/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { WriteStream } from 'node:fs';
import { createWriteStream } from 'node:fs';
import { Innertube, UniversalCache } from 'youtubei.js';
import GoogleVideo, { type Format } from '../../dist/src/index.js';
import { generateWebPoToken } from './utils.js';

const progressBars = new cliProgress.MultiBar({
stopOnComplete: true,
Expand All @@ -12,6 +13,7 @@ const progressBars = new cliProgress.MultiBar({
const formatProgressBars = new Map<string, cliProgress.SingleBar>();

const innertube = await Innertube.create({ cache: new UniversalCache(true) });
const webPoTokenResult = await generateWebPoToken(innertube.session.context.client.visitorData || '');
const info = await innertube.getBasicInfo('mzqO7oKTJKI');

console.info(`
Expand All @@ -20,6 +22,7 @@ console.info(`
Views: ${info.basic_info.view_count}
Author: ${info.basic_info.author}
Video ID: ${info.basic_info.id}
WebPoToken: ${webPoTokenResult.poToken}
`);

const durationMs = (info.basic_info?.duration ?? 0) * 1000;
Expand Down Expand Up @@ -66,12 +69,12 @@ const determineFileExtension = (mimeType: string) => {
const getOutputStream = (isVideo: boolean, mimeType: string, formatId?: number) => {
const type = isVideo ? 'video' : 'audio';
const extension = determineFileExtension(mimeType);
const stream = createWriteStream(`${sanitizedTitle}.${formatId}.${type}.${extension}`);
return stream;
return createWriteStream(`${sanitizedTitle}.${formatId}.${type}.${extension}`);
};

const serverAbrStream = new GoogleVideo.ServerAbrStream({
fetch: innertube.session.http.fetch_function,
poToken: webPoTokenResult.poToken,
serverAbrStreamingUrl,
videoPlaybackUstreamerConfig: videoPlaybackUstreamerConfig,
durationMs
Expand Down Expand Up @@ -126,12 +129,12 @@ serverAbrStream.on('error', (error) => {
console.error(error);
});

await serverAbrStream.init({
await serverAbrStream.init({
audioFormats: [ selectedAudioFormat ],
videoFormats: [ selectedVideoFormat ],
clientAbrState: {
startTimeMs: 0,
mediaType: 0 // 0 = BOTH, 1 = AUDIO (video-only is no longer supported by YouTube)
playerTimeMs: 0,
enabledTrackTypesBitfield: 0 // 0 = BOTH, 1 = AUDIO (video-only is no longer supported by YouTube)
}
});

Expand Down
Loading

0 comments on commit d683f12

Please sign in to comment.