-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save files after process to keep only one slow motion file
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
Showing
3 changed files
with
52 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |