Skip to content

Commit

Permalink
keep aspect ratio with --fast #13
Browse files Browse the repository at this point in the history
  • Loading branch information
mifi committed Oct 27, 2020
1 parent 2e10aca commit c36bcab
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---

Expand Down
38 changes: 26 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit c36bcab

Please sign in to comment.