Skip to content

Instantly share code, notes, and snippets.

@fa7ad
Forked from WouterG/tampermonkey-script.js
Last active April 24, 2024 11:04
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fa7ad/fa995474f5cb9fe91fb209686881373d to your computer and use it in GitHub Desktop.
Save fa7ad/fa995474f5cb9fe91fb209686881373d to your computer and use it in GitHub Desktop.
Disable youtube volume normalization (allow true video 100% volume)
// ==UserScript==
// @name YouTube Disable Normalization
// @namespace https://gist.github.com/fa7ad/fa995474f5cb9fe91fb209686881373d
// @version 0.2
// @description Allows true 100% volume on youtube videos.
// @author Wouter Gerarts
// @match https://www.youtube.com/*
// @match https://youtube.com/*
// @grant none
// ==/UserScript==
(function () {
"use strict";
function baseElement() {
return document.querySelector("#content");
}
if (typeof fullVolumeButtonTaskId === "number") {
console.log("clearing interval");
clearInterval(fullVolumeButtonTaskId);
}
function maxVol() {
var videos = document.querySelectorAll("video");
videos.forEach(function (video) {
video.volume = 1;
console.log(video, video.volume);
});
}
function createFullVolumeButton() {
var css = `
.full-volume-addon-button {
margin: 0 0.5em;
padding: 0.25em 1em;
background: transparent;
color: #fff;
border: 1px solid #fff;
border-radius: 1em;
font: caption;
}
.full-volume-addon-button:hover {
cursor: pointer;
background: #fff;
color: #000;
}
`;
var style = document.createElement("style");
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
document.querySelector("head").appendChild(style);
var el = document.createElement("button");
el.textContent = "100% Volume";
el.classList.add("full-volume-addon-button");
el.onclick = function (e) {
e.preventDefault();
maxVol();
};
return el;
}
function round(num, sig) {
var mult = Math.pow(10, sig);
return Math.round(num * mult) / mult;
}
var fullVolumeButtonTaskId = setInterval(function () {
if (baseElement().querySelector("video") === undefined) {
console.log("video element not found");
return;
}
var volTimer = setInterval(function () {
if (
[].every.call(document.querySelectorAll("video"), function (vid) {
return vid.volume === 1;
})
) {
console.log("vol maxed out");
clearInterval(volTimer);
return;
} else {
maxVol();
}
}, 500);
if (baseElement().querySelector(".full-volume-addon-button") != undefined) {
console.log("full volume addon button already found");
clearInterval(fullVolumeButtonTaskId);
clearInterval(volTimer);
return;
}
var video = baseElement().querySelector("video");
var videoTitleElement = baseElement().querySelector("#title h1");
videoTitleElement.appendChild(createFullVolumeButton());
}, 500);
})();
@mara004
Copy link

mara004 commented Mar 7, 2024

IIRC, enhanced-h264ify used to have successful prevention of normalization, but it stopped working at some point.

cf. https://github.com/alextrv/enhanced-h264ify/blob/e5f2ea22378e85fc55b9e6ccab27644b1bc66ba5/src/inject/inject.js#L79-L104
(and alextrv/enhanced-h264ify#1 (comment))

@mara004
Copy link

mara004 commented Mar 7, 2024

Whoops, I think I'll have to eat my own words. It seems like enhanced-h264ify actually still does work after disabling and re-enabling the checkbox as suggested in alextrv/enhanced-h264ify#7. Crazy.

FWIW, when lowering volume in the mixer, pausing and re-starting, then it stays. But when moving to a new video, it jumps back to 100%.

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