npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mediasource

v2.4.0

Published

MediaSource API as a node.js Writable stream

Downloads

8,578

Readme

mediasource travis npm downloads javascript style guide

MediaSource API as a node.js Writable stream

Sauce Test Status

Stream video/audio into a <video> or <audio> tag by attaching node.js Writable streams.

This package is used by WebTorrent (along with other approaches) to support media streaming.

install

npm install mediasource

usage

var MediaElementWrapper = require('mediasource')

function createElem (tagName) {
  var elem = document.createElement(tagName)
  elem.controls = true
  elem.autoplay = true // for chrome
  elem.play() // for firefox
  document.body.appendChild(elem)
  return elem
}

var elem = createElem('video')

var readable = // ... get a readable stream from somewhere
var wrapper = new MediaElementWrapper(elem)
// The correct mime type, including codecs, must be provided
var writable = wrapper.createWriteStream('video/webm; codecs="vorbis, vp8"')

elem.addEventListener('error', function () {
  // listen for errors on the video/audio element directly
  var errorCode = elem.error
  var detailedError = wrapper.detailedError
  // wrapper.detailedError will often have a more detailed error message
})

writable.on('error', function (err) {
  // listening to the stream 'error' event is optional
})

readable.pipe(writable)

// media should start playing now!

advanced usage

wrapper.createWriteStream() can be called multiple times if different tracks (e.g. audio and video) need to be passed in separate streams. Each call should be made with the correct mime type.

Instead of a mime type, an existing MediaSourceStream (as returned by wrapper.createWriteStream()) can be passed as the single argument to wrapper.createWriteStream(), which will cause the existing stream to be replaced by the newly returned stream. This is useful when you want to cancel the existing stream and replace it with a new one, e.g. when seeking.

should one use this package?

Naively using this package will not work for many video formats, nor will it support seeking. For an approach that is more likely to work for all video files, and supports seeking, take a look at videostream.

Or for a package that tries multiple approaches, including videostream and this package (mediasource), as well as a Blob API (non-streaming) approach, and works for many non-video file types, consider render-media.

options

opts.bufferDuration

Specify how many seconds of media should be put into the browser's buffer before applying backpressure.

errors

Handle errors by listening to the 'error' event on the <video> or <audio> tag.

Some (but not all) errors will also cause wrapper.detailedError to be set to an error value that has a more informative error message.

license

MIT. Copyright (c) Feross Aboukhadijeh.