Open stef-coenen opened 1 year ago
https://github.com/hughfenghen/WebAV/blob/main/packages/av-canvas/src/mp4-utils.ts#L364
Maybe you can refer to stream2file
and file2stream
two functions, you can read data from the stream returned by file2stream
and upload it to the server.
export function file2stream (
file,
timeSlice
) {
let timerId = 0
let sendedBoxIdx = 0
const boxes = file.boxes
const deltaBuf = (): ArrayBuffer => {
const ds = new mp4box.DataStream()
ds.endianness = mp4box.DataStream.BIG_ENDIAN
for (let i = sendedBoxIdx; i < boxes.length; i++) {
boxes[i].write(ds)
}
sendedBoxIdx = boxes.length
return ds.buffer
}
let stoped = false
let exit = null
const stream = new ReadableStream({
start (ctrl) {
timerId = self.setInterval(() => {
ctrl.enqueue(deltaBuf())
}, timeSlice)
exit = () => {
clearInterval(timerId)
file.flush()
ctrl.enqueue(deltaBuf())
ctrl.close()
}
if (stoped) exit()
},
cancel () {
clearInterval(timerId)
}
})
return {
stream,
stop: () => {
stoped = true
exit?.()
}
}
}
This repository code is still unstable, maybe in the future it will packaged an MP4 + WebCodecs tool and release it to npm.
We are trying to build an app with support for streaming fragmented mp4's directly from the server. In order to do so we are looking to segment an mp4 during the upload. The goal is to get a segmented byteArray from a readable stream. We currently have the following, but the moov box is incorrect and we can't get the audio track included.
Any clues, where to look? The documentation only got us so far, and the library has many more features, so we are confident is has to be possible in some way.