使用mp4文件创建视频和音频

时间:2015-01-30 05:03:13

标签: javascript php xmlhttprequest

在此代码中,仅在chrome上创建视频,我想要带有一个mp4/webm文件的带有音频的视频。可以在PHP吗?

请给我有用的解决方案。

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

        document.querySelector('#start-recording').onclick = function() {
            this.disabled = true;
            navigator.getUserMedia(mediaConstraints, onMediaSuccess, onMediaError);
        };

        document.querySelector('#stop-recording').onclick = function() {
            this.disabled = true;
            mediaRecorder.stop();
        };

        var mediaRecorder;

        function onMediaSuccess(stream) {
            var video = document.createElement('video');

            var videoWidth = document.getElementById('video-width').value || 320;
            var videoHeight = document.getElementById('video-height').value || 240;

            video = mergeProps(video, {
                controls: true,
                id:"preview",
                width: videoWidth,
                height: videoHeight,
                src: URL.createObjectURL(stream)
            });
          video.play();

            //videot='2000';
            videosContainer.appendChild(video);
            videosContainer.appendChild(document.createElement('hr'));

            mediaRecorder = new MediaStreamRecorder(stream);
            mediaRecorder.mimeType = 'video/mp4'; // this line is mandatory
            mediaRecorder.videoWidth  = videoWidth;
            mediaRecorder.videoHeight = videoHeight;
            mediaRecorder.ondataavailable = function(blob) {

                //alert(blob.audio);
                //alert(blob.video);
             // var a = document.createElement('a');
              // a.target = '_blank';
              // a.innerHTML = 'Open Recorded Video No. ' + (index++) + ' (Size: ' + bytesToSize(blob.size) + ') Time Length: ' + getTimeLength(timeInterval);

              // a.href = URL.createObjectURL(blob);

              //videosContainer.appendChild(a);
               // videosContainer.appendChild(document.createElement('hr'));

        var facedeteval = document.getElementById('facedetectvalue').value;
        if(facedeteval !='1')
        {
        //alert(facedeteval);
        var testuserID = document.getElementById("tuid").value;
        var capId =document.getElementById("capimg").value;
        //alert(capId);
        var fileType = 'video'; // or "audio"
        var vname = document.getElementById('sect_id').value;
              var res = vname; 
        fileName =res;
        var fileName = res+(index++)+'.mp4';  // or "wav" or "ogg"

        var formData = new FormData();
        formData.append(fileType + '-filename', fileName);
        formData.append(fileType + '-blob', blob);
        formData.append('test_uid', testuserID);
        formData.append('cap_id', capId);





        function xhr(url, data, callback) {

            var request = new XMLHttpRequest();
            request.onreadystatechange = function () {
                if (request.readyState == 4 && request.status == 200) {
                    callback(location.href + request.responseText);
                }
            };
            request.open('POST', url);
            request.send(data);
        }
        xhr('save.php', formData, function (fileURL) {

            alert('video save');
            stopflag = '0';
             document.getElementById('facedetectvalue').value = '1';
        });


                       }
                        };


            var timeInterval = document.querySelector('#time-interval').value;
            if(timeInterval) timeInterval = parseInt(timeInterval);
            else timeInterval = 5 * 1000;

            // get blob after specific time interval
            mediaRecorder.start(timeInterval);

            document.querySelector('#stop-recording').disabled = false;
        }

        function onMediaError(e) {
            console.error('media error', e);
        }

        var videosContainer = document.getElementById('videos-container');
        var index = 1;

        // below function via: http://goo.gl/B3ae8c
        function bytesToSize(bytes) {
           var k = 1000;
           var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
           if (bytes === 0) return '0 Bytes';
           var i = parseInt(Math.floor(Math.log(bytes) / Math.log(k)),10);
           return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
        }

        // below function via: http://goo.gl/6QNDcI
        function getTimeLength(milliseconds) {
            var data = new Date(milliseconds);
            return data.getUTCHours()+" hours, "+data.getUTCMinutes()+" minutes and "+data.getUTCSeconds()+" second(s)";
        }

        window.onbeforeunload = function() {
            document.querySelector('#start-recording').disabled = false;
        };

0 个答案:

没有答案