Skip to content

Instantly share code, notes, and snippets.

@djshaji
Last active February 13, 2024 07:52
Show Gist options
  • Save djshaji/8b3e7caa032536b90c7add149f316899 to your computer and use it in GitHub Desktop.
Save djshaji/8b3e7caa032536b90c7add149f316899 to your computer and use it in GitHub Desktop.
Automatically add dynamic compressor to all video elements on page
compressor = null
videos = document.getElementsByTagName ("video")
for (audioElt of videos) {
audioCtx = new AudioContext();
// Create a MediaElementAudioSourceNode
// Feed the HTMLMediaElement into it
const source = new MediaElementAudioSourceNode(audioCtx, {
mediaElement: audioElt,
});
// Create a compressor node
compressor = new DynamicsCompressorNode(audioCtx, {
threshold: -50,
knee: 40,
ratio: 12,
attack: 0,
release: 0.25,
});
// connect the AudioBufferSourceNode to the destination
source.connect(audioCtx.destination);
source.connect(compressor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment