javascript – 从套接字连接收集的二进制数据生成视频文件
作者:互联网
我已经创建了一个套接字连接,并使用该连接将二进制流数据发送到服务器,在服务器端,我现在正在使用该数据获取二进制数据,我想创建一个视频文件并将其保存在服务器上.我成功地获得二进制数据现在没有任何方式将其转换为视频文件.请帮助实现.
在服务器端m使用node.js创建套接字服务器并从客户端javascript
服务器端代码:
var server = http.createServer(function(request, response) {
//Creating websocket server
});
server.listen(1337, function() { }); // listen to 1337 port
// create the server
wsServer = new WebSocketServer({
httpServer: server
});
// WebSocket server
wsServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
// all messages from client will receive here.
connection.on('message', function(message) {
if (message.type === 'utf8') {
}else if (message.type === 'binary') {
//here i will get the binary data not want to create the video file using this
}
});
connection.on('close', function(connection) {
});
})
客户端 :
window.WebSocket = window.WebSocket || window.MozWebSocket;
var connection = new WebSocket('ws://localhost:1337');
connection.binaryType = 'arraybuffer';
var options = {
mimeType: 'video/webm;codecs=vp9'
};
mediaRecorder = new MediaRecorder(MediaStream, options);
mediaRecorder.ondataavailable = function(event) {
if (event.data.size > 0) {
recordedChunks.push(event.data);
connection.onopen = function () {
var byteArray = new Uint8Array(event.data);
connection.send(byteArray.buffer);
};
}
};
解决方法:
我已经成功地在服务器上创建了视频文件我已经使用recordRTC api将视频流转换为dataurl并使用socket.io发送到服务器,并在服务器上将dataurl转换为base64并将其写入文件.
这是我的客户端代码.
socketio = io("ws://192.168.0.42:1337");
recordVideo = RecordRTC(MediaStream, {type: 'video'});
recordVideo.startRecording();
onStopRecording();
function onStopRecording(){
recordVideo.stopRecording(function() {
recordVideo.getDataURL(function(videoDataURL) {
var d = new Date();
var files = {
video: {
type: recordVideo.getBlob().type || 'video/webm',
dataURL: videoDataURL,
time : d.getTime()
}
};
socketio.emit('message', files);
})
});
}
从服务器端:
var server = http.createServer(function(request, response) {
//Creating websocket server
});
server.listen(1337, function() { }); // listen to 1337 port
var wIoSocket = io.listen(server);
wIoSocket.sockets.on('connection', function (socket) {
socket.on('message', function (data) {
if (data.video) {
writeToDisk(data.video.dataURL, data.video.time+'.webm');
}
});
});
function writeToDisk(dataURL, fileName) {
var dataURL = dataURL.split(',').pop();
var fileBuffer = new Buffer(dataURL, 'base64');
fs.writeFileSync(fileName, fileBuffer);
}
标签:javascript,node-js,sockets,video-capture 来源: https://codeday.me/bug/20190706/1393158.html