2019-02-08 17:46:58 +00:00
|
|
|
'use strict';
|
2019-06-09 00:51:00 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2019-08-04 22:04:06 +00:00
|
|
|
/** @module FFPROBE **/
|
2019-06-09 01:43:14 +00:00
|
|
|
const fs_extra_1 = require("fs-extra");
|
2019-06-25 01:11:14 +00:00
|
|
|
const path_1 = require("path");
|
2019-06-09 01:43:14 +00:00
|
|
|
const exec_1 = require("exec");
|
2019-02-08 17:46:58 +00:00
|
|
|
//const spawn = require('spawn');
|
2019-02-08 23:21:16 +00:00
|
|
|
//const exit = require('exit');
|
2019-08-04 22:04:06 +00:00
|
|
|
class FFPROBE {
|
|
|
|
constructor(sys) {
|
2020-01-20 16:51:15 +00:00
|
|
|
this.bin = sys.deps.ffprobe;
|
2019-08-04 22:04:06 +00:00
|
|
|
}
|
2020-03-09 19:46:06 +00:00
|
|
|
/**
|
|
|
|
* Parse the fps entry into a float representing the fps of a video
|
|
|
|
**/
|
|
|
|
parseFps(fpsStr) {
|
|
|
|
let fps = 30.0;
|
|
|
|
let parts;
|
|
|
|
if (fpsStr.indexOf('/') !== -1) {
|
|
|
|
parts = fpsStr.split('/');
|
|
|
|
fps = parseFloat(parts[0]) / parseFloat(parts[1]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fps = parseFloat(fpsStr);
|
|
|
|
}
|
|
|
|
return fps;
|
|
|
|
}
|
2019-08-04 22:04:06 +00:00
|
|
|
/**
|
|
|
|
* Get info on a video in json format. Use for filmout.
|
|
|
|
*
|
|
|
|
* @param {string} video Path to video
|
|
|
|
*
|
|
|
|
* @returns {object} Video info in an object
|
|
|
|
**/
|
|
|
|
async info(video) {
|
2020-01-20 16:51:15 +00:00
|
|
|
const cmd = `${this.bin} -v quiet -print_format json -show_format -show_streams "${video}"`;
|
2019-08-04 22:04:06 +00:00
|
|
|
let fileExists;
|
|
|
|
let raw;
|
|
|
|
let json;
|
|
|
|
let vid; //whether video has stream with video data
|
|
|
|
try {
|
|
|
|
fileExists = await fs_extra_1.exists(video);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return exit(err, 5);
|
|
|
|
}
|
|
|
|
if (!fileExists) {
|
|
|
|
//return exit(`File ${video} does not exist`, 6);
|
|
|
|
console.error(new Error(`File ${video} does not exist`));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
console.log(cmd);
|
|
|
|
raw = await exec_1.exec(cmd);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
//return exit(err, 7);
|
|
|
|
console.error(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
json = JSON.parse(raw.stdout);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return raw.stdout;
|
|
|
|
}
|
2020-03-09 19:46:06 +00:00
|
|
|
if (json.format && json.format.duration) {
|
|
|
|
json.seconds = parseFloat(json.format.duration);
|
|
|
|
}
|
2019-08-04 22:04:06 +00:00
|
|
|
if (json && json.streams) {
|
|
|
|
vid = json.streams.find((stream) => {
|
|
|
|
if (stream.width && stream.height)
|
|
|
|
return stream;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (vid) {
|
|
|
|
json.width = vid.width;
|
|
|
|
json.height = vid.height;
|
2020-03-09 19:46:06 +00:00
|
|
|
json.fps = this.parseFps(vid.r_frame_rate);
|
2019-08-04 22:04:06 +00:00
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Count the number of frames in the video using one of two methods.
|
|
|
|
* The first uses -select_streams and is very fast. The second uses
|
|
|
|
* -count_frames and is VERY slow.
|
|
|
|
*
|
|
|
|
* @param {string} video Path to video
|
|
|
|
*
|
|
|
|
* @returns {integer} Number of frames in video
|
|
|
|
**/
|
|
|
|
async frames(video) {
|
|
|
|
const ext = path_1.extname(video.toLowerCase());
|
2020-01-21 16:38:50 +00:00
|
|
|
let cmd = `${this.bin} -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 "${video}"`;
|
|
|
|
let backup_cmd = `${this.bin} -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 "${video}"`;
|
2019-08-15 19:25:33 +00:00
|
|
|
let gif_cmd = `identify -format "%n\n" "${video}" | head -1`;
|
2019-08-04 22:04:06 +00:00
|
|
|
let fileExists;
|
|
|
|
let raw;
|
|
|
|
let frames;
|
|
|
|
try {
|
|
|
|
fileExists = await fs_extra_1.exists(video);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
//return exit(err, 5);
|
|
|
|
console.error(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!fileExists) {
|
|
|
|
//return exit(`File ${video} does not exist`, 6);
|
|
|
|
console.error(new Error(`File ${video} does not exist`));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ext === '.mkv') {
|
|
|
|
cmd = backup_cmd;
|
|
|
|
}
|
2019-08-15 19:25:33 +00:00
|
|
|
else if (ext === '.gif') {
|
|
|
|
cmd = gif_cmd;
|
|
|
|
}
|
2019-08-04 22:04:06 +00:00
|
|
|
try {
|
|
|
|
console.log(cmd);
|
|
|
|
raw = await exec_1.exec(cmd);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
frames = parseInt(raw.stdout);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return raw.stdout;
|
|
|
|
}
|
|
|
|
return frames;
|
2019-06-09 00:51:00 +00:00
|
|
|
}
|
2019-02-08 17:46:58 +00:00
|
|
|
}
|
2019-08-04 22:04:06 +00:00
|
|
|
/*
|
|
|
|
function map (obj : any) {
|
2019-06-09 00:51:00 +00:00
|
|
|
console.dir(obj);
|
2019-02-08 17:46:58 +00:00
|
|
|
}
|
2019-08-04 22:04:06 +00:00
|
|
|
*/
|
2019-02-08 17:46:58 +00:00
|
|
|
module.exports = (sys) => {
|
2019-08-04 22:04:06 +00:00
|
|
|
return new FFPROBE(sys);
|
2019-06-09 00:51:00 +00:00
|
|
|
};
|
|
|
|
//# sourceMappingURL=index.js.map
|