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

南笙酒味 提交于 2019-12-21 05:44:11

问题


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


来源:https://stackoverflow.com/questions/36245494/how-do-i-upload-audiovideo-as-one-to-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!