api/ffmpeg: refactor even more

This commit is contained in:
wukko 2025-06-25 19:32:36 +06:00
parent d3793c7a54
commit fcdf5da73e
No known key found for this signature in database
GPG Key ID: 3E30B3F26C7B4AA2

View File

@ -7,7 +7,7 @@ import { destroyInternalStream } from "./manage.js";
import { hlsExceptions } from "../processing/service-config.js"; import { hlsExceptions } from "../processing/service-config.js";
import { closeResponse, pipe, estimateTunnelLength, estimateAudioMultiplier } from "./shared.js"; import { closeResponse, pipe, estimateTunnelLength, estimateAudioMultiplier } from "./shared.js";
const metadataTags = [ const metadataTags = new Set([
"album", "album",
"composer", "composer",
"genre", "genre",
@ -18,13 +18,13 @@ const metadataTags = [
"track", "track",
"date", "date",
"sublanguage" "sublanguage"
]; ]);
const convertMetadataToFFmpeg = (metadata) => { const convertMetadataToFFmpeg = (metadata) => {
const args = []; const args = [];
for (const [ name, value ] of Object.entries(metadata)) { for (const [ name, value ] of Object.entries(metadata)) {
if (metadataTags.includes(name)) { if (metadataTags.has(name)) {
if (name === "sublanguage") { if (name === "sublanguage") {
args.push('-metadata:s:s:0', `language=${value}`); args.push('-metadata:s:s:0', `language=${value}`);
continue; continue;
@ -54,7 +54,7 @@ const getCommand = (args) => {
return [ffmpeg, args] return [ffmpeg, args]
} }
const render = async (res, streamInfo, ffargs, multiplier) => { const render = async (res, streamInfo, ffargs, estimateMultiplier) => {
let process; let process;
const urls = Array.isArray(streamInfo.urls) ? streamInfo.urls : [streamInfo.urls]; const urls = Array.isArray(streamInfo.urls) ? streamInfo.urls : [streamInfo.urls];
const shutdown = () => ( const shutdown = () => (
@ -66,7 +66,7 @@ const render = async (res, streamInfo, ffargs, multiplier) => {
try { try {
// if the streamInfo.urls is an array but doesn't have 2 urls, // if the streamInfo.urls is an array but doesn't have 2 urls,
// then something went wrong // then something went wrong
if (Array.isArray(streamInfo.urls) && streamInfo.urls.length !== 2) { if (urls.length !== 2) {
return shutdown(); return shutdown();
} }
@ -87,7 +87,11 @@ const render = async (res, streamInfo, ffargs, multiplier) => {
res.setHeader('Connection', 'keep-alive'); res.setHeader('Connection', 'keep-alive');
res.setHeader('Content-Disposition', contentDisposition(streamInfo.filename)); res.setHeader('Content-Disposition', contentDisposition(streamInfo.filename));
res.setHeader('Estimated-Content-Length', await estimateTunnelLength(streamInfo, multiplier));
res.setHeader(
'Estimated-Content-Length',
await estimateTunnelLength(streamInfo, estimateMultiplier)
);
pipe(muxOutput, res, shutdown); pipe(muxOutput, res, shutdown);
@ -101,7 +105,7 @@ const render = async (res, streamInfo, ffargs, multiplier) => {
const remux = async (streamInfo, res) => { const remux = async (streamInfo, res) => {
const format = streamInfo.filename.split('.').pop(); const format = streamInfo.filename.split('.').pop();
const urls = Array.isArray(streamInfo.urls) ? streamInfo.urls : [streamInfo.urls]; const urls = Array.isArray(streamInfo.urls) ? streamInfo.urls : [streamInfo.urls];
const args = [...urls.flatMap(url => ['-i', url])]; const args = urls.flatMap(url => ['-i', url]);
if (streamInfo.subtitles) { if (streamInfo.subtitles) {
args.push( args.push(
@ -115,15 +119,16 @@ const remux = async (streamInfo, res) => {
args.push( args.push(
'-map', '0:v', '-map', '0:v',
'-map', '1:a', '-map', '1:a',
'-c:v', 'copy',
); );
} else { } else {
args.push('-map', '0:v:0'); args.push(
args.push('-map', '0:a:0'); '-map', '0:v:0',
args.push('-c:v', 'copy'); '-map', '0:a:0'
);
} }
args.push( args.push(
'-c:v', 'copy',
...(streamInfo.type === 'mute' ? ['-an'] : ['-c:a', 'copy']) ...(streamInfo.type === 'mute' ? ['-an'] : ['-c:a', 'copy'])
); );