api/stream: standardize stream types & clean up related functions

This commit is contained in:
wukko 2024-08-22 17:37:31 +06:00
parent 1064be6a7a
commit facf7741ce
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
11 changed files with 134 additions and 126 deletions

View file

@ -3,7 +3,6 @@ const supportedAudio = ["mp3", "ogg", "wav", "opus"];
const ffmpegArgs = { const ffmpegArgs = {
webm: ["-c:v", "copy", "-c:a", "copy"], webm: ["-c:v", "copy", "-c:a", "copy"],
mp4: ["-c:v", "copy", "-c:a", "copy", "-movflags", "faststart+frag_keyframe+empty_moov"], mp4: ["-c:v", "copy", "-c:a", "copy", "-movflags", "faststart+frag_keyframe+empty_moov"],
copy: ["-c:a", "copy"],
audio: ["-ar", "48000", "-ac", "2", "-b:a", "320k"], audio: ["-ar", "48000", "-ac", "2", "-b:a", "320k"],
m4a: ["-movflags", "frag_keyframe+empty_moov"], m4a: ["-movflags", "frag_keyframe+empty_moov"],
gif: ["-vf", "scale=-1:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse", "-loop", "0"] gif: ["-vf", "scale=-1:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse", "-loop", "0"]

View file

@ -1,9 +1,9 @@
import { supportedAudio } from "../config.js";
import { audioIgnore, services } from "./service-config.js";
import { createResponse } from "./request.js";
import createFilename from "./create-filename.js"; import createFilename from "./create-filename.js";
import { supportedAudio } from "../config.js";
import { createResponse } from "./request.js";
import { createStream } from "../stream/manage.js"; import { createStream } from "../stream/manage.js";
import { audioIgnore, services } from "./service-config.js";
export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disableMetadata, filenameStyle, twitterGif, requestIP }) { export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disableMetadata, filenameStyle, twitterGif, requestIP }) {
let action, let action,
@ -29,9 +29,9 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
if (action === "picker" || action === "audio") { if (action === "picker" || action === "audio") {
if (!r.filenameAttributes) defaultParams.filename = r.audioFilename; if (!r.filenameAttributes) defaultParams.filename = r.audioFilename;
defaultParams.isAudioOnly = true;
defaultParams.audioFormat = audioFormat; defaultParams.audioFormat = audioFormat;
} }
if (isAudioMuted && !r.filenameAttributes) { if (isAudioMuted && !r.filenameAttributes) {
defaultParams.filename = r.filename.replace('.', '_mute.') defaultParams.filename = r.filename.replace('.', '_mute.')
} }
@ -47,12 +47,12 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
break; break;
case "gif": case "gif":
params = { type: "gif" } params = { type: "gif" };
break; break;
case "m3u8": case "m3u8":
params = { params = {
type: Array.isArray(r.urls) ? "render" : "remux" type: Array.isArray(r.urls) ? "merge" : "remux"
} }
break; break;
@ -63,8 +63,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
} }
params = { params = {
type: muteType, type: muteType,
u: Array.isArray(r.urls) ? r.urls[0] : r.urls, u: Array.isArray(r.urls) ? r.urls[0] : r.urls
mute: true
} }
if (host === "reddit" && r.typeId === "redirect") if (host === "reddit" && r.typeId === "redirect")
responseType = "redirect"; responseType = "redirect";
@ -79,7 +78,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
params = { picker: r.picker }; params = { picker: r.picker };
break; break;
case "tiktok": case "tiktok":
let audioStreamType = "render"; let audioStreamType = "audio";
if (r.bestAudio === "mp3" && (audioFormat === "mp3" || audioFormat === "best")) { if (r.bestAudio === "mp3" && (audioFormat === "mp3" || audioFormat === "best")) {
audioFormat = "mp3"; audioFormat = "mp3";
audioStreamType = "proxy" audioStreamType = "proxy"
@ -94,8 +93,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
filename: r.audioFilename, filename: r.audioFilename,
isAudioOnly: true, isAudioOnly: true,
audioFormat, audioFormat,
}), })
copy: audioFormat === "best"
} }
} }
break; break;
@ -103,7 +101,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
case "video": case "video":
switch (host) { switch (host) {
case "bilibili": case "bilibili":
params = { type: "render" }; params = { type: "merge" };
break; break;
case "youtube": case "youtube":
params = { type: r.type }; params = { type: r.type };
@ -114,7 +112,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
break; break;
case "vimeo": case "vimeo":
if (Array.isArray(r.urls)) { if (Array.isArray(r.urls)) {
params = { type: "render" } params = { type: "merge" }
} else { } else {
responseType = "redirect"; responseType = "redirect";
} }
@ -153,7 +151,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
}) })
} }
let processType = "render", let processType = "audio",
copy = false; copy = false;
if (!supportedAudio.includes(audioFormat)) { if (!supportedAudio.includes(audioFormat)) {
@ -174,7 +172,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
audioFormat = serviceBestAudio; audioFormat = serviceBestAudio;
processType = "proxy"; processType = "proxy";
if (isSoundCloud || (isTiktok && audioFormat === "m4a")) { if (isSoundCloud || (isTiktok && audioFormat === "m4a")) {
processType = "render" processType = "audio"
copy = true copy = true
} }
} else if (isBestAudio && !isSoundCloud) { } else if (isBestAudio && !isSoundCloud) {
@ -189,7 +187,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
if (r.isM3U8 || host === "vimeo") { if (r.isM3U8 || host === "vimeo") {
copy = false; copy = false;
processType = "render" processType = "audio"
} }
params = { params = {

View file

@ -177,7 +177,7 @@ export default function(obj) {
** set to `same-origin`, so we need to proxy them */ ** set to `same-origin`, so we need to proxy them */
thumb: createStream({ thumb: createStream({
service: "instagram", service: "instagram",
type: "default", type: "proxy",
u: e.node?.display_url, u: e.node?.display_url,
filename: "image.jpg" filename: "image.jpg"
}) })
@ -219,7 +219,7 @@ export default function(obj) {
** set to `same-origin`, so we need to proxy them */ ** set to `same-origin`, so we need to proxy them */
thumb: createStream({ thumb: createStream({
service: "instagram", service: "instagram",
type: "default", type: "proxy",
u: imageUrl, u: imageUrl,
filename: "image.jpg" filename: "image.jpg"
}) })

View file

@ -123,7 +123,7 @@ export default async function(obj) {
return { return {
typeId: "stream", typeId: "stream",
type: "render", type: "merge",
urls: [video, audioFileLink], urls: [video, audioFileLink],
audioFilename: `reddit_${id}_audio`, audioFilename: `reddit_${id}_audio`,
filename: `reddit_${id}.mp4` filename: `reddit_${id}.mp4`

View file

@ -166,14 +166,14 @@ export default async function({ id, index, toGif, dispatcher }) {
case 1: case 1:
if (media[0].type === "photo") { if (media[0].type === "photo") {
return { return {
type: "normal", type: "proxy",
isPhoto: true, isPhoto: true,
urls: `${media[0].media_url_https}?name=4096x4096` urls: `${media[0].media_url_https}?name=4096x4096`
} }
} }
return { return {
type: needsFixing(media[0]) ? "remux" : "normal", type: needsFixing(media[0]) ? "remux" : "proxy",
urls: bestQuality(media[0].video_info.variants), urls: bestQuality(media[0].video_info.variants),
filename: `twitter_${id}.mp4`, filename: `twitter_${id}.mp4`,
audioFilename: `twitter_${id}_audio`, audioFilename: `twitter_${id}_audio`,
@ -183,7 +183,7 @@ export default async function({ id, index, toGif, dispatcher }) {
const proxyThumb = (url) => const proxyThumb = (url) =>
createStream({ createStream({
service: "twitter", service: "twitter",
type: "default", type: "proxy",
u: url, u: url,
filename: `image.${new URL(url).pathname.split(".", 2)[1]}` filename: `image.${new URL(url).pathname.split(".", 2)[1]}`
}) })
@ -199,15 +199,15 @@ export default async function({ id, index, toGif, dispatcher }) {
} }
let url = bestQuality(content.video_info.variants); let url = bestQuality(content.video_info.variants);
const shouldRenderGif = content.type === 'animated_gif' && toGif; const shouldRenderGif = content.type === "animated_gif" && toGif;
let type = "video"; let type = "video";
if (shouldRenderGif) type = "gif"; if (shouldRenderGif) type = "gif";
if (needsFixing(content) || shouldRenderGif) { if (needsFixing(content) || shouldRenderGif) {
url = createStream({ url = createStream({
service: 'twitter', service: "twitter",
type: shouldRenderGif ? 'gif' : 'remux', type: shouldRenderGif ? "gif" : "remux",
u: url, u: url,
filename: `twitter_${id}_${i + 1}.mp4` filename: `twitter_${id}_${i + 1}.mp4`
}) })

View file

@ -263,7 +263,7 @@ export default async function(o) {
} }
if (audio && o.isAudioOnly) return { if (audio && o.isAudioOnly) return {
type: "render", type: "audio",
isAudioOnly: true, isAudioOnly: true,
urls: audio.decipher(yt.session.player), urls: audio.decipher(yt.session.player),
filenameAttributes: filenameAttributes, filenameAttributes: filenameAttributes,
@ -290,7 +290,7 @@ export default async function(o) {
if (!match && video && audio) { if (!match && video && audio) {
match = video; match = video;
type = "render"; type = "merge";
urls = [ urls = [
video.decipher(yt.session.player), video.decipher(yt.session.player),
audio.decipher(yt.session.player) audio.decipher(yt.session.player)

View file

@ -1,5 +1,5 @@
import { createInternalStream } from './manage.js'; import HLS from "hls-parser";
import HLS from 'hls-parser'; import { createInternalStream } from "./manage.js";
function getURL(url) { function getURL(url) {
try { try {

View file

@ -1,7 +1,7 @@
import { request } from 'undici'; import { request } from "undici";
import { Readable } from 'node:stream'; import { Readable } from "node:stream";
import { closeRequest, getHeaders, pipe } from './shared.js'; import { closeRequest, getHeaders, pipe } from "./shared.js";
import { handleHlsPlaylist, isHlsRequest } from './internal-hls.js'; import { handleHlsPlaylist, isHlsRequest } from "./internal-hls.js";
const CHUNK_SIZE = BigInt(8e6); // 8 MB const CHUNK_SIZE = BigInt(8e6); // 8 MB
const min = (a, b) => a < b ? a : b; const min = (a, b) => a < b ? a : b;

View file

@ -1,12 +1,13 @@
import NodeCache from "node-cache"; import NodeCache from "node-cache";
import { randomBytes } from "crypto";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { randomBytes } from "crypto";
import { strict as assert } from "assert";
import { setMaxListeners } from "node:events"; import { setMaxListeners } from "node:events";
import { decryptStream, encryptStream, generateHmac } from "../misc/crypto.js";
import { env } from "../config.js"; import { env } from "../config.js";
import { strict as assert } from "assert";
import { closeRequest } from "./shared.js"; import { closeRequest } from "./shared.js";
import { decryptStream, encryptStream, generateHmac } from "../misc/crypto.js";
// optional dependency // optional dependency
const freebind = env.freebindCIDR && await import('freebind').catch(() => {}); const freebind = env.freebindCIDR && await import('freebind').catch(() => {});
@ -37,10 +38,8 @@ export function createStream(obj) {
service: obj.service, service: obj.service,
filename: obj.filename, filename: obj.filename,
audioFormat: obj.audioFormat, audioFormat: obj.audioFormat,
isAudioOnly: !!obj.isAudioOnly,
headers: obj.headers, headers: obj.headers,
copy: !!obj.copy, copy: !!obj.copy,
mute: !!obj.mute,
metadata: obj.fileMetadata || false, metadata: obj.fileMetadata || false,
requestIP: obj.requestIP requestIP: obj.requestIP
}; };

View file

@ -1,31 +1,33 @@
import { streamAudioOnly, streamDefault, streamLiveRender, streamVideoOnly, convertToGif } from "./types.js"; import stream from "./types.js";
import { internalStream } from './internal.js';
import { closeResponse } from "./shared.js"; import { closeResponse } from "./shared.js";
import { internalStream } from "./internal.js";
export default async function(res, streamInfo) { export default async function(res, streamInfo) {
try { try {
if (streamInfo.isAudioOnly && streamInfo.type !== "proxy") {
streamAudioOnly(streamInfo, res);
return;
}
switch (streamInfo.type) { switch (streamInfo.type) {
case "proxy":
return await stream.proxy(streamInfo, res);
case "internal": case "internal":
return await internalStream(streamInfo, res); return internalStream(streamInfo, res);
case "render":
await streamLiveRender(streamInfo, res); case "merge":
break; return stream.merge(streamInfo, res);
case "gif":
convertToGif(streamInfo, res);
break;
case "remux": case "remux":
case "mute": case "mute":
streamVideoOnly(streamInfo, res); return stream.remux(streamInfo, res);
break;
default: case "audio":
await streamDefault(streamInfo, res); return stream.convertAudio(streamInfo, res);
break;
case "gif":
return stream.convertGif(streamInfo, res);
} }
closeResponse(res);
} catch { } catch {
closeResponse(res) closeResponse(res);
} }
} }

View file

@ -9,30 +9,29 @@ import { destroyInternalStream } from "./manage.js";
import { hlsExceptions } from "../processing/service-config.js"; import { hlsExceptions } from "../processing/service-config.js";
import { getHeaders, closeRequest, closeResponse, pipe } from "./shared.js"; import { getHeaders, closeRequest, closeResponse, pipe } from "./shared.js";
function toRawHeaders(headers) { const toRawHeaders = (headers) => {
return Object.entries(headers) return Object.entries(headers)
.map(([key, value]) => `${key}: ${value}\r\n`) .map(([key, value]) => `${key}: ${value}\r\n`)
.join(''); .join('');
} }
function killProcess(p) { const killProcess = (p) => {
// ask the process to terminate itself gracefully p?.kill('SIGTERM'); // ask the process to terminate itself gracefully
p?.kill('SIGTERM');
setTimeout(() => { setTimeout(() => {
if (p?.exitCode === null) if (p?.exitCode === null)
// brutally murder the process if it didn't quit p?.kill('SIGKILL'); // brutally murder the process if it didn't quit
p?.kill('SIGKILL');
}, 5000); }, 5000);
} }
function getCommand(args) { const getCommand = (args) => {
if (typeof env.processingPriority === 'number' && !isNaN(env.processingPriority)) { if (typeof env.processingPriority === 'number' && !isNaN(env.processingPriority)) {
return ['nice', ['-n', env.processingPriority.toString(), ffmpeg, ...args]] return ['nice', ['-n', env.processingPriority.toString(), ffmpeg, ...args]]
} }
return [ffmpeg, args] return [ffmpeg, args]
} }
export async function streamDefault(streamInfo, res) { const proxy = async (streamInfo, res) => {
const abortController = new AbortController(); const abortController = new AbortController();
const shutdown = () => ( const shutdown = () => (
closeRequest(abortController), closeRequest(abortController),
@ -42,7 +41,7 @@ export async function streamDefault(streamInfo, res) {
try { try {
let filename = streamInfo.filename; let filename = streamInfo.filename;
if (streamInfo.isAudioOnly) { if (streamInfo.audioFormat) {
filename = `${streamInfo.filename}.${streamInfo.audioFormat}` filename = `${streamInfo.filename}.${streamInfo.audioFormat}`
} }
@ -67,7 +66,7 @@ export async function streamDefault(streamInfo, res) {
} }
} }
export function streamLiveRender(streamInfo, res) { const merge = (streamInfo, res) => {
let process; let process;
const shutdown = () => ( const shutdown = () => (
killProcess(process), killProcess(process),
@ -127,61 +126,7 @@ export function streamLiveRender(streamInfo, res) {
} }
} }
export function streamAudioOnly(streamInfo, res) { const remux = (streamInfo, res) => {
let process;
const shutdown = () => (
killProcess(process),
closeResponse(res),
destroyInternalStream(streamInfo.urls)
);
try {
let args = [
'-loglevel', '-8',
'-headers', toRawHeaders(getHeaders(streamInfo.service)),
]
if (streamInfo.service === "twitter") {
args.push('-seekable', '0');
}
args.push(
'-i', streamInfo.urls,
'-vn'
)
if (streamInfo.metadata) {
args = args.concat(metadataManager(streamInfo.metadata))
}
args = args.concat(ffmpegArgs[streamInfo.copy ? 'copy' : 'audio']);
if (ffmpegArgs[streamInfo.audioFormat]) {
args = args.concat(ffmpegArgs[streamInfo.audioFormat])
}
args.push('-f', streamInfo.audioFormat === "m4a" ? "ipod" : streamInfo.audioFormat, 'pipe:3');
process = spawn(...getCommand(args), {
windowsHide: true,
stdio: [
'inherit', 'inherit', 'inherit',
'pipe'
],
});
const [,,, muxOutput] = process.stdio;
res.setHeader('Connection', 'keep-alive');
res.setHeader('Content-Disposition', contentDisposition(`${streamInfo.filename}.${streamInfo.audioFormat}`));
pipe(muxOutput, res, shutdown);
res.on('finish', shutdown);
} catch {
shutdown();
}
}
export function streamVideoOnly(streamInfo, res) {
let process; let process;
const shutdown = () => ( const shutdown = () => (
killProcess(process), killProcess(process),
@ -204,7 +149,7 @@ export function streamVideoOnly(streamInfo, res) {
'-c', 'copy' '-c', 'copy'
) )
if (streamInfo.mute) { if (streamInfo.type === "mute") {
args.push('-an') args.push('-an')
} }
@ -241,7 +186,64 @@ export function streamVideoOnly(streamInfo, res) {
} }
} }
export function convertToGif(streamInfo, res) { const convertAudio = (streamInfo, res) => {
let process;
const shutdown = () => (
killProcess(process),
closeResponse(res),
destroyInternalStream(streamInfo.urls)
);
try {
let args = [
'-loglevel', '-8',
'-headers', toRawHeaders(getHeaders(streamInfo.service)),
]
if (streamInfo.service === "twitter") {
args.push('-seekable', '0');
}
args.push(
'-i', streamInfo.urls,
'-vn'
)
if (streamInfo.metadata) {
args = args.concat(metadataManager(streamInfo.metadata))
}
args = args.concat(
streamInfo.copy ? ["-c:a", "copy"] : ffmpegArgs.audio
);
if (ffmpegArgs[streamInfo.audioFormat]) {
args = args.concat(ffmpegArgs[streamInfo.audioFormat])
}
args.push('-f', streamInfo.audioFormat === "m4a" ? "ipod" : streamInfo.audioFormat, 'pipe:3');
process = spawn(...getCommand(args), {
windowsHide: true,
stdio: [
'inherit', 'inherit', 'inherit',
'pipe'
],
});
const [,,, muxOutput] = process.stdio;
res.setHeader('Connection', 'keep-alive');
res.setHeader('Content-Disposition', contentDisposition(`${streamInfo.filename}.${streamInfo.audioFormat}`));
pipe(muxOutput, res, shutdown);
res.on('finish', shutdown);
} catch {
shutdown();
}
}
const convertGif = (streamInfo, res) => {
let process; let process;
const shutdown = () => (killProcess(process), closeResponse(res)); const shutdown = () => (killProcess(process), closeResponse(res));
@ -279,3 +281,11 @@ export function convertToGif(streamInfo, res) {
shutdown(); shutdown();
} }
} }
export default {
proxy,
merge,
remux,
convertAudio,
convertGif,
}