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

mp4-stream

v3.1.3

Published

Streaming mp4 encoder and decoder

Downloads

6,304

Readme

mp4-stream

Streaming mp4 encoder and decoder

npm install mp4-stream

build status

Usage

var mp4 = require('mp4-stream')
var fs = require('fs')

var decode = mp4.decode()

fs.createReadStream('video.mp4')
  .pipe(decode)
  .on('box', function (headers) {
    console.log('found box (' + headers.type + ') (' + headers.length + ')')
    if (headers.type === 'mdat') {
      // you can get the contents as a stream
      console.log('box has stream data (consume stream to continue)')
      decode.stream().resume()
    } else if (headers.type === 'moof') {
      // you can ignore some boxes
      decode.ignore()
    } else {
      // or you can fully decode them
      decode.decode(function (box) {
        console.log('box contents:', box)
      })
    }
  }
  })

All boxes have a type thats a 4 char string with a type name.

API

var stream = mp4.decode()

Create a new decoder.

The decoder is a writable stream you should write a mp4 file to. It emits the following additional events:

  • on('box', headers) - emitted when a new box is found.

Each time the box event fires, you must call one of these three functions:

  • stream.ignore() - ignore the entire box and continue parsing after its end
  • stream.stream() - get a readable stream of the box contents
  • stream.decode(callback) - decode the box, including all childeren in the case of containers, and pass the resulting box object to the callback
var fs = require('fs')
var stream = mp4.decode()

stream.on('box', function (headers) {
  console.log('found new box:', headers)
})

fs.createReadStream('my-video.mp4').pipe(stream)

var stream = mp4.encode()

Create a new encoder.

The encoder is a readable stream you can use to generate a mp4 file. It has the following API:

  • stream.box(box, [callback]) - adds a new mp4 box to the stream.
  • var ws = stream.mediaData(size) - helper that adds an mdat box. write the media content to this stream.
  • stream.finalize() - finalizes the mp4 stream. call this when you're done.
var fs = require('fs')
var stream = mp4.encode()

stream.pipe(fs.createWriteStream('my-new-video.mp4'))

stream.box(anMP4Box, function (err) {
  // box flushed

  var content = stream.mediaData(lengthOfStream, function () {
    // wrote media data
    stream.finalize()
  })

  someContent.pipe(content)
})

Decode and encode a file

To decode and encode an mp4 file with this module do

var encoder = mp4.encode()
var decoder = mp4.decode()

decoder.on('box', function (headers) {
  decoder.decode(function (box) {
    encoder.box(box, next)
  })
})

fs.createReadStream('my-movie.mp4').pipe(decoder)
encoder.pipe(fs.createWriteStream('my-movie-copy.mp4'))

Boxes

Mp4 supports a wide range of boxes, implemented in mp4-box-encoding.

License

MIT