gpac / mp4box.js

JavaScript version of GPAC's MP4Box tool
https://gpac.github.io/mp4box.js/
BSD 3-Clause "New" or "Revised" License
1.98k stars 333 forks source link

Moov never found in Sparse File #217

Closed lukepolo closed 3 years ago

lukepolo commented 4 years ago

The goal is that we write to these sparse files continuously, and want to stream video to the browser as it comes in. We only want to send buffers when we detect a moov, however a moov is never detected.

function watchFile(file: string, filePos?: number) {
  fs.open(file, "r", async (err, fd) => {
    if (err) {
      return console.error(err);
    }

    const mp4 = MP4Box.createFile();
    mp4.onError = function(error) {
      console.error(`ERROR`, error);
    };

    mp4.onReady = function(info) {
      console.info(info);
    };

    if (filePos === undefined) {
      // Get position at end of the of Sparse File
      filePos = lseek(fd, 0, Whence.SEEK_END);
    }

    fs.watch(file, (eventType, filename) => {
      const fileReader = fs.createReadStream(file, {
        start: filePos,
      });
      fileReader.on("readable", () => {
        const chunk = fileReader.read();
        if (chunk) {
          const arrayBuffer = toArrayBuffer(chunk);
          // @ts-ignore
          arrayBuffer.fileStart = filePos;
          filePos += arrayBuffer.byteLength;
          mp4.appendBuffer(arrayBuffer);

          const mp4Info = mp4.getInfo();
          if (mp4Info.hasMoov) {
            console.info("SEND BUFFER TO BROWSER", mp4.getBuffer());
            mp4.flush();
            // TODO - we need to setup a new mp4 instance?
          }
        }
      });
    });
  });
}

// watchFile("/media/test.mp4", 0);
watchFile("/media/stream.mp4");

Also to note , that using a static mp4 file does seem to work.

UPDATE It seems mp4box cannot detect moovs that may come in the middle? We can not guarantee the moov will be at the start or ending.

cconcolato commented 3 years ago

Can you share an example file?

lukepolo commented 3 years ago

Here is a sample of the type of data we are reading

226596175-424.bin.zip

cconcolato commented 3 years ago

The moov is never detected because it's not complete, at least in the file you shared.

lukepolo commented 3 years ago

Correct, our chunk size is private chunkSize = 1024 * 64; , so we may run into this issue. We fixed it by just pausing reading and reading from the file position again