mux.js icon indicating copy to clipboard operation
mux.js copied to clipboard

How to Transmux NAL Units Stream to fragmented mp4

Open AmdEagle opened this issue 7 years ago • 12 comments

I have a stream of NAL packets coming in through the websocket. I would like to know how to setup so sent the NAL units into the Transmuxer so it output is a mp4. I was trying like this but i did not see any events getting triggered

let muxer = new muxjs.Transmuxer(); muxer.init();

muxer.on( 'data', segment => { console.log( segment ); }

function onNalUnitReceived( nalUnit ) { muxer.push( nalUnit ); }

How do i convert the nalUnit i receive in onNalUnitReceived callback to an mp4

AmdEagle avatar Mar 20 '17 03:03 AmdEagle

After you've pushed some data to the transmuxer, youll want to call muxer.flush() to finish processing the data youve pushed, otherwise you'll never see a data event

mjneil avatar Mar 20 '17 14:03 mjneil

I called muxer.flush that raised the event but segment.data is an empty array.. What am i doing wrong. Am i feeding in wrong data to the muxer ??

The data i am feeding is NAL Units [ h264 packets encoding using the main profile ]. Each packet starts with the default 00 00 00 01

AmdEagle avatar Mar 21 '17 01:03 AmdEagle

I suspect your case is like mine: mux.js is expecting access unit delimiters at the start of an access unit but I do not think this is required by the spec.

thatsnotright avatar Apr 25 '17 18:04 thatsnotright

Hi, I'm facing a similar problem. I receive mpeg-ts stream over WebSocket and try to transmux it using mux.js. What I've noticed is it does seem work once I collect around 14-16 packets and flush the transmuxer. If I flush it to early, I end up with the same issue as above - empty data array. So, flushing the transmuxer every 15 packets does the job, however the stream seems to be stuttering for the first couple of seconds and then begins to play smoothly - I guess it's because at that point the player has enough data in the buffer for a smooth playback. I wonder then, is there a way we could tell there's enough data for the transmuxer to generate a valid, combined buffer (video+audio data) that we could use to tell when to flush the transmuxer?

gregpabian avatar Jul 25 '17 08:07 gregpabian

You probably want to modify transmuxer a little so that you can call flush every packet, but it doesn't clear the collected nal units (resetStream), returns early if it built no frames (but keeps the nal units/frames) and produces a GOP on i-frame boundaries instead of looking for the access_unit_delimiter which is not required by the spec to be in the stream (but is helpful)

thatsnotright avatar Jul 28 '17 16:07 thatsnotright

Do you have any luck with this task? I have raw h264 stream extracted from mkv container. And I also getting empty segment.data array.

DmitriyKarpov avatar Apr 26 '18 00:04 DmitriyKarpov

PR #200 should solve this issue.

gwolny avatar Jun 29 '18 10:06 gwolny

Did anyone get this working? I'm interested in pumping raw h264 bitstream into transmux, extracting SEI nal units and wrapping the rest in an mp4 to be displayed.

trsaunders avatar Nov 12 '18 17:11 trsaunders

@gwolny Geat Job! help me a lot!

myseemylife avatar Jan 19 '19 08:01 myseemylife

Hello @gwolny and guys. I have some issue on let the patch (https://github.com/gwolny/mux.js/commit/437ecad8581f20cd8920345585a47cb72208ce7d) works.

I'm feeding the transmuxer with uint8array NAL units (included 0001 delimiter), but as told by some guys before me, the buffer from on data event is empty. Following the code:

let mux = new muxjs.mp4.Transmuxer(); mux.on('data', function (segment) { // segment.data.buffer is empty }); webSocket = new WebSocket("ws://ip_address/stream_id"); webSocket.binaryType = "arraybuffer"; webSocket.onmessage = function (messageEvent) { mux.push(new Uint8Array(messageEvent.data)); mux.flush(); }

The stream from ffmpeg is built with this command:

.\ffmpeg.exe -stream_loop -1 -re -i .\Files\timer.mp4 -c:v libx264 -vprofile baseline -b:v 600k -bufsize 600k -tune zerolatency -pix_fmt yuv420p -an -f rawvideo http://ip_address/stream_id

Please could you help me guys? Thanks

mbk2787 avatar Feb 27 '19 18:02 mbk2787

Hello,

I would like to give you an update about my issue.

I was able to get a proper segment.data.buffer after I merged following code https://github.com/videojs/mux.js/issues/227 (basically it performs an autoflush when it recognizes a end_of_seq_rbsp NAL) to the gwolny@437ecad patch.

Is it the right direction? Or what I'm doing does not make sense?

Thanks

mbk2787 avatar Mar 01 '19 10:03 mbk2787

Hi everyone,

any news on merging this PR in master?

exalight87 avatar Dec 12 '23 14:12 exalight87