webRTC(四):Webrtc音视频数据采集&录制&采集屏面数据,前端面试题最新2024

25 阅读3分钟

最后

资料过多,篇幅有限

自古成功在尝试。不尝试永远都不会成功。勇敢的尝试是成功的一半。

开源分享:docs.qq.com/doc/DSmRnRG…

音频约束和视频约束一样,在constraints中进行配置,更多详细可以查看官方api

  • 视屏特效

Filter:

None blur Grayscale Invert sepia

//特效

filtersSelect.onchange = function(){

videoplay.className=filtersSelect.value;

}

设置特效直接设置视频源video的className即可

  • 从视频中获取图片

Take snapshot

//从视频中获取图片

var snapshot =document.querySelector("button#snapshot");

var picture =document.querySelector("canvas#picture");

picture.width=480;

picture.height=640;

//从视频中获取图片

snapshot.onclick=function(){

picture.className=filtersSelect.value

picture.getContext('2d').drawImage(videoplay,

0,0,

picture.width,

picture.height);

}

从视频中获取图片主要使用的是canvas来绘制的

  • MediaStreamAPI及获取视频约束

//获取屏幕约束

var divConstraints = document.querySelector('div#constraints')

function gotMediaStream(stream){

var videoTrack = stream.getVideoTracks()[0];

var videoConstraints = videoTrack.getSettings();

divConstraints.textContent= JSON.stringify(videoConstraints,null,2);

videoplay.srcObject=stream;

return navigator.mediaDevices.enumerateDevices();

}

结果

{ "aspectRatio": 1.3333333333333333, "deviceId": "97953df027728ab0acac98c670d59f654a1e7f36f9faf70f2e0fd7a479394fe3",

"frameRate": 29.969999313354492, "groupId": "1b83734781c08e3c51519598002aa1d5acb1bcd73772f5d2db4b976586af3666",

"height": 480, "width": 640, "videoKind": "color" }

获取视频约束,在gotMediaStream方法中获取视频轨道,信息都在轨道中获取

  • 录制音频视屏

//视频录制

btnRecord.onclick=()=>{

if(btnRecord.textContent==='Start Record'){

startRecord();

btnRecord.textContent='Stop Record'

btnPlay.disabled=true;

btnDownload.disabled=true;

}else{

stopRecord();

btnRecord.textContent='Start Record'

btnPlay.disabled=false;

btnDownload.disabled=false;

}

}

function gotMediaStream(stream){

...

window.stream=stream;

...

return navigator.mediaDevices.enumerateDevices();

}

//开始录制

function startRecord(){

buffer=[];

var options={

mimeType: 'video/webm;codecs=vp8'

}

if(!window.MediaRecorder.isTypeSupported(options.mimeType)){

console.error('${options.mimeType} is not supported');

return;

}

try {

mediaRecorder= new window.MediaRecorder(window.stream,options);

} catch (e) {

console.error('failed to create MediaRecorder:',e);

return;

}

mediaRecorder.ondataavailable= handleDataAvailable;

mediaRecorder.start(10);

}

//停止录制

function stopRecord(){

mediaRecorder.stop();

}

  • 播放录制视频

btnPlay.onclick=()=>{

var blob =new Blob(buffer,{type: 'video/webm'});

recvideo.src=window.URL.createObjectURL(blob);

recvideo.srcObject=null;

recvideo.controls=true;

recvideo.play();

}

  • 下载录制视频

btnDownload.onclick=()=>{

var blob =new Blob(buffer,{type:'video/webm'});

var url = window.URL.createObjectURL(blob);

var a=document.createElement('a');

a.href=url;

a.style.display='none';

a.download='aaa.webm';

a.click();

}


  • 采集屏面数据

//getDisplayMedia 捕获桌面 ,getUserMedia 捕获摄像头数据

function start(){

//捕获桌面

if (!navigator.mediaDevices||

!navigator.mediaDevices.getDisplayMedia) {

console.log("getUserMedia is not supported!")

return;

} else {

//捕获桌面

var constraints1={

video: true,

audio: true,

}

//getDisplayMedia 捕获桌面 ,getUserMedia 捕获摄像头数据

navigator.mediaDevices.getDisplayMedia(constraints1)

.then(gotMediaStream)

.then(gotDevices)

.catch(handleError)

}

}

采集屏幕数据其实和采集音视频信息一直,只是将getUserMedia替换成getDisplayMedia即可.

注意:要使用google浏览器打开Experimental Web Platform features

在这里插入图片描述


全部代码


  • html
WebRtc capture video and audio .none{ -webkit-filter:none; } .blur{ -webkit-filter:blur(3px); } .grayscale{ -webkit-filter:grayscale(1); } .invert{ -webkit-filter:invert(1); } .sepia{ -webkit-filter:sepia(1); }

audio input device:

audio output device:

video input device:

Filter:

None blur Grayscale Invert sepia

Start Record

Play

Download

Take snapshot

  • js

'use strict'

var videoplay=document.querySelector('video#player')

var audioSource =document.querySelector("select#audioSource");

var audioOutput =document.querySelector("select#audioOutput");

var videoSource =document.querySelector("select#videoSource");

//特效

var filtersSelect =document.querySelector("select#filter");

//从视频中获取图片

var snapshot =document.querySelector("button#snapshot");

var picture =document.querySelector("canvas#picture");

picture.width=480;

picture.height=640;

//获取屏幕约束

var divConstraints = document.querySelector('div#constraints')

//录制音视频

var recvideo = document.querySelector('video#recplayer')

var btnRecord = document.querySelector('button#record')

var btnPlay = document.querySelector('button#recplay')

var btnDownload = document.querySelector('button#download')

var buffer;

var mediaRecorder;

function gotMediaStream(stream){

var videoTrack = stream.getVideoTracks()[0];

var videoConstraints = videoTrack.getSettings();

divConstraints.textContent= JSON.stringify(videoConstraints,null,2);

window.stream=stream;

videoplay.srcObject=stream;

return navigator.mediaDevices.enumerateDevices();

}

function handleError(err){

console.log("getUserMedia error:",err);

}

function gotDevices(deviceInfos){

deviceInfos.forEach(function(deviceinfo){

var option= document.createElement('option');

option.text=deviceinfo.label;

option.value=deviceinfo.deviceId;

if(deviceinfo.kind==='audioinput'){

audioSource.appendChild(option);

}else if(deviceinfo.kind==='audiooutput'){

audioOutput.appendChild(option);

}else if(deviceinfo.kind==='videoinput'){

videoSource.appendChild(option);

}

})

}

//getDisplayMedia 捕获桌面 ,getUserMedia 捕获摄像头数据

function start(){

//捕获摄像头数据

if (!navigator.mediaDevices||

!navigator.mediaDevices.getUserMedia) {

//捕获桌面

// if (!navigator.mediaDevices||

// !navigator.mediaDevices.getDisplayMedia) {

console.log("getUserMedia is not supported!")

return;

} else {

var deviceId=videoSource.value;

//捕获摄像头数据

var constraints={

video: {

width:640,

height:480,

frameRate:30,

//environment:后置摄像头,user:前置摄像头

facingMode:"user",

deviceId: {exact:deviceId ? deviceId:undefined}

},

audio: {

//降噪

noiseSuppression:true,

//回音消除

echoCancellation:true

},

}

//捕获桌面

var constraints1={

video: true,

audio: true,

}

//getDisplayMedia 捕获桌面 ,getUserMedia 捕获摄像头数据

navigator.mediaDevices.getUserMedia(constraints)

// navigator.mediaDevices.getDisplayMedia(constraints1)

.then(gotMediaStream)

.then(gotDevices)

.catch(handleError)

}

}

start();

videoSource.onchange=start;

基础面试题

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

主要内容包括:HTML,CSS,JavaScript,浏览器,性能优化等等