// Returns the highest quality <= targetHeight, optionally allows target fps function sort(formats, targetHeight, targetFps) { targetFps = targetFps ?? Number(targetHeight.split("p")[1] || 30) if (typeof(targetHeight) == "string") targetHeight = Number(targetHeight.split("p")[0]) targetHeight += 12 return formats.sort((a, b) => { let aHeight = Number(a.split("p")[0]) let aFps = Number(a.split("p")[1] || 30) let aValue = aHeight > targetHeight ? 9999 : (targetHeight - aHeight) aValue -= aFps > targetFps ? 1 : (targetFps - aFps) let bHeight = Number(b.split("p")[0]) let bFps = Number(b.split("p")[1] || 30) let bValue = bHeight > targetHeight ? 9999 : (targetHeight - bHeight) bValue -= bFps > targetFps ? 1 : (targetFps - bFps) return aValue - bValue }) } function sortFormats(formats, targetHeight, targetFps) { let qualityRefs = {} const justQualities = formats.map(f => { qualityRefs[f.qualityLabel] = f return f.qualityLabel }) const result = sort(justQualities, targetHeight, targetFps) return result.map(q => qualityRefs[q]) } module.exports = { sort, sortFormats }