今天我們介紹一下如何對獲取到的視頻做一些特效
這部分知識實際上是屬於渲染部分的範疇
我們要在瀏覽器中做特效,我們要知道幾個基本的概念
CSS filter,-webkit-filter/filter
在不同的瀏覽器中使用的filter還不一樣
如何將video與 filter關聯
OpenGL/Metal/...
我們通過CSS調用,它最終在瀏覽器的底層調用的還是OpenGL/Metal/...這種基礎的圖形繪製庫 ,通過GPU進行繪製
具體支持哪些特效,我們下面羅列一下常見的
支持的特效種類
下面我們實際練習一下
index.html
<html>
<head>
<title>捕獲音視頻數據 WebRTC capture video and audio</title>
<style>
.none {
-webkit-filter: none;
}
.blur {
/* 特效模糊 */
-webkit-filter: blur(3px);
}
.grayscale {
/* 特效灰度 */
-webkit-filter: grayscale(1);
}
.invert {
/* 翻轉 */
-webkit-filter: invert(1);
}
.sepia {
/* 特效褐色 */
-webkit-filter: sepia(1);
}
</style>
</head>
<body>
<div>
<label>audio Source:</label>
<select id="audioSource"></select>
</div>
<div>
<label>audio Output:</label>
<select id="audioOutput"></select>
</div>
<div>
<label>video Source:</label>
<select id="videoSource"></select>
</div>
<!-- 特效選擇器 -->
<div>
<label>Filter:</label>
<select id="filter">
<option value="none">None</option>
<option value="blur">blur</option>
<option value="grayscale">Grayscale</option>
<option value="invert">Invert</option>
<option value="sepia">sepia</option>
</select>
</div>
<!--
我們創建一個video標籤,這個標籤就可以顯示我們捕獲的音視頻數據
autoplay 表示當我們拿到視頻源的時候直接播放
playsinlin 表示在瀏覽器頁面中播放而不是調用第三方工具
-->
<video autoplay playsinlin id="player"></video>
<!-- 引入 adapter.js庫 來做 不同瀏覽器的兼容 -->
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="./js/client.js"></script>
</body>
</html>
client.js
'use strict'
var audioSource = document.querySelector('select#audioSource');
var audioOutput = document.querySelector('select#audioOutput');
var videoSource = document.querySelector('select#videoSource');
// 獲取video標籤
var videoplay = document.querySelector('video#player');
//filter 特效選擇
var filtersSelect = document.querySelector('select#filter');
// deviceInfos是設備信息的數組
function gotDevices(deviceInfos){
// 遍歷設備信息數組, 函數裏面也有個參數是每一項的deviceinfo, 這樣我們就拿到每個設備的信息了
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方面裏面我們要傳人一個參數,也就是流,
// 這個流裏面實際上包含了音頻軌和視頻軌,因爲我們通過constraints設置了要採集視頻和音頻
// 我們直接吧這個流賦值給HTML中賦值的video標籤
// 當時拿到這個流了,說明用戶已經同意去訪問音視頻設備了
function gotMediaStream(stream){
videoplay.srcObject = stream; // 指定數據源來自stream,這樣視頻標籤採集到這個數據之後就可以將視頻和音頻播放出來
// 當我們採集到音視頻的數據之後,我們返回一個Promise
return navigator.mediaDevices.enumerateDevices();
}
function handleError(err){
console.log('getUserMedia error:', err);
}
function start() {
// 判斷瀏覽器是否支持
if(!navigator.mediaDevices ||
!navigator.mediaDevices.getUserMedia){
console.log('getUserMedia is not supported!');
}else{
// 獲取到deviceId
var deviceId = videoSource.value;
// 這裏是約束參數,正常情況下我們只需要是否使用視頻是否使用音頻
// 對於視頻就可以按我們剛纔所說的做一些限制
var constraints = { // 表示同時採集視頻金和音頻
video : {
width: 640, // 寬帶
height: 480, // 高度
frameRate:15, // 幀率
facingMode: 'enviroment', // 設置爲後置攝像頭
deviceId : deviceId ? deviceId : undefined // 如果deviceId不爲空直接設置值,如果爲空就是undefined
},
audio : {
noiseSuppression: true, // 降噪
echoCancellation: true // 迴音消除
},
}
// 從指定的設備中去採集數據
navigator.mediaDevices.getUserMedia(constraints)
.then(gotMediaStream) // 使用Promise串聯的方式,獲取流成功了
.then(gotDevices)
.catch(handleError);
}
}
start();
// 當我選擇攝像頭的時候,他可以觸發一個事件,
// 當我調用start之後我要改變constraints
videoSource.onchange = start;
// 選擇特效的方法
filtersSelect.onchange = function(){
videoplay.className = filtersSelect.value;
}
效果如下
模糊
灰度
翻轉底片
褐色