-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
180 lines (151 loc) · 5.41 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Innertube, YT } from 'youtubei.js/web';
import { parseArgs } from 'util';
import {
formatSize,
audioFormatToString,
videoFormatToString,
getAudioFormat,
getVideoFormat
} from './format';
import asyncPool from 'tiny-async-pool';
const { values: args } = parseArgs({
args: Bun.argv,
allowPositionals: true,
strict: true,
options: {
channel: {
'type': 'string',
},
'gen-script-template': {
'type': 'string',
'short': 'g',
},
plugin: {
'type': 'string',
'short': 'p',
},
'streams': {
'type': 'boolean',
'short': 's',
}
}
});
if (args['gen-script-template']) {
await createPluginScript(args['gen-script-template']);
process.exit(0);
}
if (!args.channel) throw Error("Channel not specified");
const youtube = await Innertube.create();
const channel = await youtube.getChannel(args.channel);
if (!channel) throw Error("Channel not found");
const feedFunc = args.streams ?
channel.getLiveStreams.bind(channel) :
channel.getVideos.bind(channel);
type StreamFeed = Awaited<ReturnType<typeof feedFunc>> | YT.ChannelListContinuation;
type FeedVideo = StreamFeed['videos'][number];
const feedVids: FeedVideo[] = [];
let feed: StreamFeed = await feedFunc();
let videoChunk: FeedVideo[] = feed.videos;
while (videoChunk.length > 0) {
feedVids.push(...videoChunk);
try {
feed = await feed.getContinuation();
} catch {
videoChunk = [];
break;
}
videoChunk = feed.videos;
}
const availableVids = feedVids.filter(video => {
return (!video.key('is_upcoming').isBoolean() || !video.key('is_upcoming').boolean()) &&
(!video.key('is_live').isBoolean() || !video.key('is_live').boolean());
});
console.log(`Found ${availableVids.length} available videos`);
const videos = asyncPool(10, availableVids, async video => youtube.getInfo(video.key('id').string()));
const plugin = args.plugin ? await import(args.plugin) : null;
let size = 0;
for await (const info of videos) {
const formats = [
...(info.streaming_data?.formats ?? []),
...(info.streaming_data?.adaptive_formats ?? []),
];
let videoFunc = plugin?.getVideoFormat ?? getVideoFormat;
let audioFunc = plugin?.getAudioFormat ?? getAudioFormat;
const format = videoFunc(formats);
if (!format) {
console.log(`No video format found for ${info.basic_info.id}`);
continue;
}
const audioFormat = audioFunc(formats);
if (!audioFormat) {
console.log(`No audio format found for ${info.basic_info.id}`);
continue;
}
console.log(`Video format for ${info.basic_info.id}: ${videoFormatToString(format)}`);
console.log(`Audio format for ${info.basic_info.id}: ${audioFormatToString(audioFormat)}`);
const videoSize = formatSize(format);
const audioSize = formatSize(audioFormat);
console.log(`Video size: ${videoSize} bytes`);
console.log(`Audio size: ${audioSize} bytes`);
const totalSize = videoSize + audioSize;
const totalSizeGB = totalSize / 1024 / 1024 / 1024;
console.log(`Total size: ${totalSize} bytes (${totalSizeGB} GB)`);
if (Number.isNaN(totalSize)) {
console.log(`Invalid size for ${info.basic_info.id}`);
continue;
}
size += totalSize;
}
const totalSizeGB = size / 1024 / 1024 / 1024;
console.log(`Total size for all videos: ${size} bytes (${totalSizeGB} GB)`);
async function createPluginScript(outpath: string) {
const content = `
/**
* This function will override the default video format selector
* and will select the highest quality video format available.
*
* @typedef {Object} Format
* @property {number} itag
* @property {string} mime_type
* @property {boolean} is_type_otf
* @property {number} bitrate
* @property {number | undefined} average_bitrate
* @property {number} width
* @property {number} height
* @property {{ start: number, end: number } | undefined} init_range
* @property {{ start: number, end: number } | undefined} index_range
* @property {Date} last_modified
* @property {number | undefined} content_length
* @property {string | undefined} quality
* @property {string | undefined} quality_label
* @property {number | undefined} fps
* @property {string | undefined} url
* @property {string | undefined} cipher
* @property {string | undefined} signature_cipher
* @property {string | undefined} audio_quality
* @property {{ audio_is_default: boolean, display_name: string, id: string } | undefined} audio_track
* @property {number} approx_duration_ms
* @property {number | undefined} audio_sample_rate
* @property {number | undefined} audio_channels
* @property {number | undefined} loudness_db
* @property {boolean} has_audio
* @property {boolean} has_video
* @property {string | null | undefined} language
* @property {boolean | undefined} is_dubbed
* @property {boolean | undefined} is_descriptive
* @property {boolean | undefined} is_original
* @property {{ primaries: string | undefined, transfer_characteristics: string | undefined, matrix_coefficients: string | undefined } | undefined} color_info
*
* @typedef {(formats: Format[]) => Format | null} FormatSelector
**/
/** @type {FormatSelector} */
function getVideoFormat(formats) {
return null;
}
/** @type {FormatSelector} */
function getAudioFormat(formats) {
return null;
}
`
await Bun.write(outpath, content);
}