Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate video file from binary data collected from socket connection

i have create a socket connection and using that connection sending the binary stream data to server and on server side i am getting the binary data now using that data i want to create a video file and save it on server. i am reached successfully up to getting the binary data now not getting any way to convert it in a video file. please help to achieve .

on server side m using node.js to create socket server and from client side javascript

server side code :

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


    });

})

client side :

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);
         };
      }
    };
like image 221
Ashish Patel Avatar asked Nov 08 '22 05:11

Ashish Patel


1 Answers

i had achieved it successfully creating video file on server i had used recordRTC api to convert the video stream to dataurl and sending to server using socket.io and at server convert the dataurl to base64 and write it to the file.

here is my client side code.

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

and from server side:

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);
}
like image 158
Ashish Patel Avatar answered Nov 15 '22 10:11

Ashish Patel