Reputation: 250
I would like to give access to user to record a video from application and user can upload that after preview. After some try and I am able to record and download a video. But I am not sure how can I upload that on server?
I have referred this code - link
HTML -
<div id='gUMArea'>
<div class="videoRadio">
<input type="radio" name="media" value="video" checked id='mediaVideo'>Video
<input type="radio" name="media" value="audio">audio
</div>
<button class="btn btn-default" id='gUMbtn'>Request Stream</button>
</div>
<div id='btns'>
<button class="btn btn-default" id='start'>Start</button>
<button class="btn btn-default" id='stop'>Stop</button>
</div>
<!-- <video id="video" width="640" height="480" autoplay></video> -->
<form id="video">
@csrf
<input type="file" name="testVideo">
<input type="file" name="video" id="uploadVideo" value="">
<div>
<ul class="list-unstyled" id='ul'></ul>
</div>
<div class="flex-btn">
<a href="/list-your-product?step=2&uniqueId={{$data[0]->uniqueId}}" class="btn btn-success w-100">Back</a>
<button class="btn btn-success w-100" type="submit" id="front-dropzone-btn">Upload</button>
</div>
</form>
My js code -
'use strict'
// var video = document.getElementById('video');
// // Get access to the camera!
// if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
// // Not adding `{ audio: true }` since we only want video now
// navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
// //video.src = window.URL.createObjectURL(stream);
// video.srcObject = stream;
// video.play();
// });
// }
let log = console.log.bind(console),
id = val => document.getElementById(val),
ul = id('ul'),
gUMbtn = id('gUMbtn'),
start = id('start'),
stop = id('stop'),
uploadVideo = id('uploadVideo'),
stream,
recorder,
counter=1,
chunks,
media;
gUMbtn.onclick = e => {
let mv = id('mediaVideo'),
mediaOptions = {
video: {
tag: 'video',
type: 'video/webm',
ext: '.mp4',
gUM: {video: true, audio: true}
},
audio: {
tag: 'audio',
type: 'audio/ogg',
ext: '.ogg',
gUM: {audio: true}
}
};
media = mv.checked ? mediaOptions.video : mediaOptions.audio;
navigator.mediaDevices.getUserMedia(media.gUM).then(_stream => {
stream = _stream;
// video.srcObject = stream;
// video.play();
id('gUMArea').style.display = 'none';
id('btns').style.display = 'inherit';
start.removeAttribute('disabled');
recorder = new MediaRecorder(stream);
recorder.ondataavailable = e => {
chunks.push(e.data);
if(recorder.state == 'inactive') makeLink();
};
log('got media successfully');
}).catch(log);
}
start.onclick = e => {
start.disabled = true;
stop.removeAttribute('disabled');
chunks=[];
recorder.start();
}
stop.onclick = e => {
stop.disabled = true;
recorder.stop();
start.removeAttribute('disabled');
}
function makeLink(){
let blob = new Blob(chunks, {type: media.type })
, url = URL.createObjectURL(blob)
, li = document.createElement('li')
, mt = document.createElement(media.tag)
, hf = document.createElement('a')
;
mt.controls = true;
mt.src = url;
hf.href = url;
hf.download = `${counter++}${media.ext}`;
hf.innerHTML = `donwload ${hf.download}`;
li.appendChild(mt);
li.appendChild(hf);
ul.appendChild(li);
console.log(hf.download);
console.log(uploadVideo.value);
uploadVideo.value = hf.download;
}
It will great if someone can help me to understand how to upload on server.
Also, by using this js code user can not see himself/herself while recording. How can I activate that?
Upvotes: 2
Views: 2866
Reputation: 12391
as per your reference website
your are getting blob data of video steam so you can send that to controller via ajax
Route::get('/', function () {
return view('welcome');
});
Route::post('/save', function (Request $request) {
$path = \Storage::disk('public')->put('videos',$request->video);
$url = \Storage::disk('public')->url($path);
return $url;
});
<!DOCTYPE html>
<html>
<head>
<title>MediaRecorder API - Sample</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="WebRTC getUserMedia MediaRecorder API">
<link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<meta name="csrf-token" content="{{ csrf_token() }}">
<style>
button{
margin: 10px 5px;
}
li{
margin: 10px;
}
body{
width: 90%;
max-width: 960px;
margin: 0px auto;
}
#btns{
display: none;
}
h1{
margin: 100px;
}
</style>
</head>
<body>
<h1> MediaRecorder API example</h1>
<p> For now it is supported only in Firefox(v25+) and Chrome(v47+)</p>
<div id='gUMArea'>
<div>
Record:
<input type="radio" name="media" value="video" checked id='mediaVideo'>Video
<input type="radio" name="media" value="audio">audio
</div>
<button class="btn btn-default" id='gUMbtn'>Request Stream</button>
</div>
<div id='btns'>
<button class="btn btn-default" id='start'>Start</button>
<button class="btn btn-default" id='stop'>Stop</button>
</div>
<div>
<ul class="list-unstyled" id='ul'></ul>
</div>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="/video.js"></script>
</body>
</html>
'use strict'
let log = console.log.bind(console),
id = val => document.getElementById(val),
ul = id('ul'),
gUMbtn = id('gUMbtn'),
start = id('start'),
stop = id('stop'),
stream,
recorder,
counter=1,
chunks,
media;
gUMbtn.onclick = e => {
let mv = id('mediaVideo'),
mediaOptions = {
video: {
tag: 'video',
type: 'video/webm',
ext: '.mp4',
gUM: {video: true, audio: true}
},
audio: {
tag: 'audio',
type: 'audio/ogg',
ext: '.ogg',
gUM: {audio: true}
}
};
media = mv.checked ? mediaOptions.video : mediaOptions.audio;
navigator.mediaDevices.getUserMedia(media.gUM).then(_stream => {
stream = _stream;
id('gUMArea').style.display = 'none';
id('btns').style.display = 'inherit';
start.removeAttribute('disabled');
recorder = new MediaRecorder(stream);
recorder.ondataavailable = e => {
chunks.push(e.data);
if(recorder.state == 'inactive') makeLink();
};
log('got media successfully');
}).catch(log);
}
start.onclick = e => {
start.disabled = true;
stop.removeAttribute('disabled');
chunks=[];
recorder.start();
}
stop.onclick = e => {
stop.disabled = true;
recorder.stop();
start.removeAttribute('disabled');
}
function makeLink(){
let blob = new Blob(chunks, {type: media.type })
, url = URL.createObjectURL(blob)
, li = document.createElement('li')
, mt = document.createElement(media.tag)
, hf = document.createElement('a')
;
mt.controls = true;
mt.src = url;
hf.href = url;
hf.download = `${counter++}${media.ext}`;
hf.innerHTML = `donwload ${hf.download}`;
li.appendChild(mt);
li.appendChild(hf);
ul.appendChild(li);
const formData = new FormData();
formData.append('_token', $('meta[name="csrf-token"]').attr('content'));
formData.append('video', blob);
fetch('/save', {
method: 'POST',
body: formData
})
.then(response => {
console.log(response);
})
.catch(error => {});
}
Here i sending blob data to server and store in storage
Upvotes: 2