From c36bcab7ead2a3649c68b9f18770d0cebb287b84 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Tue, 27 Oct 2020 17:09:56 +0100 Subject: [PATCH] keep aspect ratio with --fast #13 --- README.md | 3 +++ index.js | 38 ++++++++++++++++++++++++++------------ util.js | 3 ++- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4fce97c7..04c7b2a9 100644 --- a/README.md +++ b/README.md @@ -365,6 +365,9 @@ This project is maintained by me alone. The project will always remain free and - https://github.com/sjfricke/awesome-webgl - https://www.mltframework.org/docs/melt/ +## Videos made by you + +Submit a PR if you want to share your videos or project created with editly here. --- diff --git a/index.js b/index.js index 18768abb..42b306a9 100644 --- a/index.js +++ b/index.js @@ -70,14 +70,14 @@ const Editly = async (config = {}) => { if (verbose) console.log(JSON5.stringify(clips, null, 2)); // Try to detect parameters from first video - let detectedWidth; - let detectedHeight; + let firstVideoWidth; + let firstVideoHeight; let firstVideoFramerateStr; clips.find((clip) => clip && clip.layers.find((layer) => { if (layer.type === 'video') { - detectedWidth = layer.inputWidth; - detectedHeight = layer.inputHeight; + firstVideoWidth = layer.inputWidth; + firstVideoHeight = layer.inputHeight; firstVideoFramerateStr = layer.framerateStr; return true; @@ -90,18 +90,19 @@ const Editly = async (config = {}) => { let desiredWidth; - if (fast) desiredWidth = 320; - else if (requestedWidth) desiredWidth = requestedWidth; + if (requestedWidth) desiredWidth = requestedWidth; else if (isGif) desiredWidth = 320; - if (detectedWidth && detectedHeight) { + const roundDimension = (val) => (isGif ? Math.round(val) : multipleOf2(val)); + + if (firstVideoWidth && firstVideoHeight) { if (desiredWidth) { - const calculatedHeight = Math.round((detectedHeight / detectedWidth) * desiredWidth); - height = isGif ? calculatedHeight : multipleOf2(calculatedHeight); // x264 requires multiple of 2 + const calculatedHeight = (firstVideoHeight / firstVideoWidth) * desiredWidth; + height = roundDimension(calculatedHeight); width = desiredWidth; } else { - width = detectedWidth; - height = detectedHeight; + width = firstVideoWidth; + height = firstVideoHeight; } } else if (desiredWidth) { width = desiredWidth; @@ -114,14 +115,27 @@ const Editly = async (config = {}) => { } // User override? - if (!fast && requestedWidth && requestedHeight) { + if (requestedWidth && requestedHeight) { width = requestedWidth; height = requestedHeight; } + if (fast) { + const numPixelsEachDirection = 250; + const aspectRatio = width / height; + width = roundDimension(numPixelsEachDirection * Math.sqrt(aspectRatio)); + height = roundDimension(numPixelsEachDirection * Math.sqrt(1 / aspectRatio)); + } + assert(width, 'Width not specified or detected'); assert(height, 'Height not specified or detected'); + if (!isGif) { + // x264 requires multiple of 2, eg minimum 2 + width = Math.max(2, width); + height = Math.max(2, height); + } + let fps; let framerateStr; diff --git a/util.js b/util.js index 3e84445f..6614bf1a 100644 --- a/util.js +++ b/util.js @@ -62,7 +62,8 @@ function toArrayInteger(buffer) { return []; } -const multipleOf2 = (x) => (x + (x % 2)); +// x264 requires multiple of 2 +const multipleOf2 = (x) => Math.round(x / 2) * 2; function getPositionProps({ position, width, height }) { let originY = 'center';