Skip to content

Instantly share code, notes, and snippets.

@teebu
Forked from anonymous/index.html
Last active June 22, 2016 02:00
Show Gist options
  • Save teebu/871453ce43e4eae3569e28aac663b4f6 to your computer and use it in GitHub Desktop.
Save teebu/871453ce43e4eae3569e28aac663b4f6 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/cohomuyeji
function getOutputResolution(max_width, max_height, videoInfo) {
var tmp_max_width = 0
var tmp_max_height = 0
// ShrinkToFit: don't scale up videos
max_width = (max_width > videoInfo.width) ? videoInfo.width : max_width
max_height = (max_height > videoInfo.height) ? videoInfo.height : max_height
if (videoInfo.width < videoInfo.height) { // for portrait
tmp_max_width = Math.min(max_width, max_height) // getting the smallest number for width
tmp_max_height = Math.max(max_width, max_height) // getting the biggest number for height
} else { // for landscape
tmp_max_width = Math.max(max_width, max_height)
tmp_max_height = Math.min(max_width, max_height)
}
var scalingFactor = Math.min(
tmp_max_width / videoInfo.width,
tmp_max_height / videoInfo.height
);
//console.log(scalingFactor, scalingFactor * videoInfo.width, scalingFactor * videoInfo.width)
// fix for odd pixel error
var result_width = 2 * Math.round(Math.floor(scalingFactor * videoInfo.width) / 2)
var result_height = 2 * Math.round(Math.floor(scalingFactor * videoInfo.height) / 2)
// calculated the desired width and height of the video
return ({width: result_width, height: result_height})
}
var video = {width: 568, height: 320};
console.log( getOutputResolution(376, 376, video) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment