phoboslab / jsmpeg

MPEG1 Video Decoder in JavaScript
MIT License
6.37k stars 1.43k forks source link

possible to get width/height of decoded image for live streaming? #79

Closed iownthegame closed 7 years ago

iownthegame commented 8 years ago

Hi, I'm using jsmpeg for live streaming. I'm wondering if I can know the width and height information of the decoded image(when ready to output)? In my condition, the size of encoded image may be variant(for example, sometimes 480x800 sometimes 360x600), I couldn't know the width height in advance and let it be a fixed value.

Thanks.

iownthegame commented 8 years ago

Oh I founded, the codedWidth and codedHeight, but still not the exactly value...

phoboslab commented 8 years ago

It's just the .width and .height properties. However, for streaming over WebSockets, the width and height can only be determined after the connection is established and the header received. There's currently no callback for a "header received" event, but you can just wait for the first ondecodeframe.

var hasDimensions = false;
var player = new jsmpeg(socket, {ondecodeframe: function(){
    if (!hasDimensions) {
    hasDimensions = true;
        // Do something with the width and height
        console.log(player.width, player.height);
    }
});
iownthegame commented 8 years ago

So you mean I still need to specify the width and height before rather than getting the value after decode process?

For instance, I also used the Broadway - h264 js decoder, on its onPictureDecoded() callback will reported the imagedata, width and height. However, the jsmpg.js need to specify the width and height initially in order to run the decoding process?

phoboslab commented 8 years ago

You don't need to define the width in JavaScript. You can read the width and height from the jsmpeg instance as shown above.

In the current setup however, you need to define the width and height that ffmpeg spits out in the URL to the stream server. E.g.:

ffmpeg -s 640x480 -f video4linux2 -i /dev/video0 -f mpeg1video \
-b 800k -r 30 http://example.com:8082/yourpassword/640/480/
iownthegame commented 8 years ago

okay, I got your point, many thanks. But in my case, I have a program that may control the output url and the ffmpeg parameters dynamically, so in this case I need to make additional headers contains current width and height information in each of the data segment I received from the web socket, then I need additional part of code in jsmpg.js to adjust the width and height(and some others properties such as codedWidth, halfWidth), dose it make sense?

I'm curious if we don't know the width and height information of the encoded data, then is it still possible to decode? thanks a lot.

phoboslab commented 8 years ago

A header containing the width/height is already sent to jsmpeg when streaming over WebSockets. See socketServer on connect and decodeSocketHeader.