How do I display trickplay images with hls.js that elaborated at EXT-X-IMAGE-STREAM-INF in m3u8

CK CK 3788 
Created at  
801 0 0

Displaying trickplay images using hls.js involves integrating the trickplay image data specified by the EXT-X-IMAGE-STREAM-INF tag in the M3U8 manifest. Trickplay images are typically used to provide previews or thumbnails of video content, often displayed when a user hovers over the seek bar.

Here's a step-by-step guide to achieve this:

  1. Setup HLS.js: Ensure you have hls.js integrated into your project. You can include it via CDN or install it via npm/yarn.

    <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
    

    Or install via npm/yarn:

    npm install hls.js
    
  2. Initialize HLS.js: Initialize hls.js and attach it to your video element.

    <video id="video" controls></video>
    <div id="thumbnail-container">
        <img id="thumbnail" style="display: none;" />
    </div>
    
    if (Hls.isSupported()) {
      const video = document.getElementById('video');
      const hls = new Hls();
      hls.loadSource('path/to/your/playlist.m3u8');
      hls.attachMedia(video);
      hls.on(Hls.Events.MANIFEST_PARSED, function () {
        video.play();
      });
    }
    
  3. Parse Trickplay Image Data: Parse the EXT-X-IMAGE-STREAM-INF tag from the M3U8 manifest. This tag will provide information about the trickplay images, including the URL, resolution, and interval.

    hls.on(Hls.Events.LEVEL_LOADED, function (event, data) {
      const imageStream = data.details.fragments.find(fragment => fragment.tag === 'EXT-X-IMAGE-STREAM-INF');
      if (imageStream) {
        const imageUrlTemplate = imageStream.url;
        const imageInterval = imageStream.attrs['RESOLUTION'];
        const width = imageStream.attrs['RESOLUTION'].split('x')[0];
        const height = imageStream.attrs['RESOLUTION'].split('x')[1];
    
        setupThumbnails(imageUrlTemplate, imageInterval, width, height);
      }
    });
    
  4. Setup Thumbnail Display: Create a function to display the thumbnails when the user hovers over the seek bar.

    function setupThumbnails(imageUrlTemplate, imageInterval, width, height) {
      const video = document.getElementById('video');
      const thumbnail = document.getElementById('thumbnail');
      const thumbnailContainer = document.getElementById('thumbnail-container');
    
      video.addEventListener('mousemove', function (event) {
        const duration = video.duration;
        const videoWidth = video.clientWidth;
        const mousePosition = event.offsetX;
        const hoverTime = (mousePosition / videoWidth) * duration;
    
        const thumbnailIndex = Math.floor(hoverTime / imageInterval);
        const thumbnailUrl = imageUrlTemplate.replace('{$index}', thumbnailIndex);
    
        thumbnail.src = thumbnailUrl;
        thumbnail.style.display = 'block';
        thumbnailContainer.style.left = `${event.pageX}px`;
        thumbnailContainer.style.top = `${event.pageY - height - 10}px`;
      });
    
      video.addEventListener('mouseleave', function () {
        thumbnail.style.display = 'none';
      });
    }
    
  5. Update Styles: Add some basic styles to position the thumbnail container.

    <style>
      #thumbnail-container {
        position: absolute;
        pointer-events: none;
      }
      #thumbnail {
        width: auto;
        height: auto;
        max-width: 100px;
      }
    </style>
    
  6. M3U8 Manifest Example: Ensure your M3U8 manifest includes the EXT-X-IMAGE-STREAM-INF tag correctly.

    #EXTM3U
    #EXT-X-VERSION:7
    #EXT-X-IMAGE-STREAM-INF:RESOLUTION=320x180,CODECS="jpeg",URI="thumbnails_180p.m3u8"
    #EXT-X-IMAGE-STREAM-INF:RESOLUTION=640x360,CODECS="jpeg",URI="thumbnails_360p.m3u8"
    

    The thumbnails_180p.m3u8 might look like this:

    #EXTM3U
    #EXT-X-VERSION:7
    #EXT-X-TARGETDURATION:10
    #EXTINF:10.000,
    thumbnail_00001.jpg
    #EXTINF:10.000,
    thumbnail_00002.jpg
    #EXTINF:10.000,
    thumbnail_00003.jpg
    

This is a basic implementation to get you started with displaying trickplay images using hls.js. You can customize and optimize this code according to your requirements.

Tags EXT-X-IMAGE-STREAM-INF hls.js Facebook X
Comments 0
Similar posts
  1. Change the manifest loading timeout in HLS.js
    1,028
  2. HLS.js code to reload video when levelLoadTimeOut occurs
    703
  3. HLS.js code to reload when user face bufferfull error
    703
  4. When HLS.js need to load m3u8 URL redirecting to the different URL (302 Redirection)
    659
  5. HLS.js options to support subtitle
    513
  6. Limit Maximum Resolution in HLS.js
    663
  7. Disabling Audio Tracks in HLS.js
    371
  1. Building GPU as a Service aka Dockerized GPU
    1,566
  2. IAB Categories (a.k.a. OpenRTB Code)
    1,012
  3. FFmpeg library that can be directly integrated into C/C++/Rust/Python without CLI
    2,057
  4. 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
    593
  5. Progressive MP4 is an application of streaming techniques using the standardized MP4 container invented by MPEG LA consortium
    1,071
  6. FFmpeg CLI to mix two different audios in a single audio file such as mp3
    599
  7. FFmpeg CLI to merge a video only mp4 and mp3 audio file
    332
  8. FFmpeg CLI to convert a video file to an MP3 audio file
    345
  9. HLS.js options to support subtitle
    513
  10. When HLS.js need to load m3u8 URL redirecting to the different URL (302 Redirection)
    659
  11. HLS.js code to reload when user face bufferfull error
    703
  12. HLS.js code to reload video when levelLoadTimeOut occurs
    703
  13. Change the manifest loading timeout in HLS.js
    1,028
  14. HLS.js Custom Error Handler to load Main Manifest from unstable HLS Live Stream
    1,427
  15. Mastering Audio Sync: Keyframes, GOP, FFmpeg, and HLS Explained
    536
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,730
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    86
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    234
  17. The Concept of Scaffolding in AI Agents
    222
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    328
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252