Get maximum video resolution with getUserMedia

后端 未结 7 1105
天命终不由人
天命终不由人 2020-12-13 18:17

I\'m trying to get highest video resolution as possible through JS navigator.getUserMedia. I know about constraints, but don\'t know how to choose right in my c

7条回答
  •  我在风中等你
    2020-12-13 18:45

    You can always use applyConstraints() combined with getCapabilities() on the track. Honza Kalfus' answer claims chrome now handles the ideal prop right for resolution. I didn't test that, but Chrome 83 still screws up ideal value for frameRate and simply wont start the video if outside of the capable range... so I hope this helps someone.

    stream = await navigator.mediaDevices.getUserMedia({ video: { width: 100, height: 100, frameRate: { min: 8, max:60 }, facingMode: 'environment' } });
    track = stream.getTracks()[0];
    var frIdeal = 35;
    constrs = track.getConstraints();
    frCap = track.getCapabilities().frameRate;
    if (frCap && "min" in frCap && "max" in frCap) {
      constrs.frameRate = Math.max(frCap.min, Math.min(frCap.max, frIdeal));
      track.applyConstraints(constrs);
    }
    

    this will give me 30hz if that is the maximum (Chrome83).

    see also the MDN docs

提交回复
热议问题