Advertisement
baptx

webrtc_video_blob_record

Sep 9th, 2020 (edited)
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Customized code to allow WebRTC record on existing page or HTML5 video blob download (based on https://github.com/webrtc/samples/blob/409d5631f38f2bdc4dafb5275d1bc77738fbb1ba/src/content/getusermedia/record/js/main.js)
  2.  * Recording will start when executing this script and you will have to execute manually startDownload() function when needed in web console with or without stopRecording()
  3.  * Recording should ideally start before playing video manually and in case controls are hidden, you can record from the start with a command like document.getElementsByTagName("video")[0].currentTime = 0
  4.  * By default, the script targets the first video element document.getElementsByTagName("video")[0] but you can change the code if needed.
  5.  */
  6.  
  7. // If there is an error due to DRM, capture the stream by executing the following 2 lines before playing / loading the video:
  8. var video = document.getElementsByTagName("video")[0];
  9. var stream = video.captureStream ? video.captureStream() : video.mozCaptureStream();
  10.  
  11. var mediaSource = new MediaSource();
  12. mediaSource.addEventListener('sourceopen', handleSourceOpen, false);
  13. var mediaRecorder;
  14. var recordedBlobs;
  15. var sourceBuffer;
  16.  
  17. function startDownload() {
  18.     var blob = new Blob(recordedBlobs, {type: 'video/webm'});
  19.     var url = window.URL.createObjectURL(blob);
  20.     var a = document.createElement('a');
  21.     a.style.display = 'none';
  22.     a.href = url;
  23.     a.download = 'test.webm';
  24.     document.body.appendChild(a);
  25.     a.click();
  26.     setTimeout(() => {
  27.         document.body.removeChild(a);
  28.         window.URL.revokeObjectURL(url);
  29.     }, 100);
  30. }
  31.  
  32. function handleSourceOpen(event) {
  33.     console.log('MediaSource opened');
  34.     sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vp8"');
  35.     console.log('Source buffer: ', sourceBuffer);
  36. }
  37.  
  38. function handleDataAvailable(event) {
  39.     console.log('handleDataAvailable', event);
  40.     if (event.data && event.data.size > 0) {
  41.         recordedBlobs.push(event.data);
  42.     }
  43. }
  44.  
  45. function startRecording() {
  46.     recordedBlobs = [];
  47.  
  48.     try {
  49.         mediaRecorder = new MediaRecorder(stream);
  50.     } catch (e) {
  51.         console.error('Exception while creating MediaRecorder:', e);
  52.         return;
  53.     }
  54.    
  55.     mediaRecorder.onstop = (event) => {
  56.         console.log('Recorder stopped: ', event);
  57.         console.log('Recorded Blobs: ', recordedBlobs);
  58.     };
  59.     mediaRecorder.ondataavailable = handleDataAvailable;
  60.     mediaRecorder.start(1000); // collect 1000ms of data (to be able to download every second even if recording is not stopped)
  61.     console.log('MediaRecorder started', mediaRecorder);
  62. }
  63.  
  64. function stopRecording() {
  65.     mediaRecorder.stop();
  66. }
  67.  
  68. startRecording();
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement