Adapted primary save function to handle single streams and non-item saves (e.g. profiles).

This commit is contained in:
ThePendulum 2018-04-26 03:35:42 +02:00
parent e752667be7
commit 0313b291da
3 changed files with 10 additions and 7 deletions

View File

@ -42,10 +42,10 @@ module.exports = function(posts, user) {
return Promise.resolve().then(() => { return Promise.resolve().then(() => {
return fs.ensureDir(path.dirname(filepath)); return fs.ensureDir(path.dirname(filepath));
}).then(() => { }).then(() => {
return save(filepath, item); return save(filepath, item.streams, item);
}).then(sourcePaths => { }).then(sourcePaths => {
if(item.mux) { if(item.mux) {
return mux(sourcePaths, filepath, item); return mux(filepath, sourcePaths, item);
} }
}).then(() => { }).then(() => {
const meta = Object.entries(config.library.meta).reduce((acc, [key, value]) => { const meta = Object.entries(config.library.meta).reduce((acc, [key, value]) => {

View File

@ -3,7 +3,7 @@
const ffmpeg = require('fluent-ffmpeg'); const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs-extra'); const fs = require('fs-extra');
function mux(sources, target, item) { function mux(target, sources, item) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
return sources.reduce((acc, source) => { return sources.reduce((acc, source) => {
return acc.input(source); return acc.input(source);

View File

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