Giter Club home page Giter Club logo

yakamoz's Introduction

yakamoz

HLS framework for complex use cases.

Why?

Instead of current HLS implementations, Yakamoz provides a simple API to append buffers by yourself while handles unnecessary buffers and cleans them automaticly.

Usage

import Yakamoz from "yakamoz";
import { Parser } from "m3u8-parser";
import muxjs from "mux.js";

(async () => {
  //You can change these parts or you can connect it to indexedDB if you want!
  const baseURL = `http://localhost:5501/anime_uploads/weathering-with-you/1`;

  const m3u8File = `/1-7054810786387988481-1080p.m3u8`;
  const parser = new Parser();

  //fetching and parsing our m3u8 manifest
  async function fetchAndParseM3U8() {
    const f = await fetch(baseURL + m3u8File);
    const resp = await f.text();
    parser.push(resp);
    parser.end();
    return parser.manifest.segments;
  }

  const parsedSegments = await fetchAndParseM3U8();

  const request = (url) =>
    fetch(url).then((response) => response.arrayBuffer());

  //Transmuxer to convert mp2t buffers to fragmented mp4 buffers
  let transmuxer = new muxjs.mp4.Transmuxer();
  async function transmux(chunk, giveAsSourceBuffer) {
    return new Promise((resolve) => {
      transmuxer.off("data");
      transmuxer.on("data", (segment) => {
        if (giveAsSourceBuffer) {
          let data = new Uint8Array(
            segment.initSegment.byteLength + segment.data.byteLength
          );
          data.set(segment.initSegment, 0);
          data.set(segment.data, segment.initSegment.byteLength);
          resolve(data.buffer);
        } else {
          resolve(segment.data.buffer);
        }
      });

      transmuxer.push(new Uint8Array(chunk));
      transmuxer.flush();
    });
  }

  const yakamoz = new Yakamoz();

  //append the first buffer
  yakamoz.addEventListener("NEED_SOURCEBUFFER", async (e) => {
    const { append } = e.detail;
    const getNewBuffer = await request(`${baseURL}/${parsedSegments[0].uri}`);
    const buffer = await transmux(getNewBuffer, true);
    append(buffer);
  });

  //append proper segments when needed
  yakamoz.addEventListener("NEW_SEGMENT", async (e) => {
    const { append, targetSegment } = e.detail;
    const getNewBuffer = await request(
      `${baseURL}/${parsedSegments[targetSegment].uri}`
    );
    const buffer = await transmux(getNewBuffer);
    append(buffer);
  });

  //Do not forget to create a video element and mime property should match with the playing video.
  yakamoz.init({
    video: document.querySelector("video"),
    segments: parsedSegments,
    mime: 'video/mp4; codecs="avc1.640028,mp4a.40.2"; profiles="isom,iso2,avc1,mp41""',
  });

  //No need to clear unnecessary buffers. Yakamoz will clear them by default 😉
})();

yakamoz's People

Contributors

spongebed81 avatar wraith4081 avatar dependabot[bot] avatar hanzydev avatar

Stargazers

Emin.exe avatar ewriq avatar Nazım Sarp Tekbaş avatar erai :3 avatar Efe Hıdır avatar NullMan avatar Nicat avatar Nihad avatar  avatar Arda Samed Çelik avatar Derin Önder Eren avatar Mehmet Ali Külahçı avatar Mert Doğu avatar  avatar

Forkers

wraith4081

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.