From bded8b9e5a99f86c33a4b363c7b4016adb5575ea Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Thu, 24 Sep 2020 16:22:54 +0200 Subject: [PATCH] fix broken renderSingleFrame logic --- examples/renderSingleFrame.js | 2 +- index.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/renderSingleFrame.js b/examples/renderSingleFrame.js index 2e531b4..ba92e0e 100644 --- a/examples/renderSingleFrame.js +++ b/examples/renderSingleFrame.js @@ -8,4 +8,4 @@ const { renderSingleFrame } = require('..'); time: 0, clips: JSON5.parse(await fs.readFile('./videos.json5', 'utf-8')).clips, }); -})().catch(console.error); \ No newline at end of file +})().catch(console.error); diff --git a/index.js b/index.js index 3edc69c..5d188fe 100644 --- a/index.js +++ b/index.js @@ -385,16 +385,16 @@ async function renderSingleFrame({ outPath = `${Math.floor(Math.random() * 1e12)}.png`, }) { const clips = await parseConfig({ defaults, clips: clipsIn, allowRemoteRequests, ffprobePath }); - let totalDuration = 0; + let clipStartTime = 0; const clip = clips.find((c) => { - if (totalDuration >= time) return true; - totalDuration += c.duration; + if (clipStartTime <= time && clipStartTime + c.duration > time) return true; + clipStartTime += c.duration; return false; }); assert(clip, 'No clip found at requested time'); const clipIndex = clips.indexOf(clip); const frameSource = await createFrameSource({ clip, clipIndex, width, height, channels, verbose, logTimes, ffmpegPath, ffprobePath, enableFfmpegLog, framerateStr: '1' }); - const rgba = await frameSource.readNextFrame({ time }); + const rgba = await frameSource.readNextFrame({ time: time - clipStartTime }); // TODO converting rgba to png can be done more easily? const canvas = createFabricCanvas({ width, height });