2024年前端最全webRTC(四):Webrtc音视频数据采集&录制&采集屏面数据,2024年最新字节跳动面试没过会发邮件吗

紧跟潮流

大前端和全栈是以后前端的一个趋势,懂后端的前端,懂各端的前端更加具有竞争力,以后可以往这个方向靠拢。

这边整理了一个对标“阿里 50W”年薪企业高级前端工程师成长路线,由于图片太大仅展示一小部分

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

}

音视频数据采集主要使用getUserMedia方法获取媒体数据,constraints配置采集轨道的参数,video,audio的true表示采集,false表示不采集,然后将数据流通过gotMediaStream方法添加到视频组建上。

  • WebRTC_API_适配

添加官方的adapter-latest支持即可

  • 获取音视频设备的访问权限

function gotMediaStream(stream){

videoplay.srcObject=stream;

return navigator.mediaDevices.enumerateDevices();

}

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);

}

})

}

在gotMediaStream方法返回return navigator.mediaDevices.enumerateDevices(),这时gotDevices方法中就可以获取音视频设备

  • 视频约束

var constraints={

video: {

width:640,

height:480,

frameRate:30,

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

facingMode:“user”,

deviceId: {exact:deviceId ? deviceId:undefined}

},

视频约束所有的配置都在constraints中进行配置,更多详细可以查看官方api

  • 音频约束

var constraints={

video: {

width:640,

height:480,

frameRate:30,

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

facingMode:“user”,

deviceId: {exact:deviceId ? deviceId:undefined}

},

audio: {

//降噪

noiseSuppression:true,

//回音消除

echoCancellation:true

},

}

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

  • 视屏特效

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.οnclick=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.οnclick=()=>{

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.οnclick=()=>{

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

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

recvideo.srcObject=null;

recvideo.controls=true;

recvideo.play();

}

  • 下载录制视频

btnDownload.οnclick=()=>{

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 blur Grayscale Invert sepia

学习分享,共勉

题外话,毕竟我工作多年,深知技术改革和创新的方向,Flutter作为跨平台开发技术、Flutter以其美观、快速、高效、开放等优势迅速俘获人心

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

iv>

None blur Grayscale Invert sepia

学习分享,共勉

题外话,毕竟我工作多年,深知技术改革和创新的方向,Flutter作为跨平台开发技术、Flutter以其美观、快速、高效、开放等优势迅速俘获人心

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

[外链图片转存中…(img-yqxsITjs-1715563435027)]

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值