metadata/index.ts

272 lines
8.4 KiB
TypeScript
Raw Normal View History

2025-08-01 18:00:38 +00:00
import express from 'express';
import { Innertube } from 'youtubei.js'
import * as fs from 'node:fs'
import { EnabledTrackTypes } from 'googlevideo/utils';
import {
createOutputStream,
createStreamSink,
createSabrStream,
} from './utils/sabr-stream-factory.js';
import type { SabrPlaybackOptions } from 'googlevideo/sabr-stream';
2024-06-29 11:20:18 +00:00
const ffmpeg = require('fluent-ffmpeg')
const ffmpegStatic = require('ffmpeg-static')
2025-08-01 18:00:38 +00:00
const app = express();
2024-06-29 11:20:18 +00:00
require('express-ws')(app)
ffmpeg.setFfmpegPath(ffmpegStatic)
2024-04-04 20:09:04 +00:00
const maxRetries = 5
2025-03-14 20:41:15 +00:00
const platforms = ['IOS', 'ANDROID', 'YTSTUDIO_ANDROID', 'YTMUSIC_ANDROID']
2024-04-04 20:09:04 +00:00
2024-04-08 21:43:21 +00:00
app.get('/health', async (req, res) => {
2025-01-29 09:11:30 +00:00
try {
const urls = ['/video/sRMMwpDTs5k', '/channel/UCRijo3ddMTht_IHyNSNXpNQ', '/videos/UCRijo3ddMTht_IHyNSNXpNQ']
const results = await Promise.all(urls.map(async (url) => {
const response = await fetch(`http://localhost:8008${url}`);
2025-08-01 18:00:38 +00:00
const jsonData: any = await response.json();
2025-01-29 09:11:30 +00:00
const status = jsonData.error ? 'unhealthy' : 'healthy';
return { url, status };
}));
console.log('Health check results:', results);
const isHealthy = results.every(result => result.status === 'healthy');
if (isHealthy) {
res.status(200).json({ message: 'All endpoints are healthy', results });
} else {
res.status(500).json({ error: 'Health check failed', results });
2025-03-26 17:25:52 +00:00
switchIps()
2024-04-08 21:43:21 +00:00
}
2025-08-01 18:00:38 +00:00
} catch (error:any) {
2025-01-29 09:11:30 +00:00
console.error('Health check failed:', error.message);
2025-03-26 17:25:52 +00:00
switchIps()
2025-01-29 09:11:30 +00:00
res.status(500).json({ error: 'Health check failed', results: [], errorMessage: error.message });
}
2024-04-08 21:43:21 +00:00
})
2024-04-04 20:09:04 +00:00
app.get('/video/:id', async (req, res) => {
2025-01-29 09:11:30 +00:00
let error = ''
for (let retries = 0; retries < maxRetries; retries++) {
try {
const platform = platforms[retries % platforms.length];
const yt = await Innertube.create();
2025-08-01 18:00:38 +00:00
const info = await yt.getInfo(req.params.id, { // @ts-ignore
client: platform
});
2025-01-29 09:11:30 +00:00
if (!info) {
error = 'ErrorCantConnectToServiceAPI'
continue;
}
2025-08-01 18:00:38 +00:00
if (info.playability_status!.status !== 'OK') {
2025-01-29 09:11:30 +00:00
error = 'ErrorYTUnavailable'
continue;
}
if (info.basic_info.is_live) {
error = 'ErrorLiveVideo'
continue;
}
if (info.basic_info.title == 'Video Not Available') {
error = 'YoutubeIsFuckingWithMe'
continue;
}
return res.json(info)
} catch (error) {
continue
2024-04-04 20:09:04 +00:00
}
2025-01-29 09:11:30 +00:00
}
2024-04-04 20:09:04 +00:00
2025-01-29 09:11:30 +00:00
res.json({ error: error || 'ErrorUnknown' })
2024-04-04 20:09:04 +00:00
})
app.get('/channel/:id', async (req, res) => {
2025-01-29 09:11:30 +00:00
let error = ''
for (let retries = 0; retries < maxRetries; retries++) {
try {
const yt = await Innertube.create();
2025-08-01 18:00:38 +00:00
const info = await yt.getChannel(req.params.id);
2025-01-29 09:11:30 +00:00
if (!info) {
error = 'ErrorCantConnectToServiceAPI'
continue;
}
return res.json(info)
} catch (error) {
continue
2024-04-04 20:09:04 +00:00
}
2025-01-29 09:11:30 +00:00
}
2024-04-04 20:09:04 +00:00
2025-01-29 09:11:30 +00:00
res.json({ error: error || 'ErrorUnknown' })
2024-04-04 20:09:04 +00:00
})
app.get('/videos/:id', async (req, res) => {
2025-01-29 09:11:30 +00:00
try {
const videos = [];
const yt = await Innertube.create();
const channel = await yt.getChannel(req.params.id);
let json = await channel.getVideos();
videos.push(...json.videos);
while (json.has_continuation && videos.length < 60) {
json = await getNextPage(json);
videos.push(...json.videos);
2024-04-04 20:09:04 +00:00
}
2025-01-29 09:11:30 +00:00
return res.json(videos)
} catch (e) {
res.json(false)
}
2025-08-01 18:00:38 +00:00
async function getNextPage(json: any) {
2025-01-29 09:11:30 +00:00
const page = await json.getContinuation();
return page;
}
2024-04-04 20:09:04 +00:00
})
2025-08-01 18:00:38 +00:00
// @ts-ignore
2024-06-29 11:20:18 +00:00
app.ws('/download/:id/:quality', async (ws, req) => {
2025-03-14 21:28:58 +00:00
const yt = await Innertube.create();
2025-08-01 18:00:38 +00:00
const info = await yt.getInfo(req.params.id, {
client: 'IOS'
});
2025-01-29 09:11:30 +00:00
2025-08-01 18:00:38 +00:00
if (info.playability_status?.status !== 'OK') {
ws.send(`This video is not available for download (${info.playability_status?.status} ${info.playability_status?.reason}).`);
return ws.close()
} else if (info.basic_info.is_live) {
ws.send('This video is live, and cannot be downloaded.');
return ws.close()
} else if (info.basic_info.id != req.params.id) {
ws.send('This video is not available for download. Youtube is serving a different video.');
return ws.close()
}
const streamOptions: SabrPlaybackOptions = {
videoQuality: req.params.quality,
audioQuality: 'AUDIO_QUALITY_LOW',
2025-08-01 18:00:38 +00:00
enabledTrackTypes: EnabledTrackTypes.VIDEO_AND_AUDIO
};
const { streamResults } = await createSabrStream(req.params.id, streamOptions);
const { videoStream, audioStream, selectedFormats } = streamResults;
const whitelistedVideos = JSON.parse(fs.readFileSync('./whitelist.json', 'utf-8'))
const videoSizeTotal = (selectedFormats.audioFormat.contentLength || 0)
+ (selectedFormats.videoFormat.contentLength || 0)
if (videoSizeTotal > (1_048_576 * 150) && !whitelistedVideos.includes(req.params.id)) {
2025-01-29 09:11:30 +00:00
ws.send('Is this content considered high risk? If so, please email me at admin@preservetube.com.');
ws.send('This video is too large, and unfortunately, Preservetube does not have unlimited storage.');
return ws.close()
2025-08-01 18:00:38 +00:00
} else if (!selectedFormats.videoFormat.contentLength) {
ws.send('Youtube isn\'t giving us enough information to be able to tell if we can process this video.')
ws.send('Please try again later.')
return ws.close()
2025-01-29 09:11:30 +00:00
}
2025-08-01 18:00:38 +00:00
const audioOutputStream = createOutputStream(req.params.id, selectedFormats.audioFormat.mimeType!);
const videoOutputStream = createOutputStream(req.params.id, selectedFormats.videoFormat.mimeType!);
2025-01-29 09:11:30 +00:00
2025-08-01 18:00:38 +00:00
await Promise.all([
videoStream.pipeTo(createStreamSink(selectedFormats.videoFormat, videoOutputStream.stream, ws, 'video')),
audioStream.pipeTo(createStreamSink(selectedFormats.audioFormat, audioOutputStream.stream, ws, 'audio'))
]);
2024-06-02 07:53:26 +00:00
2025-08-01 18:00:38 +00:00
ws.send('Downloaded video and audio. Merging them together.')
2025-01-29 09:11:30 +00:00
2025-08-01 18:00:38 +00:00
await mergeIt(audioOutputStream.filePath, videoOutputStream.filePath, `./output/${req.params.id}.mp4`, ws)
await cleanupTempFiles([ audioOutputStream.filePath, videoOutputStream.filePath ]);
2025-01-29 09:11:30 +00:00
ws.send('done')
ws.close()
2024-06-29 11:20:18 +00:00
});
2025-08-01 18:00:38 +00:00
function mergeIt(audioPath: string, videoPath: string, outputPath: string, ws: any) {
2025-01-29 09:11:30 +00:00
return new Promise((resolve, reject) => {
ffmpeg()
2025-08-01 18:00:38 +00:00
.input(videoPath)
.input(audioPath)
.outputOptions([ '-c:v copy', '-c:a copy', '-map 0:v:0', '-map 1:a:0' ])
.on('progress', (progress:any) => {
if (progress.percent) {
ws.send(`[merging] ${progress.precent}% done`)
}
})
2025-01-29 09:11:30 +00:00
.on('end', () => {
2025-08-01 18:00:38 +00:00
resolve(outputPath);
2025-01-29 09:11:30 +00:00
})
2025-08-01 18:00:38 +00:00
.on('error', (err:any) => {
reject(new Error(`Error merging files: ${err.message}`));
2025-01-29 09:11:30 +00:00
})
2025-08-01 18:00:38 +00:00
.save(outputPath);
2025-01-29 09:11:30 +00:00
});
}
2025-08-01 18:00:38 +00:00
async function cleanupTempFiles(files: string[]) {
for (const file of files) {
try {
fs.unlinkSync(file);
} catch (error) {
console.warn(`Failed to delete temp file ${file}:`, error);
}
}
}
2025-01-29 09:11:30 +00:00
async function switchIps() {
2025-08-01 18:00:38 +00:00
const currentIp: any = await (await fetch('http://localhost:8000/v1/publicip/ip', {
2025-01-29 09:11:30 +00:00
headers: {
'X-API-Key': '64d1781e469965c1cdad611b0c05d313'
}
})).json()
const currentDate = new Date()
console.log(`starting switching ips. ${currentIp.public_ip}, ${currentIp.city}, ${currentIp.region}, ${currentIp.organization}`)
const s = await fetch('http://localhost:8000/v1/vpn/status', {
method: 'PUT',
headers: {
'X-API-Key': '64d1781e469965c1cdad611b0c05d313',
'Content-Type': 'application/json'
},
body: JSON.stringify({ status: 'stopped' })
})
console.log(`stopped vpn - ${await s.text()}`)
const r = await fetch('http://localhost:8000/v1/vpn/status', {
method: 'PUT',
headers: {
'X-API-Key': '64d1781e469965c1cdad611b0c05d313',
'Content-Type': 'application/json'
},
body: JSON.stringify({ status: 'running' })
})
console.log(`turned on vpn - ${await r.text()}`)
await new Promise((resolve, reject) => {
const intervalId = setInterval(async () => {
2025-08-01 18:00:38 +00:00
const newIp: any = await (await fetch('http://localhost:8000/v1/publicip/ip', {
2025-01-29 09:11:30 +00:00
headers: {
'X-API-Key': '64d1781e469965c1cdad611b0c05d313',
},
})).json();
if (newIp.public_ip !== '') {
console.log(`finished switching ips. ${newIp.public_ip}, ${newIp.city}, ${newIp.region}, ${newIp.organization}. took ${(new Date().getTime() - currentDate.getTime()) / 1000}s`)
clearInterval(intervalId);
2025-08-01 18:00:38 +00:00
resolve('done')
2025-01-29 09:11:30 +00:00
}
}, 500);
})
2024-06-29 11:20:18 +00:00
}
2024-06-02 07:53:26 +00:00
2025-03-14 22:39:03 +00:00
setInterval(switchIps, 30 * 60000) // 30 minutes
2025-01-29 09:11:30 +00:00
2024-04-04 20:09:04 +00:00
app.listen(8008, () => {
2025-01-29 09:11:30 +00:00
console.log('the metadata server is up.')
2025-03-14 22:39:03 +00:00
switchIps()
2024-04-04 20:09:04 +00:00
})