我们如何使用javascript和php将blob-url保存到我的服务器中的(.webm)视频中



我使用RecordRTC进行屏幕录制,

这是我的代码,

<html>
<head>
<style>
html, body {
margin: 0!important;
padding: 0!important;
text-align: center;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 1em;
}
video {
width: 30%;
border-radius: 5px;
border: 1px solid black;
}
</style>
<title>Screen Recording</title>
</head>
<body>
<h1>Screen Recording</h1>
<br>
<button id="btn-start-recording">Start Recording</button>
<button id="btn-stop-recording" disabled>Stop Recording</button>
<hr>
<video controls autoplay playsinline></video>
</body>
<script src="RecordRTC.js"></script>
<script>
var video = document.querySelector('video');
if(!navigator.getDisplayMedia && !navigator.mediaDevices.getDisplayMedia) {
var error = 'Your browser does NOT support the getDisplayMedia API.';
document.querySelector('h1').innerHTML = error;
document.querySelector('video').style.display = 'none';
document.getElementById('btn-start-recording').style.display = 'none';
document.getElementById('btn-stop-recording').style.display = 'none';
throw new Error(error);
}
//alert("this is test");
function invokeGetDisplayMedia(success, error) {
var displaymediastreamconstraints = {
video: {
displaySurface: 'monitor', // monitor, window, application, browser
logicalSurface: true,
cursor: 'always' // never, always, motion
}
};
// above constraints are NOT supported YET
// that's why overriding them
displaymediastreamconstraints = {
video: true
};
if(navigator.mediaDevices.getDisplayMedia) {
navigator.mediaDevices.getDisplayMedia(displaymediastreamconstraints).then(success).catch(error);
}
else {
navigator.getDisplayMedia(displaymediastreamconstraints).then(success).catch(error);
}
}
function captureScreen(callback) {      
invokeGetDisplayMedia(function(screen) {
addStreamStopListener(screen, function() {
document.getElementById('btn-stop-recording').click();

});
callback(screen);
}, function(error) {
console.error(error);
alert('Unable to capture your screen. Please check console logs.n' + error);
});
}
function stopRecordingCallback() {
video.src = video.srcObject = null;
video.src = URL.createObjectURL(recorder.getBlob());
alert(video.src);
recorder.screen.stop();
recorder.destroy();
recorder = null;
document.getElementById('btn-start-recording').disabled = false;
}
var recorder; // globally accessible
document.getElementById('btn-start-recording').onclick = function() {
this.disabled = true;
captureScreen(function(screen) {

video.srcObject = screen;
recorder = RecordRTC(screen, {
type: 'video'
});
recorder.startRecording();
// release screen on stopRecording
recorder.screen = screen;
document.getElementById('btn-stop-recording').disabled = false;
});
};
document.getElementById('btn-stop-recording').onclick = function() {
this.disabled = true;
recorder.stopRecording(stopRecordingCallback);

};
function addStreamStopListener(stream, callback) {
stream.addEventListener('ended', function() {
callback();
callback = function() {};
}, false);
stream.addEventListener('inactive', function() {
callback();
callback = function() {};
}, false);
stream.getTracks().forEach(function(track) {
track.addEventListener('ended', function() {
callback();
callback = function() {};
}, false);
track.addEventListener('inactive', function() {
callback();
callback = function() {};
}, false);
});
}
</script>
<br><br>
<footer style="margin-top: 20px; text-align: left;">
<!-- <p style="padding: 5px 10px;"><b>Hints:</b> Are you using Chrome version 71? Please enable "chrome://flags/#enable-experimental-web-platform-features"</p>
<p style="padding: 5px 10px;"><b>Browser support:</b> Firefox, Edge and Chrome &gt= 71</p>
<div><small id="send-message"></small></div> -->
</footer> 
<!--<script src="https://www.webrtc-experiment.com/common.js"></script> -->

这很好,意味着我可以在点击start recording按钮时录制。之后,如果我点击stop recording,我可以在<video controls autoplay playsinline></video>上看到视频(我录制过的视频(。如果我想下载,那么<video></video>中有一个下载按钮,然后我可以下载到这里,它很好(这是我在服务器中做的(。

但在这里,当我单击录制视频的stop recording按钮时,我需要以MP4的形式自动下载到我的服务器文件夹(recordedFiles(中。

我试过的是,有一个函数叫stopRecordingCallback(),在这个函数中,video.src生成一个像blob:https://server_ip/f4d27a7d-48f1-4499-9613-7953ef01edfe一样的video blob url。这个url我需要转换到base64,然后再次保存为(.webm(MP4

这是我在JS中尝试过的代码:

let blob = new Blob(["blob:http://localhost/166d7686-4500-4a4a-993f-3b743d3afcb4"], { type: "video/webm" }); 
// The full Blob Object can be seen  
// in the Console of the Browser 
console.log('Blob - ', blob); 

var reader = new FileReader(); 
reader.readAsDataURL(blob); 
reader.onloadend = function () { 
var base64String = reader.result; 
console.log('Base64 String - ', base64String); 

// Simply Print the Base64 Encoded String, 
// without additional data: Attributes. 
console.log('Base64 String without Tags- ',base64String.substr(base64String.indexOf(', ') + 1)); 
}

这根本不起作用。

如何使用javascript或PHP使用blob url将录制的文件保存到服务器文件夹中。

请帮忙。

感谢

我建议对PHP脚本发出XML POST请求,并使用视频"文件对象";而不是blob元素。

let request = new XMLHttpRequest();
let url = "your form treatment file path here" ;
var video = your video file object; // i don't know output gived by recorder but if you can have blob you sure can access the raw js file object 
var formData = new FormData();
formData.append("upload_video", file);

request.open("POST", url, true);
request.onload = function () {
saveResponse = request.responseText;
var result = document.getElementById('result'); // add an element with an id to display your PHP script return (to debug or display something once the video is uploaded) 
result.innerHTML = saveResponse;

}
request.send(data);

然后你可以用PHP$_FILES['upload_video']访问你的文件,并用它做你想做的事。

在recordRTC中使用mimeType: 'video/webm; codecs=vp9',它将在Google chrome、Mozilla Firefox和Microsoft Edge中工作。

const recorder = RecordRTC(stream, {
type: 'video', 
// audio/webm
// video/webm;codecs=vp9
// video/webm;codecs=vp8
// video/webm;codecs=h264
// video/x-matroska;codecs=avc1
// video/mpeg -- NOT supported by any browser, yet
// video/mp4  -- NOT supported by any browser, yet
// audio/wav
// audio/ogg  -- ONLY Firefox
// demo: simple-demos/isTypeSupported.html
mimeType: 'video/webm;codecs=vp9'
})

最新更新