baptx

webrtc_video_blob_record

Sep 9th, 2020 (edited)
1,115
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.     /* Setting high quality options since Firefox was recording in low quality:
  49.     https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/MediaRecorder
  50.     "If bits per second values are not specified for video and/or audio, the default adopted for video is 2.5Mbps or 10 Mbps, depending on the browser.
  51.     The audio default is adaptive, depending upon the sample rate and the number of channels." */
  52.     var options = {
  53.       audioBitsPerSecond: 128000,
  54.       videoBitsPerSecond: 10000000,
  55.       mimeType: "video/webm",
  56.     };
  57.  
  58.     try {
  59.         mediaRecorder = new MediaRecorder(stream, options);
  60.     } catch (e) {
  61.         console.error('Exception while creating MediaRecorder:', e);
  62.         return;
  63.     }
  64.    
  65.     mediaRecorder.onstop = (event) => {
  66.         console.log('Recorder stopped: ', event);
  67.         console.log('Recorded Blobs: ', recordedBlobs);
  68.     };
  69.     mediaRecorder.ondataavailable = handleDataAvailable;
  70.     mediaRecorder.start(1000); // collect 1000ms of data (to be able to download every second even if recording is not stopped)
  71.     console.log('MediaRecorder started', mediaRecorder);
  72. }
  73.  
  74. function stopRecording() {
  75.     mediaRecorder.stop();
  76. }
  77.  
  78. startRecording();
  79.  
Advertisement
Add Comment
Please, Sign In to add comment