Large size issue in MediaRecorder safari - javascript

While using MediaRecorder in Safari the size of video recorder is almost 15x greater than in Chrome for the same amount of time. Is there any way where we can reduce the final recorded size of the video
I tried changing pixel size of the video
await navigator.mediaDevices.getUserMedia({
video: {
width: { exact: 320 },
height: { exact: 240 }
}, audio: true
});
but still the size of the video is same

Related

Camera stream not loading on all devices with maximum quality requirements (HTML/Angular)

I'm developing an application in Angular to capture a photo with the maximum possible quality depending on the device's camera.
Currently, I have this code:
HTML:
<video #video id="video" autoplay muted playsinline></video>
Angular TS:
_requestCameraStream(width: number, height: number, secondTry: boolean) {
if (navigator.mediaDevices.getUserMedia)
{
navigator.mediaDevices
.getUserMedia({
video: {
facingMode: 'environment',
width: { ideal: width },
height: { ideal: height },
frameRate: { ideal: 30 },
},
})
.then(stream => {
console.log('_getUserMedia -> stream loaded');
this.loadStream(stream);
})
.catch(err => {
console.log(err);
if (!secondTry)
{
console.log('Started second try');
this._requestCameraStream(2560, 1440, true);
}
else
{
this.router.navigateByUrl('id-document/camera-error');
}
});
}
}
private loadStream(stream: MediaStream) {
const videoDevices = stream.getVideoTracks();
this.lastStream = stream;
this.video!.nativeElement.srcObject = stream;
this.video!.nativeElement.play();
this.ref.detectChanges();
}
Basically I check if the device has a camera available and try to load it with the width and height values that the function receives. On the first try I call the function as follows:
this._requestCameraStream(4096, 2160, false);
If the stream fails to load (probably because the camera does not support 4k quality) then it tries again with the values this._requestCameraStream(2560, 1440, true);
This is actually working pretty well on most devices, but on a Galaxy Note 10 Plus, the stream does not load, but if I click the button to take the picture, the camera does capture the image in 4k quality.
I suspect that the camera has a higher resolution than the screen, so the camera can capture a 4k image, but the screen can't load a 4k video as a preview. The problem is: the system does not trigger any warning or errors that I could capture. It is as if the preview loaded successfully.
How can I detect and treat this error? Or maybe, is there any other way that I can request the camera to capture a maximum quality image with the preview loading correctly?
You can try defining a range of resolutions instead of trying only two
async _requestCameraStream() {
if (!navigator.mediaDevices.getUserMedia) return;
try {
const stream = await navigator.mediaDevices
.getUserMedia({
video: {
facingMode: {
exact: "environment"
},
width: { min: 2288, ideal: 4096, max: 4096 },
height: { min: 1080, ideal: 2160, max: 2160 },
frameRate: { ideal: 30 },
},
});
if(stream) {
console.log('_getUserMedia -> stream loaded');
this.loadStream(stream);
}
}catch (err) {
console.log(err);
this.router.navigateByUrl('id-document/camera-error');
}
}
I think your current approach is correct to capture the maximum quality image. I have used a similar approach in one of my projects. I think the problem is with the video playback. There is an autoplay policy in the browser it behaves differently in a different browser. This browser policy does not allow to play video content without any user intervention. Check this URL, https://developer.chrome.com/blog/autoplay/
I think you should add a muted attribute in the video element and would be better if you ask the user to click before capturing the camera stream. May be this does not solve the problem which you are facing but this problem will be there in some browsers like iPhone. Apple devices do not allow any video content without user intervention.
Regards,
omi

On browser, unable to access back camera using navigator.getusermedia js in latest Samsung s10 and s20 phones ( Note:3 back cameras in latest phones)

In latest Samsung s10 and s20 phones, I facing back-camera blocked issue on browser while accessing using navigator.mediaDevices.getUserMedia javascript. But able to access front-camera successfully. These s10 and s20 mobile have 3plus back-cameras.
Note: it worked well on Samsung s9 for both front and back camera, I believe it has single back camera, so no camera access issue in s9.
Below is the simple JS code used to access back and front camera.
navigator.mediaDevices.getUserMedia({
video: {
width: screen.width > ipad_size ? 1280 : { ideal: 640 },
height: screen.width > ipad_size ? 720 : { ideal: 480 },
facingMode: method == 2 ? "user" : { exact: "environment" },
},
})
.then(function (stream){
console.log("Access camera: ");
})
.catch(function (err) {
console.log("Unable to access camera: " + err);
});
facingMode: method == 2 ? "user" : "environment",
is what I recommend.

WebRTC Errors recording video

I've tested https://webrtc.github.io/samples/src/content/getusermedia/record/, however it doesn't appear to function on iPhone, in Safari or Chrome, presenting getUserMedia errors.
The errors occur by default, I haven't changed settings, never tried this before, and it never prompts for camera access.
Clicking Start Camera:
iPhone Safari: navigator.getUserMedia error:Error: Invalid constraint
iPhone Chrome: navigator.getUserMedia error:TypeError: undefined is not an object (evaluating 'navigator.mediaDevices.getUserMedia')
Any ideas?
It sais the constraint is invalid.
This is the constraint they are using:
const constraints = {
audio: {
echoCancellation: {exact: hasEchoCancellation}
},
video: {
width: 1280, height: 720
}
};
could be echoCancellation is not supported on the safari. So maybe change the code to just
{
audio: true,
video: {
width: 1280, height: 720
}
}

When I try to stream my webcam video by using getUserMedia() api a black screen is appearing

I am trying to access the user's webcam by using navigator.getUserMedia(). I am assigning the video.srcObject to this stream. But I am getting a black screen on video.
I event tried with navigator.mediaDevices.getUserMedia()
<video controls id="webcam"></video>
<script>
const webcam = document.getElementById("webcam");
function startVideo() {
navigator
.getUserMedia({
video: true,
audio: false
},
liveStream => {
console.log(liveStream);
webcam.setAttribute("controls", 'true');
webcam.srcObject = liveStream;
webcam.play();
},
error => console.log(error)
)
}
startVideo();
</script>

How to detect user's connection speed using angular 2 and set quality to twilio video call

I am trying to set default video quality for video like this (from the example) :
createLocalTracks({
audio: true,
video: { width: 640 }
}).then(localTracks => {
return connect('$TOKEN', {
name: 'my-room-name',
tracks: localTracks
});
}).then(room => {
console.log('Connected to Room:', room.name);
});
instead of width : 640 i want to set dynamic resolution for the video base on user's internet connection. How can I do that?

Categories