Limit Maximum Resolution in HLS.js

CK CK 3788 
Created at  
665 0 0

In some cases, you may need to limit the maximum video resolution in the video player (HLS.js). Fortunately, you can limit the maximum resolution in HLS.js by setting the maxMaxBufferLength and using the levelCap feature. There are a few ways to do this:

1. Using hls.currentLevel or hls.nextLevel

You can manually control the playback level by setting hls.currentLevel or hls.nextLevel to restrict resolution.

hls.on(Hls.Events.MANIFEST_PARSED, function() {
  // Limit to 720p (Example: Find the index for 720p)
  let maxResolutionIndex = hls.levels.findIndex(level => level.height <= 720);
  if (maxResolutionIndex !== -1) {
    hls.currentLevel = maxResolutionIndex;
  }
});

2. Using hls.levelCap

You can limit the highest level by setting hls.autoLevelCapping dynamically:

hls.on(Hls.Events.MANIFEST_PARSED, function() {
  hls.autoLevelCapping = hls.levels.findIndex(level => level.height <= 720);
});

3. Handling on Level Loading

You can filter levels dynamically as well:

hls.on(Hls.Events.LEVEL_LOADED, function(event, data) {
  if (hls.levels[data.level].height > 720) {
    hls.nextLevel = hls.levels.findIndex(level => level.height <= 720);
  }
});

4. Using maxBufferLength and maxMaxBufferLength

Though not directly limiting resolution, reducing the buffer size may encourage lower-quality selections.

hls.config.maxBufferLength = 10; // 10 seconds
hls.config.maxMaxBufferLength = 30; // Maximum 30 seconds

Notes:

  • Setting hls.autoLevelCapping will prevent the player from switching to higher resolutions.
  • Make sure that the correct resolution exists in the available levels (hls.levels).

Would you like to integrate this into a broader logic for adaptive streaming handling?

Tags 1080p 480p 720p HLS.js Limit Video Resolution Video Resolution hls.currentLevel hls.levelCap hls.nextLevel Facebook X
Comments 0
Similar posts
  1. Disabling Audio Tracks in HLS.js
    376
  2. HLS.js options to support subtitle
    516
  3. When HLS.js need to load m3u8 URL redirecting to the different URL (302 Redirection)
    664
  4. HLS.js code to reload when user face bufferfull error
    705
  5. HLS.js code to reload video when levelLoadTimeOut occurs
    707
  6. Change the manifest loading timeout in HLS.js
    1,039
  7. How do I display trickplay images with hls.js that elaborated at EXT-X-IMAGE-STREAM-INF in m3u8
    806
  1. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  2. From SDI to MXL: Inside the Cloud-Native Infrastructure Powering Next-Gen Live Broadcast
    570
  3. Smooth switching with EXT-X-DISCONTINUITY in HLS
    433
  4. A Deep Dive Into Internet Advertising Ecosystem on Video Streaming Industry
    647
  5. When to use #EXT-X-DISCONTINUITY in HLS?
    1,428
  6. A prefect Apache server settings to avoid CORS error for HLS
    605
  7. CloudFront Settings for Dynamic HLS Streaming
    651
  8. SafeFrame is a web standard developed by IAB
    510
  9. CSAI vs SSAI
    578
  10. Ad Marker: SCTE-35 Message Encoder based in Python
    800
  11. Ad marker: SCTE-35 enhanced
    575
  12. FFmpeg compile options based on configure
    1,112
  13. Script checking internet restriction with FFmpeg
    684
  14. FFmpeg compile option to support SCTE-35
    840
  15. HTTP Proxy when your URL access is blocked with FFmpeg
    1,902
  16. Magic line when HLS video manifest is mixed with encrypted / unencrypted video chunks
    1,463
  17. Recovering a crashed HLS.js player
    1,282
  18. How do I create animated background for Google Meet?
    21,019
  19. Secure HLS encoding using AES-128 encryption with FFmpeg
    1,138
  20. Machine Learning Models for Ad Detection from a Video Stream
    189
  21. Unlock Flawless HLS Audio Sync: The Power of #EXT-X-INDEPENDENT-SEGMENTS
    377
  22. NVIDIA CUDA library which can be integrated with C/C++
    376
  23. Approaches to Scene Detection in Video Processing
    646
  24. FFmpeg CLI to remove audio track from MP4
    543
  25. Building GPU as a Service aka Dockerized GPU
    1,579
  26. IAB Categories (a.k.a. OpenRTB Code)
    1,014
  27. FFmpeg library that can be directly integrated into C/C++/Rust/Python without CLI
    2,073
  28. HTTP Live Streaming (HLS) was developed by Apple as part of QuickTime X in iOS3.0, and became an official standard (RFC 8126) in 2017
    596
  29. Progressive MP4 is an application of streaming techniques using the standardized MP4 container invented by MPEG LA consortium
    1,078
  30. FFmpeg CLI to mix two different audios in a single audio file such as mp3
    604
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    251
  4. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  5. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  6. Array of arguments passed to script in PHP
    90
  7. Invalid Auction Storms in Ad Tech
    98
  8. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  9. The Two Clocks of Ad Server Architecture
    70
  10. Frequency Cap Architecture Overview
    515
  11. Collecting Proof-of-Play from 100,000 Screens
    146
  12. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  13. What is RedisVL?
    109
  14. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  15. TPU & GPU Cluster Management Tools
    245
  16. The Concept of Scaffolding in AI Agents
    234
  17. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  18. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  19. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  20. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  21. Machine Learning: Training vs. Ranking
    222
  22. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  23. Designing a Data Curation System Around a Vector Database
    221
  24. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  25. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  26. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  27. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  28. Understanding Data Lineage
    263
  29. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    167
  30. GPU & LLM Cluster Infrastructure & Task Scheduling Architecture
    245