Added support for muxing streams, specifically for reddit-hosted videos (now also supported).

This commit is contained in:
2024-09-11 05:16:54 +02:00
parent 1546852f89
commit 536c427140
11 changed files with 226 additions and 33 deletions

26
src/save/mux.js Normal file
View File

@@ -0,0 +1,26 @@
'use strict';
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs-extra');
function mux(sources, target, item) {
return new Promise((resolve, reject) => {
return sources.reduce((acc, source) => {
return acc.input(source);
}, ffmpeg()).videoCodec('copy').audioCodec('copy').on('start', cmd => {
console.log('\x1b[36m%s\x1b[0m', `Muxing ${sources.length} streams to '${target}'`);
}).on('end', (stdout) => {
console.log('\x1b[32m%s\x1b[0m', `Muxed and saved '${target}'`);
resolve(stdout);
}).on('error', error => reject).save(target);
}).then(() => {
return Promise.all(sources.map(source => {
return fs.remove(source);
})).then(() => {
console.log('\x1b[36m%s\x1b[0m', `Cleaned up temporary files for '${target}'`);
});
});
};
module.exports = mux;

View File

@@ -2,22 +2,32 @@
const fs = require('fs-extra');
const path = require('path');
const ffmpeg = require('fluent-ffmpeg');
function save(filepath, item) {
const pathComponents = path.parse(filepath);
function save(filepath, stream) {
return Promise.resolve().then(() => {
return fs.ensureDir(path.dirname(filepath));
return fs.ensureDir(path.dirname(pathComponents.dir));
}).then(() => {
const file = fs.createWriteStream(filepath);
return Promise.all(item.streams.map((stream, index) => {
const target = item.streams.length > 1 ? path.join(pathComponents.dir, `${pathComponents.name}-${index}${pathComponents.ext}`) : filepath;
const file = fs.createWriteStream(target);
return new Promise((resolve, reject) => {
stream.pipe(file).on('error', error => {
reject(error);
}).on('finish', () => {
console.log('\x1b[32m%s\x1b[0m', `Saved '${filepath}'`);
return new Promise((resolve, reject) => {
stream.pipe(file).on('error', error => {
reject(error);
}).on('finish', () => {
if(item.mux) {
console.log(`Temporarily saved '${target}', queued for muxing`);
} else {
console.log('\x1b[32m%s\x1b[0m', `Saved '${target}'`);
}
resolve(filepath);
resolve(target);
});
});
});
}));
});
};