api: initial subtitles functionality with youtube support

this took way more effort than i expected it to do, mostly because of youtube locking everything down to shit

local processing doesn't function with subtitles yet, wasm needs to be updated
This commit is contained in:
wukko
2025-06-18 20:19:19 +06:00
parent 967552b26b
commit 259a0758f1
5 changed files with 149 additions and 38 deletions

View File

@@ -41,7 +41,10 @@ export function createStream(obj) {
audioFormat: obj.audioFormat,
isHLS: obj.isHLS || false,
originalRequest: obj.originalRequest
originalRequest: obj.originalRequest,
// url to a subtitle file
subtitles: obj.subtitles,
};
// FIXME: this is now a Promise, but it is not awaited
@@ -94,6 +97,18 @@ export function createProxyTunnels(info) {
);
}
if (info.subtitles) {
proxyTunnels.push(
createStream({
url: info.subtitles,
type: "proxy",
service: `${info?.service}-subtitles`,
headers: info?.headers,
requestIP: info?.requestIP
})
);
}
return proxyTunnels;
}
@@ -111,7 +126,7 @@ export function getInternalTunnelFromURL(url) {
return getInternalTunnel(id);
}
export function createInternalStream(url, obj = {}) {
export function createInternalStream(url, obj = {}, isSubtitles) {
assert(typeof url === 'string');
let dispatcher = obj.dispatcher;
@@ -132,9 +147,12 @@ export function createInternalStream(url, obj = {}) {
headers = new Map(Object.entries(obj.headers));
}
// subtitles don't need special treatment unlike big media files
const service = isSubtitles ? `${obj.service}-subtitles` : obj.service;
internalStreamCache.set(streamID, {
url,
service: obj.service,
service,
headers,
controller,
dispatcher,
@@ -245,6 +263,14 @@ function wrapStream(streamInfo) {
}
} else throw 'invalid urls';
if (streamInfo.subtitles) {
streamInfo.subtitles = createInternalStream(
streamInfo.subtitles,
streamInfo,
/*isSubtitles=*/true
);
}
return streamInfo;
}

View File

@@ -25,6 +25,7 @@ const metadataTags = [
"album_artist",
"track",
"date",
"sublanguage"
];
const convertMetadataToFFmpeg = (metadata) => {
@@ -32,6 +33,10 @@ const convertMetadataToFFmpeg = (metadata) => {
for (const [ name, value ] of Object.entries(metadata)) {
if (metadataTags.includes(name)) {
if (name === "sublanguage") {
args.push('-metadata:s:s:0', `language=${value}`);
continue;
}
args.push('-metadata', `${name}=${value.replace(/[\u0000-\u0009]/g, "")}`); // skipcq: JS-0004
} else {
throw `${name} metadata tag is not supported.`;
@@ -109,9 +114,6 @@ const merge = async (streamInfo, res) => {
streamInfo.urls.map(destroyInternalStream)
);
const headers = getHeaders(streamInfo.service);
const rawHeaders = toRawHeaders(headers);
try {
if (streamInfo.urls.length !== 2) return shutdown();
@@ -119,13 +121,21 @@ const merge = async (streamInfo, res) => {
let args = [
'-loglevel', '-8',
'-headers', rawHeaders,
'-i', streamInfo.urls[0],
'-headers', rawHeaders,
'-i', streamInfo.urls[1],
];
if (streamInfo.subtitles) {
args.push(
'-i', streamInfo.subtitles,
'-map', '2:s'
);
};
args.push(
'-map', '0:v',
'-map', '1:a',
]
);
args = args.concat(ffmpegArgs[format]);
@@ -137,8 +147,12 @@ const merge = async (streamInfo, res) => {
}
}
if (streamInfo.subtitles && format === "mp4") {
args.push('-c:s', 'mov_text');
}
if (streamInfo.metadata) {
args = args.concat(convertMetadataToFFmpeg(streamInfo.metadata))
args = args.concat(convertMetadataToFFmpeg(streamInfo.metadata));
}
args.push('-f', format, 'pipe:3');