Search code examples
javascriptnode.jsmathresolutionnode-webkit

How to determine display format


By display format, I mean names like 360p, 720p, 1080, 2K, 4K, 8K.

If I have a bunch of videos, how do I determine which format is each? Their resolutions vary, some are 1280x720, which is 720p, but others are 1270x528, which should still be 720p? Same with 960x720.


Solution

  • I made a small array with some common resolutions. The algorithm looks first for the lines and then for the dots.

    var resolution = [
            { name: '480p', dots: 852, lines: 480 },
            { name: '576p', dots: 768, lines: 576 },
            { name: '720p', dots: 1280, lines: 720 },
            { name: '1080p', dots: 1920, lines: 1080 },
            { name: '2160p', dots: 3840, lines: 2160 },
            { name: '4320p', dots: 7680, lines: 4320 },
        ];
    
    function findResolution(dots, lines) {
        var i = 0;
        while (lines > resolution[i].lines) {
            i++;
        }
        while (dots > resolution[i].dots) {
            i++;
        }
        return resolution[i].name;
    }
    document.write(findResolution(600, 600) + '<br>');
    document.write(findResolution(1920, 600) + '<br>');        
    document.write(findResolution(1270, 528) + '<br>');
    document.write(findResolution(960, 720) + '<br>');