2019-06-09 00:51:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-08-04 21:31:27 +00:00
|
|
|
/** @module ffmpeg **/
|
|
|
|
|
2019-06-18 20:57:35 +00:00
|
|
|
import { join } from 'path';
|
2019-06-09 01:43:14 +00:00
|
|
|
import { exists, mkdir, readdir, unlink } from 'fs-extra';
|
|
|
|
import { exec } from 'exec';
|
2020-03-09 19:46:06 +00:00
|
|
|
import { spawn } from 'child_process';
|
2020-02-21 18:34:22 +00:00
|
|
|
|
2020-04-24 18:33:58 +00:00
|
|
|
import Frame from 'frame';
|
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
interface FilmoutState {
|
|
|
|
frame : number;
|
|
|
|
path : string;
|
|
|
|
hash : string;
|
|
|
|
info : any;
|
2020-03-09 19:46:06 +00:00
|
|
|
frames?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface StdErr {
|
|
|
|
frame : number;
|
|
|
|
fps : number;
|
|
|
|
time : string;
|
|
|
|
speed : number;
|
|
|
|
size : string;
|
|
|
|
remaining? : number;
|
|
|
|
progress? : number;
|
|
|
|
estimated? : number;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function spawnAsync (bin : string, args : string[]) {
|
|
|
|
return new Promise((resolve : Function, reject : Function) => {
|
|
|
|
const child = spawn(bin, args);
|
|
|
|
let stdout = '';
|
|
|
|
let stderr = '';
|
|
|
|
child.on('exit', (code : number) => {
|
|
|
|
if (code === 0) {
|
|
|
|
return resolve({ stdout, stderr });
|
|
|
|
} else {
|
|
|
|
console.error(`Process exited with code: ${code}`);
|
|
|
|
console.error(stderr);
|
|
|
|
return reject(stderr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
child.stdout.on('data', (data : string) => {
|
|
|
|
stdout += data;
|
|
|
|
});
|
|
|
|
child.stderr.on('data', (data : string) => {
|
|
|
|
stderr += data;
|
|
|
|
});
|
|
|
|
return child;
|
|
|
|
});
|
2020-02-21 18:34:22 +00:00
|
|
|
}
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
/** @class FFMPEG **/
|
|
|
|
|
|
|
|
class FFMPEG {
|
2019-08-23 19:39:38 +00:00
|
|
|
private bin : string;
|
2019-08-04 21:42:27 +00:00
|
|
|
private log : any;
|
|
|
|
private id : string = 'ffmpeg';
|
|
|
|
private TMPDIR : string;
|
2020-03-09 19:46:06 +00:00
|
|
|
private child : any;
|
|
|
|
public onProgress : Function = () => {};
|
2019-08-04 21:54:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* Creates an ffmpeg class
|
|
|
|
*
|
|
|
|
* @param {object} sys System object to be used to get temp directory
|
|
|
|
**/
|
2019-08-04 21:42:27 +00:00
|
|
|
constructor (sys : any) {
|
2019-08-23 19:39:38 +00:00
|
|
|
this.bin = sys.deps.ffmpeg;
|
|
|
|
this.TMPDIR = join(sys.tmp, 'mcopy_digital');
|
|
|
|
this.init();
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|
2019-08-04 21:54:38 +00:00
|
|
|
/**
|
|
|
|
* Async method to call async functions from constructor
|
|
|
|
**/
|
2019-08-04 21:42:27 +00:00
|
|
|
async init () {
|
|
|
|
const Log = require('log');
|
|
|
|
this.log = await Log({ label : this.id });
|
|
|
|
await this.checkDir();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Add padding to a number to 5 places. Return a string.
|
|
|
|
*
|
|
|
|
* @param {integer} i Integer to pad
|
|
|
|
*
|
|
|
|
* @returns {string} Padded string
|
|
|
|
**/
|
|
|
|
|
|
|
|
private padded_frame (i : number) {
|
|
|
|
let len = (i + '').length;
|
|
|
|
let str = i + '';
|
2020-02-21 06:58:56 +00:00
|
|
|
for (let x = 0; x < 8 - len; x++) {
|
2019-08-04 21:42:27 +00:00
|
|
|
str = '0' + str;
|
|
|
|
}
|
|
|
|
return str;
|
2019-08-04 21:20:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 19:46:06 +00:00
|
|
|
/**
|
|
|
|
* Parse the stderr output of ffmpeg
|
|
|
|
*
|
|
|
|
* @param {string} line Stderr line
|
|
|
|
**/
|
|
|
|
private parseStderr (line : string) : StdErr {
|
|
|
|
//frame= 6416 fps= 30 q=31.0 size= 10251kB time=00:03:34.32 bitrate= 391.8kbits/s speed= 1x
|
|
|
|
let obj : any = {};
|
|
|
|
|
|
|
|
if (line.substring(0, 'frame='.length) === 'frame=') {
|
|
|
|
try {
|
|
|
|
obj.frame = line.split('frame=')[1].split('fps=')[0];
|
|
|
|
obj.frame = parseInt(obj.frame);
|
|
|
|
obj.fps = line.split('fps=')[1].split('q=')[0];
|
|
|
|
obj.fps = parseFloat(obj.fps);
|
|
|
|
obj.time = line.split('time=')[1].split('bitrate=')[0];
|
|
|
|
obj.speed = line.split('speed=')[1].trim().replace('x', '');
|
|
|
|
obj.speed = parseFloat(obj.speed);
|
|
|
|
obj.size = line.split('size=')[1].split('time=')[0].trim();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
console.log(line);
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
/**
|
|
|
|
* Render a single frame from a video or image to a png.
|
|
|
|
*
|
|
|
|
* @param {object} state State object containing file data
|
|
|
|
* @param {object} light Object containing color information for frame
|
|
|
|
*
|
|
|
|
* @returns {string} Path of frame
|
|
|
|
**/
|
2020-02-21 18:34:22 +00:00
|
|
|
public async frame (state : FilmoutState, light : any) {
|
2019-08-04 21:42:27 +00:00
|
|
|
const frameNum : number = state.frame;
|
|
|
|
const video : string = state.path;
|
|
|
|
const w : number = state.info.width;
|
|
|
|
const h : number = state.info.height;
|
|
|
|
const padded : string = this.padded_frame(frameNum);
|
2020-02-21 18:34:22 +00:00
|
|
|
let ext : string = 'png';
|
2020-04-24 18:33:58 +00:00
|
|
|
let rgb : any[] = light.color;
|
|
|
|
let rgba : any = {};
|
2019-08-04 21:42:27 +00:00
|
|
|
let tmpoutput : string;
|
|
|
|
let cmd : string;
|
|
|
|
let output : any;
|
2020-02-21 18:34:22 +00:00
|
|
|
let fileExists = false;
|
2019-08-04 21:42:27 +00:00
|
|
|
|
|
|
|
let scale : string = '';
|
|
|
|
if (w && h) {
|
|
|
|
scale = `,scale=${w}:${h}`;
|
|
|
|
}
|
2019-07-29 16:45:13 +00:00
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
tmpoutput = join(this.TMPDIR, `${state.hash}-export-${padded}.${ext}`);
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
try {
|
|
|
|
fileExists = await exists(tmpoutput);
|
|
|
|
} catch (err) {
|
|
|
|
//
|
|
|
|
}
|
2019-08-04 21:20:45 +00:00
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
if (fileExists) {
|
|
|
|
this.log.info(`File ${tmpoutput} exists`);
|
|
|
|
return tmpoutput;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
cmd = `${this.bin} -y -i "${video}" -vf "select='gte(n\\,${frameNum})'${scale}" -vframes 1 -compression_algo raw -pix_fmt rgb24 -crf 0 "${tmpoutput}"`;
|
|
|
|
//cmd2 = `${this.convert} "${tmpoutput}" -resize ${w}x${h} -size ${w}x${h} xc:"rgb(${rgb[0]},${rgb[1]},${rgb[2]})" +swap -compose Darken -composite "${tmpoutput}"`;
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
//ffmpeg -i "${video}" -ss 00:00:07.000 -vframes 1 "export-${time}.jpg"
|
|
|
|
//ffmpeg -i "${video}" -compression_algo raw -pix_fmt rgb24 "export-%05d.tiff"
|
|
|
|
//-vf "select=gte(n\,${frame})" -compression_algo raw -pix_fmt rgb24 "export-${padded}.png"
|
2019-06-09 00:51:00 +00:00
|
|
|
|
|
|
|
try {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.info(cmd);
|
2019-08-04 21:42:27 +00:00
|
|
|
output = await exec(cmd);
|
2019-06-09 00:51:00 +00:00
|
|
|
} catch (err) {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.error(err);
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|
2020-04-24 18:33:58 +00:00
|
|
|
|
2019-08-04 21:54:38 +00:00
|
|
|
if (output && output.stdout) this.log.info(`"${output.stdout}"`);
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2020-04-24 18:33:58 +00:00
|
|
|
if ( rgb[0] !== 255 || rgb[1] !== 255 || rgb[2] !== 255 ) {
|
|
|
|
rgb = rgb.map((e : string) => {
|
|
|
|
return parseInt(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
rgba = { r : rgb[0], g : rgb[1], b : rgb[2], a : 255 };
|
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
try {
|
2020-04-24 18:33:58 +00:00
|
|
|
//await Frame.blend(tmpoutput, rgba, tmpoutput);
|
2019-08-04 21:42:27 +00:00
|
|
|
} catch (err) {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.error(err);
|
2019-08-04 21:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
return tmpoutput;
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
/**
|
|
|
|
* Render all frames in a video to the temp directory.
|
|
|
|
* Not in use.
|
|
|
|
*
|
|
|
|
* @param {string} video Path to video
|
|
|
|
* @param {object} obj Not sure
|
|
|
|
*
|
|
|
|
* @returns {?}
|
|
|
|
**/
|
2020-04-24 18:33:58 +00:00
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
public async frames (state : FilmoutState) {
|
|
|
|
const video : string = state.path;
|
|
|
|
const w : number = state.info.width;
|
|
|
|
const h : number = state.info.height;
|
2019-08-04 21:42:27 +00:00
|
|
|
const tmppath : string = this.TMPDIR;
|
2020-02-21 18:34:22 +00:00
|
|
|
let ext : string = 'png';
|
|
|
|
let tmpoutput : string = join(tmppath, `${state.hash}-export-%08d.${ext}`);
|
2020-03-09 19:46:06 +00:00
|
|
|
let args : string[];
|
2020-02-21 18:34:22 +00:00
|
|
|
let output : any;
|
2020-03-09 19:46:06 +00:00
|
|
|
let estimated : number = -1;
|
|
|
|
|
|
|
|
//cmd = `${this.bin} -y -i "${video}" -vf "${scale}" -compression_algo raw -pix_fmt rgb24 -crf 0 "${tmpoutput}"`;
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'-y',
|
|
|
|
'-i', video
|
|
|
|
];
|
2019-08-04 21:42:27 +00:00
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
if (w && h) {
|
2020-03-09 19:46:06 +00:00
|
|
|
args.push('-vf');
|
|
|
|
args.push(`scale=${w}:${h}`);
|
2020-02-21 18:34:22 +00:00
|
|
|
}
|
2020-03-09 19:46:06 +00:00
|
|
|
|
|
|
|
args = args.concat([
|
|
|
|
'-compression_algo', 'raw',
|
|
|
|
'-pix_fmt', 'rgb24',
|
|
|
|
'-crf', '0',
|
|
|
|
tmpoutput
|
|
|
|
]);
|
|
|
|
|
2020-04-24 19:09:51 +00:00
|
|
|
//console.dir(args)
|
|
|
|
//console.dir(state)
|
2020-02-21 18:34:22 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
try {
|
|
|
|
await mkdir(tmppath);
|
|
|
|
} catch (err) {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.error(err);
|
2019-08-04 21:42:27 +00:00
|
|
|
}
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
//ffmpeg -i "${video}" -compression_algo raw -pix_fmt rgb24 "${tmpoutput}"
|
2020-02-21 18:34:22 +00:00
|
|
|
|
2020-03-09 19:46:06 +00:00
|
|
|
return new Promise((resolve : Function, reject : Function) => {
|
|
|
|
let stdout = '';
|
|
|
|
let stderr = '';
|
|
|
|
|
|
|
|
this.log.info(`${this.bin} ${args.join(' ')}`);
|
|
|
|
this.child = spawn(this.bin, args);
|
|
|
|
|
|
|
|
this.child.on('exit', (code : number) => {
|
2020-04-24 19:09:51 +00:00
|
|
|
//console.log('GOT TO EXIT');
|
2020-03-09 19:46:06 +00:00
|
|
|
if (code === 0) {
|
|
|
|
console.log(stderr);
|
|
|
|
console.log(stdout);
|
|
|
|
return resolve(true);
|
|
|
|
} else {
|
|
|
|
console.error(`Process exited with code: ${code}`);
|
|
|
|
console.error(stderr);
|
|
|
|
return reject(stderr + stdout);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.child.stdout.on('data', (data : any) => {
|
|
|
|
const line : string = data.toString();
|
|
|
|
stdout += line;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.child.stderr.on('data', (data : any) => {
|
|
|
|
const line : string = data.toString();
|
|
|
|
const obj : StdErr = this.parseStderr(line);
|
|
|
|
|
|
|
|
if (obj.frame && state.frames) {
|
|
|
|
obj.progress = obj.frame / state.frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj.frame && obj.speed && state.frames && state.info.fps) {
|
|
|
|
//scale by speed
|
|
|
|
obj.remaining = ((state.frames - obj.frame) / state.info.fps) / obj.speed;
|
|
|
|
obj.estimated = state.info.seconds / obj.speed;
|
|
|
|
if (obj.estimated > estimated) {
|
|
|
|
estimated = obj.estimated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj.frame) {
|
|
|
|
//log.info(`${input.name} ${obj.frame}/${input.frames} ${Math.round(obj.progress * 1000) / 10}% ${Math.round(obj.remaining)} seconds remaining of ${Math.round(obj.estimated)}`);
|
|
|
|
this.onProgress(obj);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public cancel () {
|
|
|
|
if (this.child) {
|
|
|
|
this.child.kill();
|
|
|
|
this.log.info(`Stopped exporting sequence with ffmpeg`);
|
2020-02-21 18:34:22 +00:00
|
|
|
}
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
/**
|
|
|
|
* Clears a specific frame from the tmp directory
|
|
|
|
*
|
|
|
|
* @param {integer} frame Integer of frame to clear
|
|
|
|
*
|
|
|
|
* @returns {boolean} True if successful, false if not
|
|
|
|
**/
|
2020-02-21 18:34:22 +00:00
|
|
|
public async clear (state : any) {
|
|
|
|
const padded : string = this.padded_frame(state.frame);
|
|
|
|
let ext : string = 'png';
|
2019-08-04 21:42:27 +00:00
|
|
|
let tmppath : string;
|
|
|
|
let fileExists : boolean;
|
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
tmppath = join(this.TMPDIR, `${state.hash}-export-${padded}.${ext}`);
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
try {
|
|
|
|
fileExists = await exists(tmppath);
|
|
|
|
} catch (err) {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.error(err);
|
2019-08-04 21:42:27 +00:00
|
|
|
}
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
if (!fileExists) return false;
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
try {
|
|
|
|
await unlink(tmppath);
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.info(`Cleared frame ${tmppath}`);
|
2019-08-04 21:42:27 +00:00
|
|
|
} catch (err) {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.error(err);
|
2019-08-04 21:42:27 +00:00
|
|
|
}
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
return true;
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
/**
|
2020-02-21 06:58:56 +00:00
|
|
|
* Deletes all frames in temp directory.
|
2019-08-04 21:42:27 +00:00
|
|
|
*
|
|
|
|
**/
|
|
|
|
public async clearAll () {
|
|
|
|
const tmppath : string = this.TMPDIR;
|
|
|
|
let files : any;
|
2019-06-09 00:51:00 +00:00
|
|
|
try {
|
2019-08-04 21:42:27 +00:00
|
|
|
files = await readdir(tmppath);
|
2019-06-09 00:51:00 +00:00
|
|
|
} catch (err) {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.error(err);
|
2019-08-04 21:42:27 +00:00
|
|
|
}
|
2020-02-21 18:34:22 +00:00
|
|
|
files = files.filter((file : string) => {
|
|
|
|
if (file.indexOf('-export-') !== -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2019-08-04 21:42:27 +00:00
|
|
|
if (files) {
|
|
|
|
files.forEach(async (file : string, index : any) => {
|
|
|
|
try {
|
|
|
|
await unlink(join(tmppath, file));
|
|
|
|
} catch (err) {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.error(err);
|
2019-08-04 21:42:27 +00:00
|
|
|
}
|
|
|
|
});
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-04 21:42:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if mcopy temp directory exists. If it doesn't,
|
2020-02-21 06:58:56 +00:00
|
|
|
* creates it.
|
2019-08-04 21:42:27 +00:00
|
|
|
**/
|
|
|
|
private async checkDir () {
|
|
|
|
let fileExists : boolean;
|
|
|
|
try {
|
|
|
|
fileExists = await exists(this.TMPDIR);
|
|
|
|
} catch (err) {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.error('Error checking for tmp dir', err);
|
2019-08-04 21:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileExists) {
|
|
|
|
try {
|
|
|
|
await mkdir(this.TMPDIR);
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.info(`Created tmpdir ${this.TMPDIR}`);
|
2019-08-04 21:42:27 +00:00
|
|
|
} catch (err) {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.error('Error creating tmp dir', err);
|
2019-08-04 21:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this.clearAll();
|
|
|
|
} catch (err) {
|
2019-08-04 21:54:38 +00:00
|
|
|
this.log.error(err);
|
2019-08-04 21:42:27 +00:00
|
|
|
}
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
module.exports = (sys : any) => {
|
|
|
|
return new FFMPEG(sys);
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|