2019-02-08 17:46:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
|
|
|
const exec = require('exec');
|
|
|
|
//const spawn = require('spawn');
|
2019-02-08 23:21:16 +00:00
|
|
|
//const exit = require('exit');
|
2019-02-08 17:46:58 +00:00
|
|
|
|
|
|
|
let system = {};
|
|
|
|
|
|
|
|
async function info (video) {
|
|
|
|
let cmd = `ffprobe -v quiet -print_format json -show_format -show_streams "${video}"`
|
|
|
|
let exists;
|
|
|
|
let raw;
|
|
|
|
let json;
|
|
|
|
let vid;
|
|
|
|
|
|
|
|
try {
|
|
|
|
exists = await fs.exists(video);
|
|
|
|
} catch (err) {
|
|
|
|
return exit(err, 5);
|
|
|
|
}
|
|
|
|
if (!exists) {
|
2019-02-08 23:21:16 +00:00
|
|
|
//return exit(`File ${video} does not exist`, 6);
|
|
|
|
console.error(err);
|
|
|
|
return false
|
2019-02-08 17:46:58 +00:00
|
|
|
}
|
2019-02-08 23:21:16 +00:00
|
|
|
|
2019-02-08 17:46:58 +00:00
|
|
|
try {
|
2019-02-08 23:21:16 +00:00
|
|
|
console.log(cmd);
|
2019-02-08 17:46:58 +00:00
|
|
|
raw = await exec(cmd);
|
|
|
|
} catch (err) {
|
2019-02-08 23:21:16 +00:00
|
|
|
//return exit(err, 7);
|
|
|
|
console.error(err);
|
|
|
|
return false
|
2019-02-08 17:46:58 +00:00
|
|
|
}
|
2019-02-08 23:21:16 +00:00
|
|
|
|
2019-02-08 17:46:58 +00:00
|
|
|
try {
|
2019-02-08 23:21:16 +00:00
|
|
|
json = JSON.parse(raw.stdout);
|
2019-02-08 17:46:58 +00:00
|
|
|
} catch (err) {
|
2019-02-08 23:21:16 +00:00
|
|
|
return raw.stdout;
|
2019-02-08 17:46:58 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function frames (video) {
|
|
|
|
let cmd = `ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 "${video}"`;
|
|
|
|
let exists;
|
|
|
|
let raw;
|
|
|
|
let frames;
|
|
|
|
|
|
|
|
try {
|
|
|
|
exists = await fs.exists(video);
|
|
|
|
} catch (err) {
|
2019-02-08 23:21:16 +00:00
|
|
|
//return exit(err, 5);
|
|
|
|
console.error(err);
|
|
|
|
return false
|
2019-02-08 17:46:58 +00:00
|
|
|
}
|
|
|
|
if (!exists) {
|
2019-02-08 23:21:16 +00:00
|
|
|
//return exit(`File ${video} does not exist`, 6);
|
|
|
|
console.error(err);
|
|
|
|
return false;
|
2019-02-08 17:46:58 +00:00
|
|
|
}
|
2019-02-08 23:21:16 +00:00
|
|
|
|
2019-02-08 17:46:58 +00:00
|
|
|
try {
|
2019-02-08 23:21:16 +00:00
|
|
|
console.log(cmd);
|
2019-02-08 17:46:58 +00:00
|
|
|
raw = await exec(cmd);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2019-02-08 23:21:16 +00:00
|
|
|
return false;
|
2019-02-08 17:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-02-08 23:21:16 +00:00
|
|
|
frames = parseInt(raw.stdout)
|
2019-02-08 17:46:58 +00:00
|
|
|
} catch (err) {
|
2019-02-08 23:21:16 +00:00
|
|
|
return raw.stdout;
|
2019-02-08 17:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
function map (obj) {
|
|
|
|
console.dir(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = (sys) => {
|
|
|
|
system = sys;
|
|
|
|
return {
|
|
|
|
info,
|
|
|
|
frames
|
|
|
|
}
|
|
|
|
}
|