起因
当前程序是以electron + vue作为前端,go作为后端http主服务。最近新来了一个需求,需要能够使用多个PC USB摄像头进行录像,并能够区分到底是哪个摄像头在录,这就需要前端页面能够实时的展示摄像头视频。
实现方式
- 浏览器支持(例:chrome),直接调用js的MediaRecorder对象来处理。
- 搭建rtmp服务器,通过推流(如:ffmpeg)和拉视频流的方式展示视频。
- 后端通过ffmpeg获取视频流,转成图片发给界面展示。
浏览器的MediaRecorder
fileSaver.js 用于浏览器保存文件
<!DOCTYPE html>
<html>
<head>
<title>video recoder</title>
<script src="./fileSaver.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<!-- <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"> -->
</head>
<style>
body {
background-color: #EFEDEF;
}
</style>
<body>
<article style="border:1px solid white;width:400px;height:400px;margin:0 auto;background-color:white;">
<section class="experiment" style="width:320px; height:240px;border:1px solid green; margin:50px auto;">
<div id="videos-container" style="width:320px; height:240px;">
</div>
</section>
<section class="experiment" style="text-align:center;border:none; margin-top:20px;">
<button id="openCamera">打开摄像头</button>
<button id="start-recording" disabled>开始录制</button>
<button id="save-recording" disabled>保存</button>
<!--<a href="javascript:void(0)" οnclick="send()">发送</a>-->
</section>
</article>
<!-- <div style="display:none;"> -->
<!-- <div>
<button οnclick="tGet();">测试https请求</button>
</div> -->
<script>
var mediaStream;
var recorderFile;
var stopRecordCallback;
var openBtn = document.getElementById("openCamera");
var startBtn = document.getElementById("start-recording");
var saveBtn = document.getElementById("save-recording");
openBtn.onclick = function () {
this.disabled = true;
startBtn.disabled = false;
openCamera();
};
startBtn.onclick = function () {
this.disabled = true;
startRecord();
};
saveBtn.onclick = function () {
saver();
// alert('Drop WebM file on Chrome or Firefox. Both can play entire file. VLC player or other players may not work.');
};
var mediaRecorder;
var videosContainer = document.getElementById('videos-container');
function openCamera() {
var len = videosContainer.childNodes.length;
for (var i = 0; i < len; i++) {
videosContainer.removeChild(videosContainer.childNodes[i]);
}
var video = document.createElement('video');
var videoWidth = 320;
var videoHeight = 240;
video.controls = false;
video.muted = true;
video.width = videoWidth;
video.height = videoHeight;
MediaUtils.getUserMedia(true, false, function (err, stream) {
if (err) {
throw err;
} else {
// 通过 MediaRecorder 记录获取到的媒体流
console.log("通过 MediaRecorder 记录获取到的媒体流");
mediaRecorder = new MediaRecorder(stream);
mediaStream = stream;
var chunks = [],
startTime = 0;
video.srcObject = stream;
video.play();
videosContainer.appendChild(video);
mediaRecorder.ondataavailable = function (e) {
console.log("ondataavailable:", e)
mediaRecorder.blobs.push(e.data);
chunks.push(e.data);
// f = new Blob([e.data], {
// 'type': mediaRecorder.mimeType
// });
// send1(f);
};
mediaRecorder.blobs = [];
mediaRecorder.onstop = function (e) {
recorderFile = new Blob(chunks, {
'type': mediaRecorder.mimeType
});
chunks = [];
if (null != stopRecordCallback) {
stopRecordCallback();
}
};
}
});
}
// 停止录制
function stopRecord(callback) {
stopRecordCallback = callback;
// 终止录制器
mediaRecorder.stop();
// 关闭媒体流
MediaUtils.closeStream(mediaStream);
}
var MediaUtils = {
/**
* 获取用户媒体设备(处理兼容的问题)
* @param videoEnable {boolean} - 是否启用摄像头
* @param audioEnable {boolean} - 是否启用麦克风
* @param callback {Function} - 处理回调
*/
getUserMedia: function (videoEnable, audioEnable, callback) {
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator
.mozGetUserMedia ||
navigator.msGetUserMedia || window.getUserMedia;
var constraints = {
video: videoEnable,
audio: audioEnable
};
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
callback(false, stream);
})['catch'](function (err) {
callback(err);
});
} else if (navigator.getUserMedia) {
navigator.getUserMedia(constraints, function (stream) {
callback(false, stream);
}, function (err) {
callback(err);
});
} else {
callback(new Error('Not support userMedia'));
}
},
/**
* 关闭媒体流
* @param stream {MediaStream} - 需要关闭的流
*/
closeStream: function (stream) {
if (typeof stream.stop === 'function') {
stream.stop();
} else {
let trackList = [stream.getAudioTracks(), stream.getVideoTracks()];
for (let i = 0; i < trackList.length; i++) {
let tracks = trackList[i];
if (tracks && tracks.length > 0) {
for (let j = 0; j < tracks.length; j++) {
let track = tracks[j];
if (typeof track.stop === 'function') {
track.stop();
}
}
}
}
}
}
};
function startRecord() {
mediaRecorder.start();
// mediaRecorder.start(5000);
setTimeout(function () {
// 结束
stopRecord(function () {
console.log("录制成功!");
openBtn.disabled = false;
saveBtn.disabled = false;
console.log(recorderFile);
// 直接保存
// saver();
// alert("保存成功");
// send();
});
}, 1000 * 5);
}
function saver() {
var file = new File([recorderFile], 'msr-' + (new Date).toISOString().replace(/:|\./g, '-') + '.mp4', {
type: 'video/mp4'
});
saveAs(file);
}
function send() {
var file = new File([recorderFile], 'msr-' + (new Date).toISOString().replace(/:|\./g, '-') + '.mp4', {
type: 'video/mp4'
});
var data = new FormData();
data.append("username", "test");
data.append("file", file);
var req = new XMLHttpRequest();
req.open("POST", "http://127.0.0.1:4780/upload");
req.send(data);
}
function send1(bbb) {
var file = new File([bbb], 'msr-' + (new Date).toISOString().replace(/:|\./g, '-') + '.webm', {
type: 'video/webm'
});
var data = new FormData();
data.append("username", "test");
data.append("file", file);
var req = new XMLHttpRequest();
// req.open("POST", "http://127.0.0.1:4780/upload");
req.open("POST", "https://127.0.0.1:7080/upload");
req.send(data);
}
function tGet() {
timedGetTest("https://127.0.0.1:7080", 5000)
}
function timedGetTest(url, time, callback) {
var request = new XMLHttpRequest();
var timeout = false;
var timer = setTimeout(function () {
console.log("超时了:", arguments);
timeout = true;
request.abort();
}, time);
request.open("GET", url);
request.onreadystatechange = function () {
console.log(arguments);
if (request.readyState !== 4) return;
if (timeout) return;
clearTimeout(timer);
if (request.status === 200) {
callback(request.responseText);
}
}
request.send(null);
}
</script>
</body>
</html>
直播推流拉流式
主要使用了开源库livego来作为rtmp服务器(其支持的协议有RTMP/AMF/HLS/HTTP-FLV),具体的详细介绍与使用,可以上github自行查看。
ffmpeg直接获取视频流,转换为图片发送给前端进行展示
用go语言实现,主要的难点就是cgo,需要通过cgo集成C的相关代码,获取视频流,然后转成图片即可。或者使用别人已经集成好的开源库goav进行相应的操作。
总结
本篇主要是讲了下web展示摄像头内容的几种实现方式,做个记录,也了解了一下直播相关的一些知识。通过开源库livego和flv.js搭建了一下直播客户端和服务器,使用OBS Studio进行视频推流,自己随便玩了一下,感兴趣的也可以自己尝试着搭建玩玩。