mirror of
https://github.com/imputnet/cobalt.git
synced 2025-07-17 18:58:33 +00:00
stream: generalize/decompose somewhat
This commit is contained in:
parent
7f563ef485
commit
7df43c43c4
@ -1,5 +1,5 @@
|
||||
import { strict as assert } from 'node:assert';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { spawn } from './shared.js';
|
||||
import { path as ffprobe } from 'ffprobe-static';
|
||||
|
||||
function mapFormat(format) {
|
||||
|
38
src/modules/stream/shared.js
Normal file
38
src/modules/stream/shared.js
Normal file
@ -0,0 +1,38 @@
|
||||
import { spawn as _node_spawn } from 'child_process'
|
||||
|
||||
export function killProcess(p) {
|
||||
// ask the process to terminate itself gracefully
|
||||
p?.kill('SIGTERM');
|
||||
setTimeout(() => {
|
||||
if (p?.exitCode === null)
|
||||
// brutally murder the process if it didn't quit
|
||||
p?.kill('SIGKILL');
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
export function pipe(from, to, done) {
|
||||
from.on('error', done)
|
||||
.on('close', done);
|
||||
|
||||
to.on('error', done)
|
||||
.on('close', done);
|
||||
|
||||
from.pipe(to);
|
||||
}
|
||||
|
||||
export function wrapCommand(command, args = []) {
|
||||
if (process.env.PROCESSING_PRIORITY && process.platform !== "win32") {
|
||||
return ['nice', ['-n', process.env.PROCESSING_PRIORITY, command, ...args]]
|
||||
}
|
||||
|
||||
return [command, args]
|
||||
}
|
||||
|
||||
export function spawn(command, args, opts) {
|
||||
opts = {
|
||||
...opts,
|
||||
windowsHide: true
|
||||
};
|
||||
|
||||
return _node_spawn(...wrapCommand(command, args), opts);
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import { spawn } from "child_process";
|
||||
import ffmpeg from "ffmpeg-static";
|
||||
import { ffmpegArgs, genericUserAgent } from "../config.js";
|
||||
import { spawn, pipe, killProcess } from "./shared.js";
|
||||
import { metadataManager } from "../sub/utils.js";
|
||||
import { request } from "undici";
|
||||
import { create as contentDisposition } from "content-disposition-header";
|
||||
@ -15,33 +15,6 @@ function closeResponse(res) {
|
||||
return res.destroy();
|
||||
}
|
||||
|
||||
function killProcess(p) {
|
||||
// ask the process to terminate itself gracefully
|
||||
p?.kill('SIGTERM');
|
||||
setTimeout(() => {
|
||||
if (p?.exitCode === null)
|
||||
// brutally murder the process if it didn't quit
|
||||
p?.kill('SIGKILL');
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
function pipe(from, to, done) {
|
||||
from.on('error', done)
|
||||
.on('close', done);
|
||||
|
||||
to.on('error', done)
|
||||
.on('close', done);
|
||||
|
||||
from.pipe(to);
|
||||
}
|
||||
|
||||
function getCommand(args) {
|
||||
if (process.env.PROCESSING_PRIORITY && process.platform !== "win32") {
|
||||
return ['nice', ['-n', process.env.PROCESSING_PRIORITY, ffmpeg, ...args]]
|
||||
}
|
||||
return [ffmpeg, args]
|
||||
}
|
||||
|
||||
export async function streamDefault(streamInfo, res) {
|
||||
const abortController = new AbortController();
|
||||
const shutdown = () => (closeRequest(abortController), closeResponse(res));
|
||||
@ -98,8 +71,7 @@ export async function streamLiveRender(streamInfo, res) {
|
||||
}
|
||||
args.push('-f', format, 'pipe:4');
|
||||
|
||||
process = spawn(...getCommand(args), {
|
||||
windowsHide: true,
|
||||
process = spawn(ffmpeg, args, {
|
||||
stdio: [
|
||||
'inherit', 'inherit', 'inherit',
|
||||
'pipe', 'pipe'
|
||||
@ -140,18 +112,18 @@ export function streamAudioOnly(streamInfo, res) {
|
||||
)
|
||||
|
||||
if (streamInfo.metadata) {
|
||||
args = args.concat(metadataManager(streamInfo.metadata))
|
||||
args.push(...metadataManager(streamInfo.metadata))
|
||||
}
|
||||
let arg = streamInfo.copy ? ffmpegArgs["copy"] : ffmpegArgs["audio"];
|
||||
args = args.concat(arg);
|
||||
|
||||
args.push(...ffmpegArgs[streamInfo.copy ? "copy" : "audio"]);
|
||||
|
||||
if (ffmpegArgs[streamInfo.audioFormat]) {
|
||||
args = args.concat(ffmpegArgs[streamInfo.audioFormat])
|
||||
args.push(...ffmpegArgs[streamInfo.audioFormat]);
|
||||
}
|
||||
|
||||
args.push('-f', streamInfo.audioFormat === "m4a" ? "ipod" : streamInfo.audioFormat, 'pipe:3');
|
||||
|
||||
process = spawn(...getCommand(args), {
|
||||
windowsHide: true,
|
||||
process = spawn(ffmpeg, args, {
|
||||
stdio: [
|
||||
'inherit', 'inherit', 'inherit',
|
||||
'pipe'
|
||||
@ -198,8 +170,7 @@ export function streamVideoOnly(streamInfo, res) {
|
||||
}
|
||||
args.push('-f', format, 'pipe:3');
|
||||
|
||||
process = spawn(...getCommand(args), {
|
||||
windowsHide: true,
|
||||
process = spawn(ffmpeg, args, {
|
||||
stdio: [
|
||||
'inherit', 'inherit', 'inherit',
|
||||
'pipe'
|
||||
@ -235,8 +206,7 @@ export function convertToGif(streamInfo, res) {
|
||||
args = args.concat(ffmpegArgs["gif"]);
|
||||
args.push('-f', "gif", 'pipe:3');
|
||||
|
||||
process = spawn(...getCommand(args), {
|
||||
windowsHide: true,
|
||||
process = spawn(ffmpeg, args, {
|
||||
stdio: [
|
||||
'inherit', 'inherit', 'inherit',
|
||||
'pipe'
|
||||
|
Loading…
Reference in New Issue
Block a user