Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to generate valid .mp4 from ADTS stream #436

Open
gmcgarry opened this issue Aug 8, 2023 · 0 comments
Open

Fix to generate valid .mp4 from ADTS stream #436

gmcgarry opened this issue Aug 8, 2023 · 0 comments

Comments

@gmcgarry
Copy link

gmcgarry commented Aug 8, 2023

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
         },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant