
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
mediasource
Advanced tools
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.
npm install mediasource
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!
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.
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.
Specify how many seconds of media should be put into the browser's buffer before applying backpressure.
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.
MIT. Copyright (c) Feross Aboukhadijeh.
FAQs
MediaSource API as a node.js Writable stream
The npm package mediasource receives a total of 913 weekly downloads. As such, mediasource popularity was classified as not popular.
We found that mediasource demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.