mediasource
MediaSource API as a node.js Writable stream
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 = { var elem = document elemcontrols = true elemautoplay = true // for chrome elem // for firefox documentbody return elem} var elem = var readable = // ... get a readable stream from somewherevar wrapper = elem// The correct mime type, including codecs, must be providedvar writable = wrapper elem writable readable // 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.