Skip to content

Instantly share code, notes, and snippets.

@baptx
Last active July 19, 2026 23:10
Show Gist options
  • Select an option

  • Save baptx/8a1549d91996a37378faca159b3adc17 to your computer and use it in GitHub Desktop.

Select an option

Save baptx/8a1549d91996a37378faca159b3adc17 to your computer and use it in GitHub Desktop.
WebRTC and video blob record (compatible with DRM and any videos like Facebook / Instagram stories)
/* 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)
* 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()
* 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
* By default, the script targets the first video element document.getElementsByTagName("video")[0] but you can change the code if needed.
*/
// If there is an error due to DRM, capture the stream by executing the following 2 lines before playing / loading the video:
var video = document.getElementsByTagName("video")[0];
var stream = video.captureStream ? video.captureStream() : video.mozCaptureStream();
var mediaSource = new MediaSource();
mediaSource.addEventListener('sourceopen', handleSourceOpen, false);
var mediaRecorder;
var recordedBlobs;
var sourceBuffer;
function startDownload() {
var blob = new Blob(recordedBlobs, {type: 'video/webm'});
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'test.webm';
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
}
function handleSourceOpen(event) {
console.log('MediaSource opened');
sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vp8"');
console.log('Source buffer: ', sourceBuffer);
}
function handleDataAvailable(event) {
console.log('handleDataAvailable', event);
if (event.data && event.data.size > 0) {
recordedBlobs.push(event.data);
}
}
function startRecording() {
recordedBlobs = [];
/* Setting high quality options since Firefox was recording in low quality:
https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/MediaRecorder
"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.
The audio default is adaptive, depending upon the sample rate and the number of channels." */
var options = {
audioBitsPerSecond: 128000,
videoBitsPerSecond: 10000000,
mimeType: "video/webm",
};
try {
mediaRecorder = new MediaRecorder(stream, options);
} catch (e) {
console.error('Exception while creating MediaRecorder:', e);
return;
}
mediaRecorder.onstop = (event) => {
console.log('Recorder stopped: ', event);
console.log('Recorded Blobs: ', recordedBlobs);
};
mediaRecorder.ondataavailable = handleDataAvailable;
mediaRecorder.start(1000); // collect 1000ms of data (to be able to download every second even if recording is not stopped)
console.log('MediaRecorder started', mediaRecorder);
}
function stopRecording() {
mediaRecorder.stop();
}
startRecording();
@baptx

baptx commented Mar 7, 2024

Copy link
Copy Markdown
Author

@dreamflasher Do you mean no bytes are coming in Chrome or Chromium? Did you try executing my script without modifying it?
If you removed the DRM capture restriction from Chromium source code, you don't need to execute the 2 lines of code mentioned previously separately, you can execute the script as a whole when the video is playing.
I never tried compiling Chromium but I will give it a try when I have more time.
To enable DRM in Chromium, you may also need to install Widevine:
https://github.com/proprietary/chromium-widevine

@dreamflasher

dreamflasher commented Mar 7, 2024

Copy link
Copy Markdown

@baptx yes, I tried your script unmodified and that doesn't work. No bytes are coming in.

@baptx

baptx commented Apr 14, 2024

Copy link
Copy Markdown
Author

@dreamflasher To get the best video resolution with Firefox, a solution for me on Facebook stories was to detach the Developer Tools window in a separate window. If necessary, replay the video to get the best resolution.
When I tested playing a video (without recording it) with Chromium on https://integration.widevine.com/player, it behaved like Firefox, by default it may not get the best resolution and this may change based on your Internet connection speed. Some players like the one from the Widevine website allow to select the resolution instead of the automatic selection.

@DeadFrostt

Copy link
Copy Markdown

Hey! Is this script still working? I cant get it to work. Is there a guide or a readme I can refer to?

@baptx

baptx commented Mar 2, 2025

Copy link
Copy Markdown
Author

@DeadFrostt Hi, yes it is still working, I tested it with latest Firefox 135.0.1 64-bit Linux version from Mozilla deb repository. Instructions are at the beginning of the script and in the comment https://gist.github.com/baptx/8a1549d91996a37378faca159b3adc17?permalink_comment_id=4726941#gistcomment-4726941.

@lthrhx

lthrhx commented May 14, 2025

Copy link
Copy Markdown

Still working with Firefox 138.0.1, however the script doesn't honor a different codec. When changing
sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vp8"');
to
sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vp9"');
The file downloaded is still encoded as vp8. I also tried avc1 as that is the source codec, but it still results in vp8 encoded file.

Also Changing the filename from test.webm to download.webm works, but changing the video container and extension from webm to mp4 doesn't . When you are prompted to the save file it is always ".webm"

@baptx

baptx commented Jul 19, 2026

Copy link
Copy Markdown
Author

@dreamflasher I updated the script by configuring the video bitrate to 10 Mbps like documented on https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/MediaRecorder.
This allows recording in higher quality. I tested with Firefox 152 for the FIFA world cup since there was an ad I wanted to record.

@lthrhx If you want to try recording as mp4, you also need to configure it in the options parameter of MediaRecorder (which was not used in the previous versions of my script).
But I was not able to create a working mp4 recording with Firefox. And the webm recording is always in vp8. It seems to be a web browser limitation but recording in webm vp8 should be fine now that I updated the script to use a higher bitrate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment