prevent crash if youtube video is fucked up

ill remake a youtube function to pick a good video instead of broken one later, i just dont want cobalt to hang if one youtube cdn is down

i hate youtube so much
This commit is contained in:
wukko 2022-08-14 23:09:06 +06:00
parent 7ff5f56538
commit a5e081e2bf
3 changed files with 34 additions and 22 deletions

View file

@ -81,7 +81,7 @@ if (fs.existsSync('./.env')) {
res.status(200).json({ "status": "continue" }); res.status(200).json({ "status": "continue" });
} else if (req.query.t) { } else if (req.query.t) {
let ip = req.header('x-forwarded-for') ? req.header('x-forwarded-for') : req.ip let ip = req.header('x-forwarded-for') ? req.header('x-forwarded-for') : req.ip
stream(res, ip, req.query.t, req.query.h, req.query.e, languageCode(req)); stream(res, ip, req.query.t, req.query.h, req.query.e);
} else { } else {
let j = apiJSON(0, { t: loc(languageCode(req), 'ErrorNoStreamID') }) let j = apiJSON(0, { t: loc(languageCode(req), 'ErrorNoStreamID') })
res.status(j.status).json(j.body); res.status(j.status).json(j.body);

View file

@ -2,7 +2,7 @@ import { apiJSON } from "../sub/utils.js";
import { verifyStream } from "./manage.js"; import { verifyStream } from "./manage.js";
import { streamAudioOnly, streamDefault, streamLiveRender } from "./types.js"; import { streamAudioOnly, streamDefault, streamLiveRender } from "./types.js";
export default function(res, ip, id, hmac, exp, lang) { export default function(res, ip, id, hmac, exp) {
try { try {
let streamInfo = verifyStream(ip, id, hmac, exp, process.env.streamSalt); let streamInfo = verifyStream(ip, id, hmac, exp, process.env.streamSalt);
if (!streamInfo.error) { if (!streamInfo.error) {
@ -11,10 +11,10 @@ export default function(res, ip, id, hmac, exp, lang) {
} else { } else {
switch (streamInfo.type) { switch (streamInfo.type) {
case "render": case "render":
streamLiveRender(streamInfo, res, lang); streamLiveRender(streamInfo, res);
break; break;
default: default:
streamDefault(streamInfo, res, lang); streamDefault(streamInfo, res);
break; break;
} }
} }

View file

@ -3,8 +3,6 @@ import ffmpeg from "ffmpeg-static";
import got from "got"; import got from "got";
import { ffmpegArgs, genericUserAgent } from "../config.js"; import { ffmpegArgs, genericUserAgent } from "../config.js";
import { msToTime } from "../sub/utils.js"; import { msToTime } from "../sub/utils.js";
import { internalError } from "../sub/errors.js";
import loc from "../../localization/manager.js";
export async function streamDefault(streamInfo, res) { export async function streamDefault(streamInfo, res) {
try { try {
@ -16,16 +14,16 @@ export async function streamDefault(streamInfo, res) {
isStream: true isStream: true
}); });
stream.pipe(res).on('error', (err) => { stream.pipe(res).on('error', (err) => {
internalError(res); res.end();
}); });
stream.on('error', (err) => { stream.on('error', (err) => {
internalError(res); res.end();
}); });
} catch (e) { } catch (e) {
internalError(res); res.end();
} }
} }
export async function streamLiveRender(streamInfo, res, lang) { export async function streamLiveRender(streamInfo, res) {
try { try {
if (streamInfo.urls.length == 2) { if (streamInfo.urls.length == 2) {
let headers = {}; let headers = {};
@ -51,28 +49,39 @@ export async function streamLiveRender(streamInfo, res, lang) {
'pipe', 'pipe', 'pipe' 'pipe', 'pipe', 'pipe'
], ],
}); });
ffmpegProcess.on('error', (err) => {
ffmpegProcess.kill();
});
audio.on('error', (err) => {
ffmpegProcess.kill();
});
video.on('error', (err) => {
ffmpegProcess.kill();
});
res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}"`); res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}"`);
ffmpegProcess.stdio[5].pipe(res); ffmpegProcess.stdio[5].pipe(res);
ffmpegProcess.on('error', (err) => {
ffmpegProcess.kill();
res.end();
return;
});
video.pipe(ffmpegProcess.stdio[3]).on('error', (err) => { video.pipe(ffmpegProcess.stdio[3]).on('error', (err) => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.end();
return;
}); });
audio.pipe(ffmpegProcess.stdio[4]).on('error', (err) => { audio.pipe(ffmpegProcess.stdio[4]).on('error', (err) => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.end();
return;
});
audio.on('error', (err) => {
ffmpegProcess.kill();
res.end();
return;
});
video.on('error', (err) => {
ffmpegProcess.kill();
res.end();
return;
}); });
} else { } else {
res.status(400).json({ status: "error", text: loc(lang, 'ErrorCorruptedStream') }); res.end();
} }
} catch (e) { } catch (e) {
internalError(res); res.end();
} }
} }
export async function streamAudioOnly(streamInfo, res) { export async function streamAudioOnly(streamInfo, res) {
@ -100,16 +109,19 @@ export async function streamAudioOnly(streamInfo, res) {
}); });
ffmpegProcess.on('error', (err) => { ffmpegProcess.on('error', (err) => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.end();
}); });
audio.on('error', (err) => { audio.on('error', (err) => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.end();
}); });
res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}.${streamInfo.audioFormat}"`); res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}.${streamInfo.audioFormat}"`);
ffmpegProcess.stdio[4].pipe(res); ffmpegProcess.stdio[4].pipe(res);
audio.pipe(ffmpegProcess.stdio[3]).on('error', (err) => { audio.pipe(ffmpegProcess.stdio[3]).on('error', (err) => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.end();
}); });
} catch (e) { } catch (e) {
internalError(res); res.end();
} }
} }