Skip to content

Instantly share code, notes, and snippets.

@technikhil314
Last active August 20, 2021 13:34
Show Gist options
  • Save technikhil314/971c2a99a801f0857243256b2b0672d0 to your computer and use it in GitHub Desktop.
Save technikhil314/971c2a99a801f0857243256b2b0672d0 to your computer and use it in GitHub Desktop.
Browser based audio output recorder

HOW TO USE

  1. copy and paste the code to browser console
  2. run record(); to record audio output
  3. run stop(); to stop recording
  4. run download() to download last recording
let recorder;
let recordedChunks = [];
function dataAvailable(event) {
if (event.data.size > 0) {
recordedChunks.push(event.data);
}
}
function resetState() {
recorder = null;
recordedChunks = [];
}
function download() {
let blob = new Blob(recordedChunks, {
type: "audio/aac",
});
let url = URL.createObjectURL(blob);
let a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = `${new Date().toISOString()}.webm`;
a.click();
window.URL.revokeObjectURL(url);
resetState();
}
async function record() {
let stream = await navigator.mediaDevices.getUserMedia({
audio: true,
video: true
});
stream.getVideoTracks().forEach((x) => x.stop());
stream.getAudioTracks().forEach((x) => x.stop());
const devices = await navigator.mediaDevices.enumerateDevices();
console.log(devices);
const firstAudioOutput = devices.find((device) => {
return device.kind === "audiooutput"
});
stream = await navigator.mediaDevices.getUserMedia({
audio: {
deviceId: {
exact: firstAudioOutput.deviceId
}
}
});
recorder = new MediaRecorder(stream);
recorder.start();
recorder.addEventListener("stop", download);
recorder.addEventListener("dataavailable", dataAvailable);
}
function stop() {
recorder.stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment