Skip to content

Fix to generate valid .mp4 from ADTS stream #436

@gmcgarry

Description

@gmcgarry

Here's a simple nodejs module to convert .aac to .mp4

import muxjs from "./es/index.js"
import * as fs from 'fs'

const transmuxer = new muxjs.mp4.Transmuxer({ })

const readable = fs.createReadStream('sample.aac')
readable.on('data', (chunk) => transmuxer.push(chunk))
readable.on('end', () => transmuxer.flush())

const writable = fs.createWriteStream('output.mp4')

let isFirst = true

transmuxer.on('data', (segment) => {
    if (isFirst) {
        const data = new Uint8Array(segment.initSegment.byteLength + segment.data.byteLength)
        data.set(segment.initSegment, 0)
        data.set(segment.data, segment.initSegment.byteLength)
        writable.write(data)
    } else {
        writable.write(segment.data)
    }
    isFirst = false
})

Here's the patch to get it working so that the output file is playable:

diff --git a/lib/mp4/mp4-generator.js b/lib/mp4/mp4-generator.js
index a44781a..5ac031d 100644
--- a/lib/mp4/mp4-generator.js
+++ b/lib/mp4/mp4-generator.js
@@ -305,7 +305,7 @@ moov = function(tracks) {
     boxes[i] = trak(tracks[i]);
   }
 
-  return box.apply(null, [types.moov, mvhd(0xffffffff)].concat(boxes).concat(mvex(tracks)));
+  return box.apply(null, [types.moov, mvhd(0)].concat(boxes).concat(mvex(tracks)));
 };
 mvex = function(tracks) {
   var
@@ -637,7 +637,7 @@ traf = function(track) {
  * @return {Uint8Array} the track box
  */
 trak = function(track) {
-  track.duration = track.duration || 0xffffffff;
+  track.duration = track.duration
   return box(types.trak,
              tkhd(track),
              mdia(track));
diff --git a/lib/mp4/transmuxer.js b/lib/mp4/transmuxer.js
index a248585..8f709d6 100644
--- a/lib/mp4/transmuxer.js
+++ b/lib/mp4/transmuxer.js
@@ -962,6 +962,7 @@ Transmuxer = function(options) {
       }
 
       audioTrack = audioTrack || {
+        id: 1,
         timelineStartInfo: {
           baseMediaDecodeTime: self.baseMediaDecodeTime
         },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions