How do I upload Audio+video as one to server?

2019-03-31 14:40发布

问题:

How to upload RecordRTC based audio+video recordings to server in a single file format?

RecordRTC seems generating two separate files: one for audio (as WAV) and another one for video (as WebM). How to upload both files simultaneously on a PHP server?

回答1:

Since Chrome >=49, RecordRTC is now using MediaRecorder API to support microphone+webcam recordings into single WebM file format.

Here is how to record both audio+video into single WebM (this works both on Chrome >= 49 and Firefox >= 38)

<script src="https://cdn.WebRTC-Experiment.com/gumadapter.js"></scrip>
<script>
var recorder;

function successCallback(audioVideoStream) {
    recorder = RecordRTC(audioVideoStream, {
        type: 'video',
        mimeType: 'video/webm',
        bitsPerSecond: 512 * 8 * 1024
    });
    recorder.startRecording();
}

function errorCallback(error) {
    // maybe another application is using the device
}

var mediaConstraints = {
    video: true,
    audio: true
};

navigator.mediaDevices.getUserMedia(mediaConstraints).then(successCallback).catch(errorCallback);

document.querySelector('#btn-stop-recording').onclick = function() {
    if (!recorder) return;
    recorder.stopRecording(function() {
        var audioVideoBlob = recorder.blob;

        // you can upload Blob to PHP/ASPNET server
        uploadBlob(audioVideoBlob);

        // you can even upload DataURL
        recorder.getDataURL(function(dataURL) {
            $.ajax({
                type: 'POST',
                url: '/save.php',
                data: {
                    dataURL: dataURL
                },
                contentType: 'application/json; charset=utf-8',
                success: function(msg) {
                    alert('Successfully uploaded.');
                },
                error: function(jqXHR, textStatus, errorMessage) {
                    alert('Error:' + JSON.stringify(errorMessage));
                }
            });
        });
    });
};

function uploadBlob(blob) {
    var formData = new FormData();
    formData.append('file', blob);
    $.ajax({
        url: "/save.php",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false,
        success: function(response) {
            alert('Successfully uploaded.');
        },
        error: function(jqXHR, textStatus, errorMessage) {
            alert('Error:' + JSON.stringify(errorMessage));
        }
    });
}
</script>

Here is an example save.php file that reads for video-filename and video-blob:

<?php
foreach(array('video', 'audio') as $type) {
    if (isset($_FILES["${type}-blob"])) {

        echo 'uploads/';

        $fileName = $_POST["${type}-filename"];
        $uploadDirectory = 'uploads/'.$fileName;

        if (!move_uploaded_file($_FILES["${type}-blob"]["tmp_name"], $uploadDirectory)) {
            echo(" problem moving uploaded file");
        }

        echo($fileName);
    }
}
?>

Above save.php requires following FormData:

function uploadBlob(blob) {
    var formData = new FormData();
    formData.append('video-blob', blob);
    formData.append('video-filename', 'FileName.webm');
    $.ajax({
        url: "/save.php",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false,
        success: function(response) {
            alert('Successfully uploaded.');
        },
        error: function(jqXHR, textStatus, errorMessage) {
            alert('Error:' + JSON.stringify(errorMessage));
        }
    });
}


标签: recordrtc