Send MediaRecorder blobs to server and build file on backend

前端 未结 3 779
悲哀的现实
悲哀的现实 2021-02-04 15:08

I\'m working on a website using nodejs and SailsJs.

My objective is send the blobs generated by MediaRecorder.ondataavailable event (which returns small blobs) to the se

3条回答
  •  旧时难觅i
    2021-02-04 15:36

    This is my solution if it helps anybody:

    I send chuncks in binary format (I have selected Uint8Array), and add each chunk to a file in the server side after packing the received data (converted to binary in the same unsigned char decoding)

    Client Side Javascript:

    let order=0;
    mediaRecorder.ondataavailable = async (e) => {
        if(e.data && e.data.size > 0) {         
            var reader = new FileReader();
            reader.readAsArrayBuffer(e.data); 
            reader.onloadend = async function(event) {
                let arrayBuffer = reader.result;   
                let uint8View = new Uint8Array(arrayBuffer);
                let response = await fetch('save-video.php', {
                    method: 'POST',                 
                    body: JSON.stringify({
                        chunk: uint8View,
                        order: order
                    })                  
                });
                order += 1;
            }
    
        }       
    }
    

    Server Side PHP:

    
    

提交回复
热议问题