Add types, return crop strategy with stretch factors

This commit is contained in:
Tamius Han 2021-03-30 21:54:08 +02:00
parent c07916362e
commit d1baebcceb
2 changed files with 34 additions and 5 deletions

View File

@ -63,7 +63,7 @@ class PlayerData {
//#region misc stuff //#region misc stuff
extensionMode: any; extensionMode: any;
dimensions: any; dimensions: {width?: number, height?: number, fullscreen?: boolean};
private playerIdElement: any; private playerIdElement: any;
private observer: ResizeObserver; private observer: ResizeObserver;
//#endregion //#endregion

View File

@ -5,6 +5,31 @@ import VideoData from '../video-data/VideoData';
import Logger from '../Logger'; import Logger from '../Logger';
export enum CropStrategy {
/**
* Nomenclature explained:
*
* SP - stream AR < player AR
* PS - the opposite of
*
* ArDominant - given aspect ratio is bigger than stream AR and player AR
* PSDominant - stream AR or player AR are bigger than given aspect ratio
*/
CropLetterbox = 1,
NoCropPillarbox = 2,
NoCropLetterbox = 3,
CropPillarbox = 4
}
export type VideoDimensions = {
xFactor?: number;
yFactor?: number;
cropStrategy?: number;
arCorrectionFactor?: number;
actualWidth?: number;
actualHeight?: number;
}
// računa velikost videa za približevanje/oddaljevanje // računa velikost videa za približevanje/oddaljevanje
// does video size calculations for zooming/cropping // does video size calculations for zooming/cropping
@ -72,7 +97,7 @@ class Scaler {
return null; return null;
} }
calculateCrop(ar) { calculateCrop(ar: {type: AspectRatioType, ratio?: number}) {
/** /**
* STEP 1: NORMALIZE ASPECT RATIO * STEP 1: NORMALIZE ASPECT RATIO
* *
@ -151,7 +176,7 @@ class Scaler {
this.logger.log('info', 'scaler', "[Scaler::calculateCrop] ar is " ,ar.ratio, ", file ar is", streamAr, ", this.conf.player.dimensions are ", this.conf.player.dimensions.width, "×", this.conf.player.dimensions.height, "| obj:", this.conf.player.dimensions); this.logger.log('info', 'scaler', "[Scaler::calculateCrop] ar is " ,ar.ratio, ", file ar is", streamAr, ", this.conf.player.dimensions are ", this.conf.player.dimensions.width, "×", this.conf.player.dimensions.height, "| obj:", this.conf.player.dimensions);
const videoDimensions = { const videoDimensions: VideoDimensions = {
xFactor: 1, xFactor: 1,
yFactor: 1, yFactor: 1,
actualWidth: 0, // width of the video (excluding pillarbox) when <video> tag height is equal to width actualWidth: 0, // width of the video (excluding pillarbox) when <video> tag height is equal to width
@ -172,18 +197,20 @@ class Scaler {
* @param {*} streamAr * @param {*} streamAr
* @param {*} playerAr * @param {*} playerAr
*/ */
calculateCropCore(videoDimensions, ar, streamAr, playerAr) { calculateCropCore(videoDimensions: VideoDimensions, ar: number, streamAr: number, playerAr: number) {
if (streamAr < playerAr) { if (streamAr < playerAr) {
if (streamAr < ar){ if (streamAr < ar){
// in this situation we have to crop letterbox on top/bottom of the player // in this situation we have to crop letterbox on top/bottom of the player
// we cut it, but never more than the player // we cut it, but never more than the player
videoDimensions.xFactor = Math.min(ar, playerAr) / streamAr; videoDimensions.xFactor = Math.min(ar, playerAr) / streamAr;
videoDimensions.yFactor = videoDimensions.xFactor; videoDimensions.yFactor = videoDimensions.xFactor;
videoDimensions.cropStrategy = CropStrategy.CropLetterbox;
} else { } else {
// in this situation, we would be cutting pillarbox. Inside horizontal player. // in this situation, we would be cutting pillarbox. Inside horizontal player.
// I don't think so. Except exceptions, we'll wait for bug reports. // I don't think so. Except exceptions, we'll wait for bug reports.
videoDimensions.xFactor = 1; videoDimensions.xFactor = 1;
videoDimensions.yFactor = 1; videoDimensions.yFactor = 1;
videoDimensions.cropStrategy = CropStrategy.NoCropPillarbox;
} }
} else { } else {
if (streamAr < ar || playerAr < ar){ if (streamAr < ar || playerAr < ar){
@ -191,10 +218,12 @@ class Scaler {
// this means we simply don't crop anything _at all_ // this means we simply don't crop anything _at all_
videoDimensions.xFactor = 1; videoDimensions.xFactor = 1;
videoDimensions.yFactor = 1; videoDimensions.yFactor = 1;
videoDimensions.cropStrategy = CropStrategy.NoCropLetterbox;
} else { } else {
// meant for handling pillarbox crop. not quite implemented. // meant for handling pillarbox crop. not quite implemented.
videoDimensions.xFactor = streamAr / Math.min(ar.ratio, playerAr); videoDimensions.xFactor = streamAr / Math.min(ar, playerAr);
videoDimensions.yFactor = videoDimensions.xFactor; videoDimensions.yFactor = videoDimensions.xFactor;
videoDimensions.cropStrategy = CropStrategy.CropPillarbox;
// videoDimensions.xFactor = Math.max(ar.ratio, playerAr) * fileAr; // videoDimensions.xFactor = Math.max(ar.ratio, playerAr) * fileAr;
// videoDimensions.yFactor = videoDimensions.xFactor; // videoDimensions.yFactor = videoDimensions.xFactor;
} }