我想流raspivid的輸出到具有nodejs的Web應用程序。問題是我流式傳輸的數據無法顯示。這是節點服務器的代碼:流raspivid輸出與nodejs到瀏覽器
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const spawn = require('child_process').spawn;
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
const raspivid = spawn(
'raspivid',
['-t', '0', '-w', '300', '-h', '300', '-hf', '-fps', '20', '-o', '-']);
raspivid.stdout.on('data', (data) => {
var base64Image = data.toString('base64');
io.emit('videostream', base64Image);
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
對於Web應用我嘗試了許多事情,我想在一個視頻標籤,以顯示在圖像標籤中的流以及,所以使用以下中的一個標籤:
<video id="video" width="400" height="300"></video>
<img id="img" src="">
我試着顯示流嘗試以下操作:
var socket = io(),
video = document.getElementById('video'),
img = document.getElementById('img'),
vendorUrl = window.URL || window.webkitURL;
socket.on('videostream', function(stream) {
var contentType = 'image/png';
img.src = ' data:image/png;base64,' + stream;//op1 doesn't work
var blob = b64toBlob(stream, contentType);
img.src = vendorUrl.createObjectURL(blob);//op2 doesn't work
video.src = vendorUrl.createObjectURL(blob);//op3 doesn't work
video.play();
});
誰能告訴我怎麼可以在瀏覽器中顯示流或點我在正確的方向?在此先感謝