2019-06-09 00:51:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
let system : any = {};
|
|
|
|
let TMPDIR : string;
|
|
|
|
|
|
|
|
function padded_frame (i : number) {
|
|
|
|
let len = (i + '').length;
|
|
|
|
let str = i + '';
|
|
|
|
for (let x = 0; x < 5 - len; x++) {
|
|
|
|
str = '0' + str;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function frame (state : any, light : any) {
|
2019-08-04 21:20:45 +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 = 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}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
//console.dir(state)
|
2019-07-29 16:45:13 +00:00
|
|
|
|
2019-06-18 17:47:55 +00:00
|
|
|
//if (system.platform !== 'nix') {
|
2019-06-09 00:51:00 +00:00
|
|
|
ext = 'png';
|
2019-06-18 17:47:55 +00:00
|
|
|
//}
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-08-04 21:20:45 +00:00
|
|
|
tmpoutput = join(TMPDIR, `export-${padded}.${ext}`);
|
|
|
|
|
2019-06-09 00:51:00 +00:00
|
|
|
rgb = rgb.map((e : string) => {
|
|
|
|
return parseInt(e);
|
|
|
|
});
|
2019-08-04 21:20:45 +00:00
|
|
|
//
|
|
|
|
cmd = `ffmpeg -y -i "${video}" -vf "select='gte(n\\,${frameNum})'${scale}" -vframes 1 -compression_algo raw -pix_fmt rgb24 "${tmpoutput}"`;
|
2019-06-09 00:51:00 +00:00
|
|
|
cmd2 = `convert "${tmpoutput}" -resize ${w}x${h} -size ${w}x${h} xc:"rgb(${rgb[0]},${rgb[1]},${rgb[2]})" +swap -compose Darken -composite "${tmpoutput}"`;
|
|
|
|
|
|
|
|
//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"
|
|
|
|
|
|
|
|
try {
|
|
|
|
console.log(cmd);
|
|
|
|
output = await exec(cmd);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
if (output && output.stdout) console.log(`"${output.stdout}"`);
|
|
|
|
|
|
|
|
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}"`);
|
2019-06-25 01:11:14 +00:00
|
|
|
return tmpoutput
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function frames (video : string, obj : any) {
|
|
|
|
let tmppath = TMPDIR;
|
|
|
|
let ext = 'tif';
|
|
|
|
let tmpoutput;
|
|
|
|
|
2019-06-18 17:47:55 +00:00
|
|
|
//if (system.platform !== 'nix') {
|
2019-06-09 00:51:00 +00:00
|
|
|
ext = 'png';
|
2019-06-18 17:47:55 +00:00
|
|
|
//}
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-06-18 20:57:35 +00:00
|
|
|
tmpoutput = join(tmppath, `export-%05d.${ext}`);
|
2019-06-09 00:51:00 +00:00
|
|
|
try {
|
2019-06-09 01:43:14 +00:00
|
|
|
await mkdir(tmppath);
|
2019-06-09 00:51:00 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
//ffmpeg -i "${video}" -compression_algo raw -pix_fmt rgb24 "${tmpoutput}"
|
|
|
|
}
|
|
|
|
|
|
|
|
async function clear (frame : number) {
|
|
|
|
let padded = padded_frame(frame);
|
|
|
|
let ext = 'tif';
|
|
|
|
let tmppath;
|
|
|
|
let tmpoutput;
|
|
|
|
let cmd;
|
2019-06-09 01:43:14 +00:00
|
|
|
let fileExists;
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-06-18 17:47:55 +00:00
|
|
|
//if (system.platform !== 'nix') {
|
2019-06-09 00:51:00 +00:00
|
|
|
ext = 'png';
|
2019-06-18 17:47:55 +00:00
|
|
|
//}
|
2019-06-09 00:51:00 +00:00
|
|
|
|
2019-06-18 20:57:35 +00:00
|
|
|
tmppath = join(TMPDIR, `export-${padded}.${ext}`);
|
2019-06-09 00:51:00 +00:00
|
|
|
|
|
|
|
try {
|
2019-06-09 01:43:14 +00:00
|
|
|
fileExists = await exists(tmppath);
|
2019-06-09 00:51:00 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exists) return false;
|
|
|
|
|
|
|
|
try {
|
2019-06-09 01:43:14 +00:00
|
|
|
await unlink(tmppath);
|
2019-06-09 00:51:00 +00:00
|
|
|
console.log(`Cleared frame ${tmppath}`);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function clearAll () {
|
|
|
|
let tmppath = TMPDIR;
|
|
|
|
let files;
|
|
|
|
try {
|
2019-06-09 01:43:14 +00:00
|
|
|
files = await readdir(tmppath);
|
2019-06-09 00:51:00 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
if (files) {
|
|
|
|
files.forEach(async (file : string, index : any) => {
|
|
|
|
try {
|
2019-06-18 20:57:35 +00:00
|
|
|
await unlink(join(tmppath, file));
|
2019-06-09 00:51:00 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkDir () {
|
2019-06-09 01:43:14 +00:00
|
|
|
let fileExists;
|
2019-06-09 00:51:00 +00:00
|
|
|
try {
|
2019-06-09 01:43:14 +00:00
|
|
|
fileExists = await exists(TMPDIR);
|
2019-06-09 00:51:00 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error('Error checking for tmp dir', err);
|
|
|
|
}
|
|
|
|
|
2019-06-15 15:06:57 +00:00
|
|
|
if (!fileExists) {
|
2019-06-09 00:51:00 +00:00
|
|
|
try {
|
2019-06-09 01:43:14 +00:00
|
|
|
await mkdir(TMPDIR);
|
2019-06-09 00:51:00 +00:00
|
|
|
console.log(`Created tmpdir ${TMPDIR}`);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error creating tmp dir', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await clearAll();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = (sys : any) => {
|
|
|
|
system = sys;
|
2019-06-18 20:57:35 +00:00
|
|
|
TMPDIR = join(system.tmp, 'mcopy_digital');
|
2019-06-09 00:51:00 +00:00
|
|
|
|
|
|
|
checkDir();
|
|
|
|
|
|
|
|
return {
|
|
|
|
frames,
|
|
|
|
frame,
|
|
|
|
clear,
|
|
|
|
clearAll
|
|
|
|
}
|
|
|
|
}
|