2019-03-21 18:58:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-07-07 04:02:01 +00:00
|
|
|
import sharp from 'sharp';
|
2019-08-15 19:25:33 +00:00
|
|
|
import { default as animated } from 'animated-gif-detector';
|
2019-07-07 04:02:01 +00:00
|
|
|
import { extname } from 'path';
|
|
|
|
import { readFile } from 'fs-extra';
|
2019-06-15 15:06:57 +00:00
|
|
|
import { delay } from 'delay';
|
2020-02-21 18:34:22 +00:00
|
|
|
import { createHash } from 'crypto';
|
2019-03-21 18:58:00 +00:00
|
|
|
|
2019-08-25 18:59:39 +00:00
|
|
|
/**
|
|
|
|
* @module FilmOut
|
|
|
|
**/
|
2019-08-15 19:25:33 +00:00
|
|
|
|
2019-06-18 19:42:30 +00:00
|
|
|
class FilmOut {
|
|
|
|
private id : string = 'filmout';
|
2019-07-07 04:02:01 +00:00
|
|
|
private videoExtensions : string[] = ['.mpg', '.mpeg', '.mov', '.mkv', '.avi', '.mp4'];
|
|
|
|
private stillExtensions : string[] = ['.tif', '.tiff', '.png', '.jpg', '.jpeg', '.bmp'];
|
|
|
|
private gifExtension : string = '.gif';
|
2019-03-21 22:03:53 +00:00
|
|
|
public state : any = {
|
|
|
|
frame : 0,
|
|
|
|
frames : 0,
|
2019-07-07 04:02:01 +00:00
|
|
|
still : false,
|
2019-03-21 22:03:53 +00:00
|
|
|
path : null,
|
|
|
|
fileName : null,
|
|
|
|
info : {},
|
|
|
|
dir : true,
|
|
|
|
enabled : false
|
|
|
|
};
|
|
|
|
private display : any;
|
|
|
|
private ffmpeg : any;
|
|
|
|
private ffprobe : any;
|
|
|
|
private light : any;
|
|
|
|
private ipc : any;
|
|
|
|
private ui : any;
|
|
|
|
private log : any;
|
|
|
|
/**
|
2019-08-25 18:59:39 +00:00
|
|
|
* @constructor
|
|
|
|
* Builds FilmOut class with display, ffmpeg, ffprobe, ui and light as internal properties.
|
2019-03-21 22:03:53 +00:00
|
|
|
*
|
2019-08-25 18:59:39 +00:00
|
|
|
* @param {object} display Display object for showing frames
|
|
|
|
* @param {object} ffmpeg FFMPEG wrapper
|
|
|
|
* @param {object} ffprobe FFPROBE wrapper for file info
|
|
|
|
* @param {object} ui Electron ui object
|
|
|
|
* @param {object} light Light device object
|
2019-03-21 22:03:53 +00:00
|
|
|
**/
|
|
|
|
constructor (display : any, ffmpeg : any, ffprobe : any, ui : any, light : any) {
|
|
|
|
this.display = display;
|
|
|
|
this.ffmpeg = ffmpeg;
|
|
|
|
this.ffprobe = ffprobe;
|
|
|
|
this.ui = ui;
|
|
|
|
this.light = light;
|
|
|
|
this.init();
|
2019-03-21 18:58:00 +00:00
|
|
|
}
|
2019-03-21 22:03:53 +00:00
|
|
|
/**
|
2019-08-25 18:59:39 +00:00
|
|
|
* Async function for requiring log, ipcMain and bind events.
|
2019-03-21 22:03:53 +00:00
|
|
|
**/
|
|
|
|
private async init () {
|
2019-06-15 15:06:57 +00:00
|
|
|
const Log = require('log');
|
2019-06-18 19:42:30 +00:00
|
|
|
this.log = await Log({ label : this.id });
|
2019-03-21 22:03:53 +00:00
|
|
|
this.ipc = require('electron').ipcMain;
|
|
|
|
this.listen();
|
2019-03-21 18:58:00 +00:00
|
|
|
}
|
2019-03-21 22:03:53 +00:00
|
|
|
/**
|
2019-08-25 18:59:39 +00:00
|
|
|
*
|
2019-03-21 22:03:53 +00:00
|
|
|
**/
|
|
|
|
private listen () {
|
2019-06-18 19:42:30 +00:00
|
|
|
this.ipc.on(this.id, this.onConnect.bind(this));
|
2019-07-07 04:02:01 +00:00
|
|
|
|
2019-06-18 17:47:55 +00:00
|
|
|
this.ipc.on('focus', this.focus.bind(this));
|
2019-06-18 19:42:30 +00:00
|
|
|
this.ipc.on('field', this.field.bind(this));
|
|
|
|
this.ipc.on('meter', this.meter.bind(this));
|
|
|
|
this.ipc.on('filmout_close', this.close.bind(this));
|
2019-06-27 00:08:49 +00:00
|
|
|
this.ipc.on('preview', this.preview.bind(this));
|
2019-06-25 16:13:15 +00:00
|
|
|
this.ipc.on('preview_frame', this.previewFrame.bind(this));
|
2020-02-21 18:34:22 +00:00
|
|
|
this.ipc.on('display', this.onDisplay.bind(this));
|
2020-03-09 19:46:06 +00:00
|
|
|
this.ipc.on('pre_export', this.onPreExport.bind(this));
|
|
|
|
|
|
|
|
this.ffmpeg.onProgress = (obj : any) => {
|
|
|
|
this.ui.send('pre_export_progress', { progress: obj });
|
|
|
|
}
|
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Create a hash of a string.
|
|
|
|
*
|
|
|
|
* @param {string} data Data to produce hash of
|
|
|
|
*/
|
|
|
|
private hash (data : string) {
|
|
|
|
return createHash('sha1').update(data).digest('hex');
|
2019-03-21 22:03:53 +00:00
|
|
|
}
|
|
|
|
/**
|
2019-08-25 18:59:39 +00:00
|
|
|
* Sets filmout direction.
|
2019-03-21 22:03:53 +00:00
|
|
|
*
|
2019-08-25 18:59:39 +00:00
|
|
|
* @param {boolean} dir Direction of filmout
|
2019-03-21 22:03:53 +00:00
|
|
|
**/
|
|
|
|
public set (dir : boolean) {
|
|
|
|
this.state.dir = dir;
|
|
|
|
}
|
|
|
|
/**
|
2019-08-25 18:59:39 +00:00
|
|
|
* Moves filmout a frame at a time.
|
2019-03-21 22:03:53 +00:00
|
|
|
**/
|
|
|
|
public async move () {
|
|
|
|
let start : number = +new Date();
|
2019-07-07 04:02:01 +00:00
|
|
|
if (this.state.still) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-21 22:03:53 +00:00
|
|
|
if (this.state.dir) {
|
|
|
|
this.state.frame++;
|
|
|
|
} else {
|
|
|
|
this.state.frame--;
|
|
|
|
}
|
|
|
|
if (this.state.frame < 1) {
|
|
|
|
this.state.frame = 1;
|
|
|
|
}
|
|
|
|
return (+new Date()) - start;
|
|
|
|
}
|
|
|
|
/**
|
2019-08-25 18:59:39 +00:00
|
|
|
* Begin the process of exporting single frames from the video for display.
|
2019-03-21 22:03:53 +00:00
|
|
|
**/
|
2020-02-21 18:34:22 +00:00
|
|
|
async start () {
|
|
|
|
let path;
|
|
|
|
|
2019-03-21 22:03:53 +00:00
|
|
|
try {
|
2020-02-21 18:34:22 +00:00
|
|
|
path = await this.ffmpeg.frame(this.state, this.light.state);
|
2019-03-21 22:03:53 +00:00
|
|
|
} catch (err) {
|
2019-06-27 00:08:49 +00:00
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
2019-03-21 22:03:53 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2020-02-21 18:34:22 +00:00
|
|
|
|
|
|
|
await this.display.show(path);
|
2019-03-21 22:03:53 +00:00
|
|
|
await delay(20);
|
2019-03-21 18:58:00 +00:00
|
|
|
}
|
2019-07-07 04:02:01 +00:00
|
|
|
/**
|
2019-08-25 18:59:39 +00:00
|
|
|
* Ends the filmout process and closes the display.
|
2019-03-21 22:03:53 +00:00
|
|
|
**/
|
|
|
|
private async end () {
|
|
|
|
await delay(20);
|
2019-03-22 02:33:30 +00:00
|
|
|
this.display.hide();
|
2019-03-21 18:58:00 +00:00
|
|
|
}
|
2019-03-21 22:03:53 +00:00
|
|
|
/**
|
2019-06-18 19:42:30 +00:00
|
|
|
* Use a video file as a film out source on "projector"
|
2019-03-21 22:03:53 +00:00
|
|
|
*
|
2019-08-25 18:59:39 +00:00
|
|
|
* @param {object} evt Original connect event
|
|
|
|
* @param {object} arg Arguments from ipc message
|
2019-03-21 22:03:53 +00:00
|
|
|
**/
|
2019-06-18 19:42:30 +00:00
|
|
|
async onConnect (evt : any, arg : any) {
|
2019-07-07 04:02:01 +00:00
|
|
|
let frames : number = 0;
|
|
|
|
let isAnimated : boolean = false;
|
|
|
|
let info : any;
|
|
|
|
let ext : string;
|
2019-03-21 18:58:00 +00:00
|
|
|
|
2019-07-07 04:02:01 +00:00
|
|
|
ext = extname(arg.fileName.toLowerCase());
|
|
|
|
|
2019-08-25 18:59:39 +00:00
|
|
|
//console.dir(arg)
|
|
|
|
//console.log(ext)
|
2019-07-07 04:02:01 +00:00
|
|
|
|
|
|
|
if (ext === this.gifExtension) {
|
|
|
|
try {
|
|
|
|
isAnimated = await this.isGifAnimated(arg.path);
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
await this.ui.send(this.id, { valid : false });
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.state.still = !isAnimated;
|
|
|
|
} else if (this.stillExtensions.indexOf(ext) !== -1) {
|
|
|
|
this.state.still = true;
|
|
|
|
} else if (this.videoExtensions.indexOf(ext) !== -1) {
|
|
|
|
this.state.still = false;
|
|
|
|
} else {
|
|
|
|
this.log.error(`File is not of a valid file type`, 'FILMOUT', true, true);
|
2019-03-21 22:03:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-21 18:58:00 +00:00
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
try {
|
|
|
|
await this.ffmpeg.clearAll();
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2019-07-07 04:02:01 +00:00
|
|
|
if (this.state.still) {
|
|
|
|
try {
|
|
|
|
info = await this.stillInfo(arg.path);
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
this.state.enabled = false;
|
|
|
|
await this.ui.send(this.id, { valid : false });
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
frames = 1;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
info = await this.ffprobe.info(arg.path);
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
this.state.enabled = false;
|
|
|
|
await this.ui.send(this.id, { valid : false });
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
frames = await this.ffprobe.frames(arg.path);
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
this.state.enabled = false;
|
|
|
|
await this.ui.send(this.id, { valid : false });
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 18:59:39 +00:00
|
|
|
//console.dir(info)
|
2019-03-21 22:03:53 +00:00
|
|
|
this.state.frame = 0;
|
|
|
|
this.state.path = arg.path;
|
|
|
|
this.state.fileName = arg.fileName;
|
|
|
|
this.state.frames = frames;
|
|
|
|
this.state.info = info;
|
2020-02-21 18:34:22 +00:00
|
|
|
this.state.hash = this.hash(arg.path);
|
2019-03-21 22:03:53 +00:00
|
|
|
|
2020-03-09 19:46:06 +00:00
|
|
|
if (info.seconds) {
|
|
|
|
this.state.seconds = info.seconds;
|
|
|
|
} else if (info.fps && frames) {
|
|
|
|
this.state.seconds = frames / info.fps;
|
|
|
|
}
|
|
|
|
|
2019-06-18 19:42:30 +00:00
|
|
|
this.log.info(`Opened ${this.state.fileName}`, 'FILMOUT', true, true);
|
|
|
|
this.log.info(`Frames : ${frames}`, 'FILMOUT', true, true);
|
2019-03-21 22:03:53 +00:00
|
|
|
this.state.enabled = true;
|
2019-06-18 19:42:30 +00:00
|
|
|
return await this.ui.send(this.id, { valid : true, state : JSON.stringify(this.state) });
|
2019-03-21 22:03:53 +00:00
|
|
|
}
|
2020-01-20 06:15:20 +00:00
|
|
|
|
2020-02-21 18:34:22 +00:00
|
|
|
/**
|
|
|
|
* Pre-export all frames from video for display.
|
|
|
|
*
|
|
|
|
* @param {object} evt IPC event
|
|
|
|
* @param {object} arg IPC args
|
|
|
|
*/
|
|
|
|
async onPreExport (evt : Event, arg : any) {
|
|
|
|
if (!this.state.path) {
|
|
|
|
return await this.ui.send('pre_export', { complete : false, err : 'No file to pre export.' });
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.ffmpeg.frames(this.state);
|
|
|
|
} catch (err) {
|
|
|
|
return await this.ui.send('pre_export', { complete : false, err });
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.ui.send('pre_export', { complete : true });
|
|
|
|
}
|
|
|
|
|
2019-07-07 04:02:01 +00:00
|
|
|
/**
|
|
|
|
* Return true if gif is animated, false if it is a still
|
2019-08-25 18:59:39 +00:00
|
|
|
*
|
|
|
|
* @param {string} pathStr Path to gif to check
|
|
|
|
*
|
|
|
|
* @returns {boolean} Whether or not gif is animated
|
2019-07-07 04:02:01 +00:00
|
|
|
**/
|
|
|
|
async isGifAnimated (pathStr : string) {
|
|
|
|
let gifBuffer : Buffer;
|
|
|
|
try {
|
|
|
|
gifBuffer = await readFile(pathStr);
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return animated(gifBuffer);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Return information on a still image using the sharp module
|
2019-08-25 18:59:39 +00:00
|
|
|
*
|
|
|
|
* @param {string} pathStr Path to gif to check
|
|
|
|
*
|
|
|
|
* @returns {object} Info about still from sharp
|
2019-07-07 04:02:01 +00:00
|
|
|
**/
|
|
|
|
async stillInfo (pathStr : string) {
|
|
|
|
return sharp(pathStr).metadata();
|
|
|
|
}
|
|
|
|
/**
|
2019-08-25 18:59:39 +00:00
|
|
|
* Preview a frame from the selected video.
|
2019-07-07 04:02:01 +00:00
|
|
|
*
|
2019-08-25 18:59:39 +00:00
|
|
|
* @param {object} evt Original event
|
|
|
|
* @param {object} arg Arguments from message
|
2019-07-07 04:02:01 +00:00
|
|
|
**/
|
2019-06-25 16:13:15 +00:00
|
|
|
async previewFrame (evt : any, arg : any) {
|
|
|
|
const state : any = JSON.parse(JSON.stringify(this.state));
|
|
|
|
let path : string;
|
2019-06-27 00:08:49 +00:00
|
|
|
|
2019-06-25 16:13:15 +00:00
|
|
|
state.frame = arg.frame;
|
2019-06-27 00:08:49 +00:00
|
|
|
|
2019-06-25 16:13:15 +00:00
|
|
|
try {
|
|
|
|
path = await this.ffmpeg.frame(state, { color : [255, 255, 255] });
|
|
|
|
} catch (err) {
|
2019-06-27 00:08:49 +00:00
|
|
|
this.log.error(err, 'FILMOUT', true, true);;
|
2019-06-25 16:13:15 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2020-02-21 18:34:22 +00:00
|
|
|
|
2019-06-25 16:13:15 +00:00
|
|
|
this.ui.send('preview_frame', { path, frame : arg.frame })
|
|
|
|
}
|
2019-07-07 04:02:01 +00:00
|
|
|
/**
|
2020-02-21 18:34:22 +00:00
|
|
|
* Open a single frame in a display window to preview filmout.
|
2019-07-07 04:02:01 +00:00
|
|
|
*
|
2019-08-25 18:59:39 +00:00
|
|
|
* @param {object} evt Original event
|
|
|
|
* @param {object} arg Arguments from message
|
2019-07-07 04:02:01 +00:00
|
|
|
**/
|
2019-06-25 16:13:15 +00:00
|
|
|
async preview (evt : any, arg : any) {
|
2019-06-27 00:08:49 +00:00
|
|
|
const state : any = JSON.parse(JSON.stringify(this.state));
|
|
|
|
let path : string;
|
|
|
|
|
|
|
|
state.frame = arg.frame;
|
|
|
|
|
|
|
|
this.log.info(`Previewing frame ${state.frame} of ${state.fileName}`);
|
2020-02-21 18:34:22 +00:00
|
|
|
|
2019-06-27 00:08:49 +00:00
|
|
|
try {
|
|
|
|
path = await this.ffmpeg.frame(state, { color : [255, 255, 255] });
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
throw err;
|
|
|
|
}
|
2019-06-25 16:13:15 +00:00
|
|
|
|
2019-06-27 00:08:49 +00:00
|
|
|
try {
|
|
|
|
await this.display.open();
|
2020-02-21 18:34:22 +00:00
|
|
|
await this.display.show(path);
|
2019-06-27 00:08:49 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
}
|
2019-06-25 16:13:15 +00:00
|
|
|
}
|
2019-07-07 04:02:01 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-06-18 19:42:30 +00:00
|
|
|
async focus (evt : any, arg : any) {
|
2019-06-27 00:08:49 +00:00
|
|
|
this.log.info(`Showing focus screen`);
|
2019-06-18 19:42:30 +00:00
|
|
|
try {
|
|
|
|
await this.display.open();
|
|
|
|
await this.display.focus();
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 04:02:01 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-06-18 19:42:30 +00:00
|
|
|
async field (evt : any, arg : any) {
|
2019-08-25 19:26:43 +00:00
|
|
|
const ratio : number = arg.ratio;
|
2019-06-27 00:08:49 +00:00
|
|
|
this.log.info(`Showing field guide screen`);
|
2019-06-18 19:42:30 +00:00
|
|
|
try {
|
|
|
|
await this.display.open();
|
2019-08-25 19:26:43 +00:00
|
|
|
await this.display.field(ratio);
|
2019-06-18 19:42:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 04:02:01 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-06-18 19:42:30 +00:00
|
|
|
async meter (evt : any, arg : any) {
|
2019-06-27 00:08:49 +00:00
|
|
|
this.log.info(`Showing meter screen`);
|
2019-06-18 19:42:30 +00:00
|
|
|
try {
|
|
|
|
await this.display.open();
|
|
|
|
await this.display.meter();
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 04:02:01 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-06-18 19:42:30 +00:00
|
|
|
async close (evt : any, arg : any) {
|
|
|
|
try {
|
|
|
|
await this.display.hide();
|
|
|
|
await this.display.close();
|
|
|
|
} catch (err) {
|
|
|
|
this.log.error(err, 'FILMOUT', true, true);
|
|
|
|
}
|
2019-06-18 17:47:55 +00:00
|
|
|
}
|
2019-07-07 04:02:01 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
2019-06-27 00:08:49 +00:00
|
|
|
onDisplay (evt : any, arg : any) {
|
|
|
|
this.display.change(arg.display);
|
|
|
|
this.log.info(`Changing the display to ${arg.display}`);
|
|
|
|
}
|
2019-03-21 18:58:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 22:03:53 +00:00
|
|
|
module.exports = (display : any, ffmpeg : any, ffprobe : any, ui : any, light : any) => {
|
2019-06-18 19:42:30 +00:00
|
|
|
return new FilmOut(display, ffmpeg, ffprobe, ui, light);
|
2019-06-14 15:31:13 +00:00
|
|
|
}
|