Skip to content

Commit

Permalink
Save files after process to keep only one slow motion file
Browse files Browse the repository at this point in the history
This behavior is supposed to allow you to add a single source for slomo
replays in OBS with a single shortcut to avoid switching between obs and
your file system to drag n drop files
  • Loading branch information
Nico ESPIAU committed Jan 5, 2019
1 parent 76d86f1 commit 9cd461c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 50 deletions.
10 changes: 6 additions & 4 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use extract';

module.exports = {
dirToWatch: "vids",
destDir: "rendered",
filePattern: /^replay.*/,
frFactor: process.env.FR_FACTOR || 0.5
dirToWatch: process.env.DIR_TO_WATCH || "vids/input",
destDir: process.env.DEST_DIR || "vids/rendered",
archiveDir: process.env.ARCHIVE_DIR || "vids/archives",
filePattern: /^replay.*/,
frFactor: process.env.FR_FACTOR || 0.5,
secondsToKeep: process.env.SECONDS_TO_KEEP || 4
};
5 changes: 2 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ services:
restart: always
volumes:
- ./:/var/www
- /Users/BonjourJohn/Movies/OBS/replays/:/var/www/vids/
- /Users/BonjourJohn/Movies/OBS/slomo/:/var/www/rendered/

- /Users/BonjourJohn/Movies/OBS/:/var/www/vids/
command: sh
87 changes: 44 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
'use strict';

const FS = require('fs');
const Path = require('path');
const {spawn} = require('child_process');
const FS = require('fs'),
Path = require('path'),
spawnSync = require('child_process').spawnSync,
Events = require('events');

const EventHandler = new Events();
const config = require('./config/config');

FS.watch(config.dirToWatch, (eventType, filename) => {
if (eventType === "rename" && config.filePattern.test(filename)) {
//get framerate
const videoInfoProcess = spawn("exiftool", [Path.join(config.dirToWatch, filename)]);
let videoInfo;
let frameRate;
let err;

videoInfoProcess.stdout.on('data', data => {
videoInfo += data;
});

videoInfoProcess.stderr.on('data', data => {
err += data;
});

videoInfoProcess.on('close', () => {
frameRate = (/Video Frame Rate *: *([\d\.]+)/gi.exec(videoInfo))[1];
try {
convert(filename, parseInt(frameRate));
} catch (e) {
process.stdout.write("ERR:", e);
}
});
}
console.log(eventType, filename);

if (eventType === "rename" && config.filePattern.test(filename) && FS.existsSync(Path.join(config.dirToWatch, filename))) {
console.time("fullprocess");
console.time("cut");
const cutFilename = cut(filename);
console.timeEnd("cut");
console.time("framerate");
const framerate = getFramerate(cutFilename);
console.timeEnd("framerate");
console.time("convert");
const slowmoFilename = convert(cutFilename, framerate);
console.timeEnd("convert");
console.time("cleanFiles");
const ts = new Date().toISOString().replace(/[\D]/g, '');
//move original file to archive
FS.renameSync(Path.join(config.dirToWatch, filename), Path.join(config.archiveDir, filename.replace(/(.+)\.([\d\w]+)/, '$1_'+ts+'.$2')));
//move cut file to archive
FS.renameSync(Path.join(config.destDir, cutFilename), Path.join(config.archiveDir, cutFilename.replace(/(.+)\.([\d\w]+)/, '$1_'+ts+'.$2')));
//copy slow mo file
const tsSlomoFilename = slowmoFilename.replace(/(.+)\.([\d\w]+)/, '$1_'+ts+'.$2');
FS.copyFileSync(Path.join(config.destDir, slowmoFilename), Path.join(config.archiveDir, tsSlomoFilename));
console.timeEnd("cleanFiles");
console.timeEnd("fullprocess");
}
});

function convert(filename, frameRate) {
console.log(Path.join(config.dirToWatch, filename));
const videoConverting = spawn("ffmpeg", ["-i", Path.join(config.dirToWatch, filename), "-vf", `setpts=${1/config.frFactor}*PTS`, "-r", (1/config.frFactor)*frameRate, filename.replace(/(.+)\.([\d\w]+)/, Path.join(config.destDir, '$1_slomo.$2'))]);

let conversionInfo;
let err;

videoConverting.stdout.on('data', data => {
conversionInfo += data;
});
function getFramerate(filename) {
const spawnRes = spawnSync("exiftool", [Path.join(config.destDir, filename)]);
const videoInfo = spawnRes.stdout;
return (/Video Frame Rate *: *([\d\.]+)/gi.exec(videoInfo))[1];
}

videoConverting.stderr.on('data', data => {
err += data;
});
function convert(filename, frameRate) {
const slowmoFilename = filename.replace(/(.+)\.([\d\w]+)/, '$1_slomo.$2');
spawnSync("ffmpeg", ["-i", Path.join(config.destDir, filename), "-vf", `setpts=${1 / config.frFactor}*PTS`, "-r", (1 / config.frFactor) * frameRate, Path.join(config.destDir, slowmoFilename), "-y"]);
return slowmoFilename;
}

videoConverting.on('close', () => {
console.log(`${filename} converted`);
});
function cut(input) {
const output = input.replace(/(.+)\.([\d\w]+)/, 'replay_cut.$2');
spawnSync("ffmpeg", ["-sseof", "-"+config.secondsToKeep, "-y", "-i", Path.join(config.dirToWatch, input), "-an", Path.join(config.destDir, output)]);
return output;
}

0 comments on commit 9cd461c

Please sign in to comment.