h264 MSE Single Frame

by dustinkerstein

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/92/three.min.js"></script>
<progress id="progress" value="0"></progress>
<video id="video" autoplay muted width="300"></video>

JavaScript

const video = document.getElementById("video");
const progressBar = document.getElementById("progress");

var sourceBuffer, frameRate, arrayOfBytes, chunk, file, index = 0, videoInitialized = false,videoSeeked = false, isUserInteracting = false, segments = [], ms = new MediaSource();

video.src = window.URL.createObjectURL(ms);
ms.addEventListener('sourceopen', onMediaSourceOpen);

getData('hd_dash.mpd');

function videoInit(bytes) {
  chunk = bytes.slice(0, parseInt(segments[0].getAttribute("mediaRange").toString().split('-')[1]) + 1);
  console.log("pausing video, appending buffer, and waiting..");
	video.pause(); // same behavior if autoplay is disabled
  appendToBuffer(chunk);
}

function onMediaSourceOpen() {
  sourceBuffer = ms.addSourceBuffer('video/mp4; codecs="avc1.640033"');
  sourceBuffer.mode = 'sequence';
}

function appendToBuffer(videoChunk) {
  if (sourceBuffer && sourceBuffer.updating) {
    console.log("Buffer not ready");
  }
  if (videoChunk && sourceBuffer && !sourceBuffer.updating) {
    sourceBuffer.appendBuffer(videoChunk);
    setTimeout(() => {
      console.log("setting currentTime now");
      video.currentTime = index;
    }, 5000);
  }
}

function getData(url) {
  if (url !== "") {
    url = "https://data.panomoments.com/processed/5849a51bc26d08000b62f9f8/5c95d9843c344e001f1588f6/" + url;
    var xhr = new XMLHttpRequest(); // Set up xhr request
    xhr.open("GET", url, true); // Open the request
    xhr.responseType = "text"; // Set the type of response expected
    xhr.send();

    //  Asynchronously wait for the data to return
    xhr.onreadystatechange = function() {
      if (xhr.readyState == xhr.DONE) {
        var tempoutput = xhr.response;
        var parser = new DOMParser(); //  create a parser object

        // Create an xml document from the .mpd file for searching
        var xmlData = parser.parseFromString(tempoutput, "text/xml");
        // Get and display the parameters of the .mpd file
       ...