Skip to content

Commit

Permalink
Merge branch 'master' into core
Browse files Browse the repository at this point in the history
Conflicts:
	cli.js
	package-lock.json
	package.json
  • Loading branch information
tungs committed Apr 3, 2022
2 parents 6b52ce7 + d78c331 commit f42765d
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ const timesnap = require('timesnap-core');
const path = require('path');
const fs = require('fs');
const spawn = require('child_process').spawn;
const cpus = require('os').cpus().length;
const defaultFPS = 60;
const defaultThreads = 1;
const defaultDuration = 5;

const makeFileDirectoryIfNeeded = function (filepath) {
var dir = path.parse(filepath).dir, ind, currDir;
Expand Down Expand Up @@ -76,6 +79,7 @@ module.exports = async function (config) {
var outputOptions = config.outputOptions || [];
var frameDirectory = config.tempDir || config.frameDir;
var fps;
var threads;
var frameMode = config.frameCache || !config.pipeMode;
var pipeMode = config.pipeMode;
var processError;
Expand Down Expand Up @@ -109,6 +113,11 @@ module.exports = async function (config) {
fps = defaultFPS;
}

threads = config.threads || defaultThreads;
if (threads > cpus) {
threads = cpus;
}

const log = function () {
if (!config.quiet) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -197,7 +206,28 @@ module.exports = async function (config) {

var overallError;
try {
await timesnap(timesnapConfig);
if (threads === 1) {
await timesnap(timesnapConfig);
} else {
var progress = [];
var framesLeft = config.frames || config.duration * fps || defaultDuration * fps;
var startFrame = 0;
while (threads >= 1) {
let frameLength = Math.floor(framesLeft / threads--);
let frameStart = startFrame;
let frameEnd = frameStart + frameLength;
let threadConfig = Object.assign({} , timesnapConfig, {
shouldSkipFrame({ frameCount }) {
// frameCount is 1 based
return frameCount <= frameStart || frameCount > frameEnd;
}
});
progress.push(timesnap(threadConfig));
startFrame = frameEnd;
framesLeft -= frameLength;
}
await Promise.all(progress);
}
if (convertProcess) {
convertProcess.stdin.end();
}
Expand Down

0 comments on commit f42765d

Please sign in to comment.