|
|
|
@ -4,15 +4,38 @@ const assert = require('assert'); |
|
|
|
const { getFfmpegCommonArgs } = require('../ffmpeg'); |
|
|
|
const { readFileStreams } = require('../util'); |
|
|
|
|
|
|
|
module.exports = async ({ width, height, channels, framerateStr, verbose, ffmpegPath, ffprobePath, enableFfmpegLog, params }) => { |
|
|
|
module.exports = async ({ statelessMode, width, height, channels, framerateStr, verbose, ffmpegPath, ffprobePath, enableFfmpegLog, params }) => { |
|
|
|
const targetSize = width * height * channels; |
|
|
|
|
|
|
|
const buf = Buffer.allocUnsafe(targetSize); |
|
|
|
|
|
|
|
// TODO assert that we have read the correct amount of frames
|
|
|
|
|
|
|
|
const { path, cutFrom, cutTo, resizeMode = 'cover', backgroundColor = '#000000', framePtsFactor } = params; |
|
|
|
const { path, cutFrom, cutTo, resizeMode = 'cover', backgroundColor = '#000000', framePtsFactor, inputDuration } = params; |
|
|
|
|
|
|
|
let initializing; |
|
|
|
|
|
|
|
let stream; |
|
|
|
let ps; |
|
|
|
let length; |
|
|
|
|
|
|
|
let timeout; |
|
|
|
let ended; |
|
|
|
|
|
|
|
async function restartProcess(cutFromWithProgress) { |
|
|
|
console.log('(Re)start process', { cutFromWithProgress }); |
|
|
|
|
|
|
|
if (initializing) throw new Error('Already initializing video'); |
|
|
|
initializing = true; |
|
|
|
|
|
|
|
stream = undefined; |
|
|
|
if (ps) ps.cancel(); |
|
|
|
ps = undefined; |
|
|
|
length = 0; |
|
|
|
clearTimeout(timeout); |
|
|
|
timeout = undefined; |
|
|
|
ended = false; |
|
|
|
|
|
|
|
const buf = Buffer.allocUnsafe(targetSize); |
|
|
|
let length = 0; |
|
|
|
// let inFrameCount = 0;
|
|
|
|
|
|
|
|
let ptsFilter = ''; |
|
|
|
@ -40,9 +63,11 @@ module.exports = async ({ width, height, channels, framerateStr, verbose, ffmpeg |
|
|
|
const args = [ |
|
|
|
...getFfmpegCommonArgs({ enableFfmpegLog }), |
|
|
|
...inputCodecArgs, |
|
|
|
...(cutFrom ? ['-ss', cutFrom] : []), |
|
|
|
...(cutFromWithProgress ? ['-ss', cutFromWithProgress] : []), |
|
|
|
'-i', path, |
|
|
|
...(cutTo ? ['-t', (cutTo - cutFrom) * framePtsFactor] : []), |
|
|
|
// TODO -t maybe not needed as we will close the stream when we don't need more?
|
|
|
|
// ...(cutTo ? ['-t', (cutTo - cutFrom) * framePtsFactor] : []),
|
|
|
|
...(statelessMode ? ['-vframes', '1'] : []), |
|
|
|
'-vf', `${ptsFilter}fps=${framerateStr},${scaleFilter}`, |
|
|
|
'-map', 'v:0', |
|
|
|
'-vcodec', 'rawvideo', |
|
|
|
@ -52,12 +77,9 @@ module.exports = async ({ width, height, channels, framerateStr, verbose, ffmpeg |
|
|
|
]; |
|
|
|
if (verbose) console.log(args.join(' ')); |
|
|
|
|
|
|
|
const ps = execa(ffmpegPath, args, { encoding: null, buffer: false, stdin: 'ignore', stdout: 'pipe', stderr: process.stderr }); |
|
|
|
ps = execa(ffmpegPath, args, { encoding: null, buffer: false, stdin: 'ignore', stdout: 'pipe', stderr: process.stderr }); |
|
|
|
|
|
|
|
const stream = ps.stdout; |
|
|
|
|
|
|
|
let timeout; |
|
|
|
let ended = false; |
|
|
|
stream = ps.stdout; |
|
|
|
|
|
|
|
stream.once('end', () => { |
|
|
|
clearTimeout(timeout); |
|
|
|
@ -65,7 +87,19 @@ module.exports = async ({ width, height, channels, framerateStr, verbose, ffmpeg |
|
|
|
ended = true; |
|
|
|
}); |
|
|
|
|
|
|
|
const readNextFrame = () => new Promise((resolve, reject) => { |
|
|
|
initializing = false; // TODO try finally
|
|
|
|
} |
|
|
|
|
|
|
|
// await restartProcess();
|
|
|
|
|
|
|
|
const renderFrame = async (progress) => { |
|
|
|
// console.log(progress)
|
|
|
|
if (statelessMode || progress === 0) { |
|
|
|
const cutFromWithProgress = (cutFrom || 0) + (progress * inputDuration); |
|
|
|
await restartProcess(cutFromWithProgress); |
|
|
|
} |
|
|
|
|
|
|
|
const data = await new Promise((resolve, reject) => { |
|
|
|
if (ended) { |
|
|
|
console.log(path, 'Tried to read next video frame after ffmpeg video stream ended'); |
|
|
|
resolve(); |
|
|
|
@ -124,18 +158,19 @@ module.exports = async ({ width, height, channels, framerateStr, verbose, ffmpeg |
|
|
|
stream.on('end', onEnd); |
|
|
|
stream.on('error', reject); |
|
|
|
stream.resume(); |
|
|
|
}).then((data) => { |
|
|
|
if (data) assert(data.length === targetSize); |
|
|
|
return data; |
|
|
|
}); |
|
|
|
|
|
|
|
if (data) assert.equal(data.length, targetSize); |
|
|
|
return data; |
|
|
|
}; |
|
|
|
|
|
|
|
const close = () => { |
|
|
|
if (verbose) console.log('Close', path); |
|
|
|
ps.cancel(); |
|
|
|
}; |
|
|
|
|
|
|
|
return { |
|
|
|
readNextFrame, |
|
|
|
renderFrame, |
|
|
|
close, |
|
|
|
}; |
|
|
|
}; |