Your logic for determining if 4k or 1080p is incorrect.

3 months ago (edited)
Bugs
Completed
These are all 4k:
3840 x 2080 (1440p/QHD)
3832 x 2076 (1440p/QHD)
3840 x 1608 (1440p/QHD)
3840 x 1600 (1440p/QHD)
2876 x 2156

These are all 1080p
1920 x 1040 (720p/HD)
1920 x 960 (720p/HD)
1920 x 872 (720p/HD)
1920 x 804 (720p/HD)
1440 x 1080

16:9 calcs
If the frame is 3840 wide it is 4k
stop processing
If the frame is 1920 wide is 1080p
stop processing

4:3 calcs
if the frame is 2160 high it is 4k
stop processing
if the frame is 1080 high is 1080p
stop processing

4:3 4k
2880 × 2160 is also 4k

4:3 1080p
1440 × 1080

Here is some fuzzy logic:

#!/usr/bin/env python3
import argparse

def classify_resolution(width, height):
    """
    Classify video resolution as 4K, 1080p, 720p, or 480p.

    Args:
        width: Video width in pixels
        height: Video height in pixels

    Returns:
        str: Resolution classification
    """

    # 16:9 calculations (width-based)
    if width == 3840:
        return "4K"

    if width == 1920:
        return "1080p"

    if width == 1280:
        return "720p"

    if width == 854 or width == 848 or width == 720:
        return "480p"

    # 4:3 calculations (height-based)
    if height == 2160:
        return "4K"

    if height == 1080:
        return "1080p"

    if height == 720:
        return "720p"

    if height == 480:
        return "480p"

    # Fallback: classify by approximate pixel count
    total_pixels = width * height

    if total_pixels >= 8_000_000:  # ~4K threshold (3840x2160 = 8,294,400)
        return "4K"
    elif total_pixels >= 1_400_000:  # ~1080p threshold (midpoint between 720p and 1080p)
        return "1080p"
    elif total_pixels >= 600_000:  # ~720p threshold (midpoint between 480p and 720p)
        return "720p"
    elif total_pixels >= 300_000:  # ~480p threshold
        return "480p"
    else:
        return "Unknown (below 480p)"

def main():
    parser = argparse.ArgumentParser(
        description='Classify video resolution',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument('-w', '--width', type=int, required=True,
                        help='Video width in pixels')
    parser.add_argument('-t', '--height', type=int, required=True,
                        help='Video height in pixels')

    args = parser.parse_args()

    resolution = classify_resolution(args.width, args.height)
    print(f"{args.width} x {args.height} is classified as: {resolution}")

if __name__ == "__main__":
    main()

Discussion

Comments

0