diff options
| author | Zephyrrus <[email protected]> | 2020-07-02 23:42:44 +0300 |
|---|---|---|
| committer | Zephyrrus <[email protected]> | 2020-07-02 23:42:44 +0300 |
| commit | 22f9eb4dff9ee03b5ec655db2204050ffe7a7771 (patch) | |
| tree | 74a18e6887b90062d470b41c406d0ec146dc7360 /src/api/utils/videoPreview/FrameIntervalPreview.js | |
| parent | feat: refactor account to use vuex, fix small presentational components (diff) | |
| download | host.fuwn.me-22f9eb4dff9ee03b5ec655db2204050ffe7a7771.tar.xz host.fuwn.me-22f9eb4dff9ee03b5ec655db2204050ffe7a7771.zip | |
feat: refactor preview to support random fragment extraction
Diffstat (limited to 'src/api/utils/videoPreview/FrameIntervalPreview.js')
| -rw-r--r-- | src/api/utils/videoPreview/FrameIntervalPreview.js | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/api/utils/videoPreview/FrameIntervalPreview.js b/src/api/utils/videoPreview/FrameIntervalPreview.js new file mode 100644 index 0000000..75f6d2b --- /dev/null +++ b/src/api/utils/videoPreview/FrameIntervalPreview.js @@ -0,0 +1,72 @@ +const ffmpeg = require('fluent-ffmpeg'); +const probe = require('ffmpeg-probe'); + +const noop = () => {}; + +module.exports = async opts => { + const { + log = noop, + + // general output options + quality = 2, + width, + height, + input, + output, + + numFrames, + numFramesPercent = 0.05 + } = opts; + + const info = await probe(input); + // const numFramesTotal = parseInt(info.streams[0].nb_frames, 10); + const { avg_frame_rate: avgFrameRate, duration } = info.streams[0]; + const [frames, time] = avgFrameRate.split('/').map(e => parseInt(e, 10)); + + const numFramesTotal = (frames / time) * duration; + + let numFramesToCapture = numFrames || numFramesPercent * numFramesTotal; + numFramesToCapture = Math.max(1, Math.min(numFramesTotal, numFramesToCapture)) | 0; + const nthFrame = (numFramesTotal / numFramesToCapture) | 0; + + const result = { + output, + numFrames: numFramesToCapture + }; + + await new Promise((resolve, reject) => { + let scale = null; + + if (width && height) { + result.width = width | 0; + result.height = height | 0; + scale = `scale=${width}:${height}`; + } else if (width) { + result.width = width | 0; + result.height = ((info.height * width) / info.width) | 0; + scale = `scale=${width}:-1`; + } else if (height) { + result.height = height | 0; + result.width = ((info.width * height) / info.height) | 0; + scale = `scale=-1:${height}`; + } else { + result.width = info.width; + result.height = info.height; + } + + const filter = [`select=not(mod(n\\,${nthFrame}))`, scale].filter(Boolean).join(','); + + ffmpeg(input) + .outputOptions(['-vsync', 'vfr']) + .outputOptions(['-q:v', quality, '-vf', filter]) + .noAudio() + .outputFormat('webm') + .output(output) + .on('start', cmd => log && log({ cmd })) + .on('end', () => resolve()) + .on('error', err => reject(err)) + .run(); + }); + + return result; +}; |