How to set the video quality in getUserMedia?

To adjust the video quality of getUserMedia, you can use the MediaTrackConstraints object to specify the desired parameters. The following example code demonstrates how to set the maximum and minimum values for video width and height.

navigator.mediaDevices.getUserMedia({
  video: {
    width: { min: 640, ideal: 1280, max: 1920 },
    height: { min: 480, ideal: 720, max: 1080 }
  }
})
.then(function(stream) {
  // 处理视频流
})
.catch(function(error) {
  console.log('访问摄像头失败: ', error);
});

In the code above, the width and height properties specify the minimum, ideal, and maximum width and height of the video respectively. These can be adjusted according to specific needs.

Please note that the actual video stream may not meet the specified exact parameters, especially on mobile devices. Therefore, it is recommended to use a range of values to set the quality and resolution.

bannerAds