{"id":13634721,"url":"https://github.com/mafintosh/mp4-stream","last_synced_at":"2025-04-12T19:48:30.903Z","repository":{"id":48829697,"uuid":"45442166","full_name":"mafintosh/mp4-stream","owner":"mafintosh","description":"Streaming mp4 encoder and decoder","archived":false,"fork":false,"pushed_at":"2021-02-24T23:22:34.000Z","size":66,"stargazers_count":218,"open_issues_count":11,"forks_count":21,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-12T19:48:27.424Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mafintosh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-03T04:43:48.000Z","updated_at":"2025-02-11T15:48:20.000Z","dependencies_parsed_at":"2022-08-27T21:10:41.267Z","dependency_job_id":null,"html_url":"https://github.com/mafintosh/mp4-stream","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fmp4-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fmp4-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fmp4-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fmp4-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mafintosh","download_url":"https://codeload.github.com/mafintosh/mp4-stream/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625500,"owners_count":21135513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T00:00:31.735Z","updated_at":"2025-04-12T19:48:30.877Z","avatar_url":"https://github.com/mafintosh.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","graphic (图形库)"],"sub_categories":[],"readme":"# mp4-stream\n\nStreaming mp4 encoder and decoder\n\n```\nnpm install mp4-stream\n```\n\n[![build status](http://img.shields.io/travis/mafintosh/mp4-stream.svg?style=flat)](http://travis-ci.org/mafintosh/mp4-stream)\n\n## Usage\n\n``` js\nvar mp4 = require('mp4-stream')\nvar fs = require('fs')\n\nvar decode = mp4.decode()\n\nfs.createReadStream('video.mp4')\n  .pipe(decode)\n  .on('box', function (headers) {\n    console.log('found box (' + headers.type + ') (' + headers.length + ')')\n    if (headers.type === 'mdat') {\n      // you can get the contents as a stream\n      console.log('box has stream data (consume stream to continue)')\n      decode.stream().resume()\n    } else if (headers.type === 'moof') {\n      // you can ignore some boxes\n      decode.ignore()\n    } else {\n      // or you can fully decode them\n      decode.decode(function (box) {\n        console.log('box contents:', box)\n      })\n    }\n  }\n  })\n```\n\nAll boxes have a type thats a 4 char string with a type name.\n\n## API\n\n#### `var stream = mp4.decode()`\n\nCreate a new decoder.\n\nThe decoder is a writable stream you should write a mp4 file to. It emits the following additional events:\n\n* `on('box', headers)` - emitted when a new box is found.\n\nEach time the `box` event fires, you must call one of these three functions:\n\n* `stream.ignore()` - ignore the entire box and continue parsing after its end\n* `stream.stream()` - get a readable stream of the box contents\n* `stream.decode(callback)` - decode the box, including all childeren in the case of containers, and pass\nthe resulting box object to the callback\n\n``` js\nvar fs = require('fs')\nvar stream = mp4.decode()\n\nstream.on('box', function (headers) {\n  console.log('found new box:', headers)\n})\n\nfs.createReadStream('my-video.mp4').pipe(stream)\n```\n\n#### `var stream = mp4.encode()`\n\nCreate a new encoder.\n\nThe encoder is a readable stream you can use to generate a mp4 file. It has the following API:\n\n* `stream.box(box, [callback])` - adds a new mp4 box to the stream.\n* `var ws = stream.mediaData(size)` - helper that adds an `mdat` box. write the media content to this stream.\n* `stream.finalize()` - finalizes the mp4 stream. call this when you're done.\n\n``` js\nvar fs = require('fs')\nvar stream = mp4.encode()\n\nstream.pipe(fs.createWriteStream('my-new-video.mp4'))\n\nstream.box(anMP4Box, function (err) {\n  // box flushed\n\n  var content = stream.mediaData(lengthOfStream, function () {\n    // wrote media data\n    stream.finalize()\n  })\n\n  someContent.pipe(content)\n})\n\n```\n\n## Decode and encode a file\n\nTo decode and encode an mp4 file with this module do\n\n``` js\nvar encoder = mp4.encode()\nvar decoder = mp4.decode()\n\ndecoder.on('box', function (headers) {\n  decoder.decode(function (box) {\n    encoder.box(box, next)\n  })\n})\n\nfs.createReadStream('my-movie.mp4').pipe(decoder)\nencoder.pipe(fs.createWriteStream('my-movie-copy.mp4'))\n```\n\n## Boxes\n\nMp4 supports a wide range of boxes, implemented in\n[mp4-box-encoding](https://github.com/jhiesey/mp4-box-encoding).\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Fmp4-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmafintosh%2Fmp4-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Fmp4-stream/lists"}