2019-06-09 00:51:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-08-04 21:31:27 +00:00
|
|
|
/** @module ffmpeg **/
|
|
|
|
|
2019-06-09 00:51:00 +00:00
|
|
|
import uuid from 'uuid/v4';
|
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';
|
2019-06-09 00:51:00 +00:00
|
|
|
//const spawn = require('spawn');
|
2019-06-09 01:43:14 +00:00
|
|
|
import { exit } from 'exit';
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
/** @class FFMPEG **/
|
|
|
|
|
|
|
|
class FFMPEG {
|
|
|
|
private system : any;
|
|
|
|
private log : any;
|
|
|
|
private id : string = 'ffmpeg';
|
|
|
|
private TMPDIR : string;
|
|
|
|
constructor (sys : any) {
|
|
|
|
this.system = sys;
|
|
|
|
this.TMPDIR = join(this.system.tmp, 'mcopy_digital');
|
|
|
|
this.init()
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|
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 + '';
|
|
|
|
for (let x = 0; x < 5 - len; x++) {
|
|
|
|
str = '0' + str;
|
|
|
|
}
|
|
|
|
return str;
|
2019-08-04 21:20:45 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
**/
|
|
|
|
public async frame (state : any, light : any) {
|
|
|
|
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);
|
|
|
|
let ext : string = 'tif';
|
|
|
|
let rgb : any[] = light.color;
|
|
|
|
let tmpoutput : string;
|
|
|
|
let cmd : string;
|
|
|
|
let output : any;
|
|
|
|
let cmd2 : string;
|
|
|
|
let output2 : any;
|
|
|
|
|
|
|
|
let scale : string = '';
|
|
|
|
if (w && h) {
|
|
|
|
scale = `,scale=${w}:${h}`;
|
|
|
|
}
|
2019-07-29 16:45:13 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
//console.dir(state)
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
//if (system.platform !== 'nix') {
|
|
|
|
ext = 'png';
|
|
|
|
//}
|
2019-08-04 21:20:45 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
tmpoutput = join(this.TMPDIR, `export-${padded}.${ext}`);
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
rgb = rgb.map((e : string) => {
|
|
|
|
return parseInt(e);
|
|
|
|
});
|
|
|
|
//
|
|
|
|
cmd = `ffmpeg -y -i "${video}" -vf "select='gte(n\\,${frameNum})'${scale}" -vframes 1 -compression_algo raw -pix_fmt rgb24 "${tmpoutput}"`;
|
|
|
|
cmd2 = `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:42:27 +00:00
|
|
|
console.log(cmd);
|
|
|
|
output = await exec(cmd);
|
2019-06-09 00:51:00 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2019-08-04 21:42:27 +00:00
|
|
|
if (output && output.stdout) console.log(`"${output.stdout}"`);
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
if (rgb[0] !== 255 || rgb[1] !== 255 || rgb[2] !== 255) {
|
|
|
|
try {
|
|
|
|
console.log(cmd2);
|
|
|
|
output2 = await exec(cmd2);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output2 && output2.stdout) console.log(`"${output2.stdout}"`);
|
|
|
|
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 {?}
|
|
|
|
**/
|
|
|
|
public async frames (video : string, obj : any) {
|
|
|
|
const tmppath : string = this.TMPDIR;
|
|
|
|
let ext : string = 'tif';
|
|
|
|
let tmpoutput : string;
|
|
|
|
|
|
|
|
//if (system.platform !== 'nix') {
|
|
|
|
ext = 'png';
|
|
|
|
//}
|
|
|
|
|
|
|
|
tmpoutput = join(tmppath, `export-%05d.${ext}`);
|
|
|
|
try {
|
|
|
|
await mkdir(tmppath);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
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}"
|
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
|
|
|
|
**/
|
|
|
|
public async clear (frame : number) {
|
|
|
|
const padded : string = this.padded_frame(frame);
|
|
|
|
let ext : string = 'tif';
|
|
|
|
let tmppath : string;
|
|
|
|
let tmpoutput : string;
|
|
|
|
let cmd : string;
|
|
|
|
let fileExists : boolean;
|
|
|
|
|
|
|
|
//if (system.platform !== 'nix') {
|
|
|
|
ext = 'png';
|
|
|
|
//}
|
|
|
|
|
|
|
|
tmppath = join(this.TMPDIR, `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) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
if (!exists) return false;
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:42:27 +00:00
|
|
|
try {
|
|
|
|
await unlink(tmppath);
|
|
|
|
console.log(`Cleared frame ${tmppath}`);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* Delete all frames in temp directory.
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
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:42:27 +00:00
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
if (files) {
|
|
|
|
files.forEach(async (file : string, index : any) => {
|
|
|
|
try {
|
|
|
|
await unlink(join(tmppath, file));
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
});
|
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,
|
|
|
|
* create it.
|
|
|
|
**/
|
|
|
|
private async checkDir () {
|
|
|
|
let fileExists : boolean;
|
|
|
|
try {
|
|
|
|
fileExists = await exists(this.TMPDIR);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error checking for tmp dir', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileExists) {
|
|
|
|
try {
|
|
|
|
await mkdir(this.TMPDIR);
|
|
|
|
console.log(`Created tmpdir ${this.TMPDIR}`);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error creating tmp dir', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this.clearAll();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
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
|
|
|
}
|