# mp4-stream

> Streaming mp4 encoder and decoder

Latest version **3.1.3** (published 2021-02-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install mp4-stream
pnpm add mp4-stream
yarn add mp4-stream
bun add mp4-stream
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.3 |
| Published | 2021-02-24 |
| First published | 2015-11-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 11.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 215 |
| Author | Mathias Buus |
| Maintainers | mafintosh, feross, jhiesey |

## Links

- npm: https://www.npmjs.com/package/mp4-stream
- Repository: https://github.com/mafintosh/mp4-stream
- Issues: https://github.com/mafintosh/mp4-stream/issues
- npm.io page: https://npm.io/package/mp4-stream

## Dependencies (4)

- [next-event](https://npm.io/package/next-event.md) ^1.0.0
- [queue-microtask](https://npm.io/package/queue-microtask.md) ^1.2.2
- [readable-stream](https://npm.io/package/readable-stream.md) ^3.0.6
- [mp4-box-encoding](https://npm.io/package/mp4-box-encoding.md) ^1.3.0

## Recent versions

- 3.1.3 (latest) — 2021-02-24
- 3.1.2 — 2021-02-11
- 3.1.1 — 2021-02-11
- 3.1.0 — 2019-08-07
- 3.0.0 — 2018-12-12
- 2.0.3 — 2018-03-23
- 2.0.2 — 2016-11-23
- 2.0.1 — 2016-02-14
- 2.0.0 — 2016-02-01
- 1.0.0 — 2015-12-13
- 0.0.2 — 2015-12-09
- 0.0.1 — 2015-11-04
- 0.0.0 — 2015-11-03

## README

# mp4-stream

Streaming mp4 encoder and decoder

```
npm install mp4-stream
```

[![build status](http://img.shields.io/travis/mafintosh/mp4-stream.svg?style=flat)](http://travis-ci.org/mafintosh/mp4-stream)

## Usage

``` js
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

``` js
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.

``` js
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

``` js
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](https://github.com/jhiesey/mp4-box-encoding).

## License

MIT

---
_Source: https://npm.io/package/mp4-stream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
