2018-04-22 23:50:07 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
2018-04-26 01:26:39 +00:00
|
|
|
const ffmpeg = require('fluent-ffmpeg');
|
|
|
|
|
2018-05-05 15:08:40 +00:00
|
|
|
function save(filepath, streams, item, post) {
|
2018-04-26 01:26:39 +00:00
|
|
|
const pathComponents = path.parse(filepath);
|
2018-04-22 23:50:07 +00:00
|
|
|
|
2018-04-26 01:35:42 +00:00
|
|
|
// allow for single stream argument
|
|
|
|
streams = [].concat(streams);
|
|
|
|
|
2018-04-22 23:50:07 +00:00
|
|
|
return Promise.resolve().then(() => {
|
2018-04-26 22:42:11 +00:00
|
|
|
return fs.ensureDir(pathComponents.dir);
|
2018-04-22 23:50:07 +00:00
|
|
|
}).then(() => {
|
2018-04-26 01:35:42 +00:00
|
|
|
return Promise.all(streams.map((stream, index) => {
|
|
|
|
const target = streams.length > 1 ? path.join(pathComponents.dir, `${pathComponents.name}-${index}${pathComponents.ext}`) : filepath;
|
2018-04-26 01:26:39 +00:00
|
|
|
const file = fs.createWriteStream(target);
|
2018-04-22 23:50:07 +00:00
|
|
|
|
2018-04-26 01:26:39 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
stream.pipe(file).on('error', error => {
|
|
|
|
reject(error);
|
|
|
|
}).on('finish', () => {
|
2018-04-26 01:35:42 +00:00
|
|
|
if(item && item.mux) {
|
2018-04-26 01:26:39 +00:00
|
|
|
console.log(`Temporarily saved '${target}', queued for muxing`);
|
|
|
|
} else {
|
|
|
|
console.log('\x1b[32m%s\x1b[0m', `Saved '${target}'`);
|
|
|
|
}
|
2018-04-22 23:50:07 +00:00
|
|
|
|
2018-04-26 01:26:39 +00:00
|
|
|
resolve(target);
|
|
|
|
});
|
2018-04-22 23:50:07 +00:00
|
|
|
});
|
2018-04-26 01:26:39 +00:00
|
|
|
}));
|
2018-04-22 23:50:07 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = save;
|