Skip to content

Instantly share code, notes, and snippets.

@abec2304
Last active March 3, 2024 01:26
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save abec2304/2782f4fc47f9d010dfaab00f25e69c8a to your computer and use it in GitHub Desktop.
Save abec2304/2782f4fc47f9d010dfaab00f25e69c8a to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name No YouTube Volume Normalization
// @namespace https://gist.github.com/abec2304
// @match https://www.youtube.com/*
// @grant none
// @version 2.1
// @author abec2304
// @description Enjoy YouTube videos at their true volume
// @inject-into content
// @run-at document-start
// @allFrames true
// ==/UserScript==
(function(recurse) {
// try in case script ran later than document-start
if(!recurse())
return;
// fallback to waiting for video element to be added
const videoObserver = new MutationObserver(function(records) {
records.forEach(function(mutation) {
Array.prototype.forEach.call(mutation.addedNodes, function(node) {
if("VIDEO" === node.tagName) {
videoObserver.disconnect();
recurse();
}
});
});
})
// begin waiting
videoObserver.observe(document.documentElement, {
subtree: true,
childList: true
});
})(function() {
// get volume bar element
const volumeElement = document.querySelector(".ytp-volume-panel");
// get video element
const videoElement = document.querySelector(".html5-main-video");
// if elements weren't found, don't continue
if(!volumeElement || !videoElement)
return 1;
// get volume set function and bind to video element
const setVolume = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "volume").set.bind(videoElement);
// define convenience function
function ytSetVolume(widget) {
const newVolume = widget.getAttribute("aria-valuenow") / 100;
setVolume(newVolume);
}
// determine index of video element for non-sandboxed world
const globalIndex = [].slice.call(document.getElementsByTagName("video")).indexOf(videoElement);
// define function for shadowing the volume field
const volumeShadow = function(targetIndex) {
const targetVideo = document.getElementsByTagName("video")[targetIndex];
Object.defineProperty(targetVideo, "volume", {
value: 42
});
}
// inject shadowing script (this breaks the volume bar; fixed below)
const contextScript = document.createElement("script");
contextScript.id = "ytvolfix2";
contextScript.textContent = "(" + volumeShadow+ ")(" + globalIndex + ")";
contextScript.onload = function() {
// remove script after execution
this.parentElement.removeChild(this);
};
(document.head || document.documentElement).appendChild(contextScript);
// define observer for volume bar change
const volumeObserver = new MutationObserver(function(records) {
ytSetVolume(records[0].target);
});
// register observer to make volume bar function
volumeObserver.observe(volumeElement, {
attributes: true,
attributeFilter: ["aria-valuenow"]
});
// set volume to current value of bar
ytSetVolume(volumeElement);
});
@V-Coba
Copy link

V-Coba commented Mar 5, 2023

...or perhaps the version of Mac OS you use !!!

I'm using the most recent version of macOS called Ventura which is 13.2.1 (22D68).
Снимок экрана 2023-03-05 в 02 32 13

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

Okay, stop offtopic here 😀

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

Can't help you I'm afraid because as said I don't have any issues with scrolling be it in Chrome or across the entire Mac system and as also said with no need to install/ add extra drivers or apps just to achieve something as simple as smooth scrolling...

It looks like you have some kind of special Mac))) macOS does not support smooth scrolling when you connect a regular mouse with a wheel to it. That is why there are such utilities as "SmoothScroll", "MOS", "Mac Mouse Fix" and others.

Here is a video in which the author of the "SmoothScroll" utility talks about this: https://www.youtube.com/watch?v=tz_FT-aDNKk

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

This is what scrolling looks like on a Mac when you connect a regular mouse with a wheel to it: https://www.youtube.com/watch?v=-3aF-4p_vKo

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

That is, when you connect a regular mouse to the Mac, we get stepped scrolling, not smooth.

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

It looks like your Mac has some utility or driver installed that makes scrolling smooth. You just don't know about it 😉

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

LOL!!! 🤣 🤣 🤣 And you are funny 😂

Fix MacOS's Weird Scrolling with a Non-Apple Mouse! — starting at 2:53 you can see what scrolling looks like in macOS by default.

Mos APP smooth scrolling showcase on MacOS — how content scrolling looks like with the default mouse in macOS is shown at the very beginning of the video.

Everyone doesn't get any sort of smooth scrolling when connecting a regular mouse to a Mac, but you do. Yes, you are just a magician, not otherwise! 🧙‍♂️

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

Fix MacOS's Weird Scrolling with a Non-Apple Mouse! — starting at 2:53 you can see what scrolling looks like in macOS by default.

From the video description:
«A brief tutorial on how to fix the scrolling issue on your Mac. If you're a convert from Windows to Mac and you DON'T use Apple's own peripherals, you might find the scrolling experience to be a little.... bad. But have no fear! Go to the link below to download Discrete Scroll - a great little background utility that disables scroll acceleration and gives you back control of your scrolling momentum!»

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

Mos APP smooth scrolling showcase on MacOS — how content scrolling looks like with the default mouse in macOS is shown at the very beginning of the video.

From the video description:
«Mos is a lightweight but powerfull tool for mouse on MacOS. It aims to optimize your mouse scrolling experience from providing the smooth scrolling, it also can set the scroll direction of mouse independently.»

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

Videos on YouTube prove that I am right and you are wrong (^_~) There is nothing further to discuss here ┐( ̄ヮ ̄)┌

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

Another video proving that there is no smooth scrolling in macOS when using regular mice: «Mac Smooth Scrolling»

However, this is quite logical. Apple must somehow sell its Magic Mouse and Magic Trackpad ^^

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

This is what scrolling looks like on a Mac when you connect a regular mouse with a wheel to it: https://www.youtube.com/watch?v=-3aF-4p_vKo

From the video description:
«It works inside all apps on your Mac. This example show scrolling with my mouse wheel inside Spotify with and without SmoothScroll. Feel the difference! Happy scrolling :)»

I confirm! Scrolling by default comes with noticeable jerks (not smooth), plus there is this absolutely idiotic and unnecessary acceleration. Thank God there is a Mac Mouse Fix utility! 🙏
Perhaps Apple decided to remove smooth scrolling for normal mice starting with version 11 (Big Sur), just like they removed sub-pixel anti-aliasing for text throughout the system 🤔

@hakimzulkufli
Copy link

hakimzulkufli commented Mar 5, 2023

Remember that this script is to disable normalization (so the loudness will be at it's actual levels) and not boost levels. You need to use a different extension for that.

If you're not sure whether this script would work on a video or not, simply look at the loudness to normalized ratio.

The video you linked has 100% to 100% loudness to normalized ratio, which means it's not getting normalized by YouTube. Therefore the script won't make a difference.

The script will only work for audio that has been normalized to be lower than it's actual loudness. For example, a video with 100%/37% ratio will work.

cc: @V-Coba @Eisys

@Izofeu
Copy link

Izofeu commented Mar 5, 2023

Shut the F up, you spammed my email with offtopic garbage you pieces of sht vcoba and madfranko.

@Izofeu
Copy link

Izofeu commented Mar 5, 2023

Jak próbujesz obrazić mnie po niemiecku to coś ci nie wyszło 🤣

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

Remember that this script is to disable normalization (so the loudness will be at it's actual levels) and not boost levels. You need to use a different extension for that.

If you're not sure whether this script would work on a video or not, simply look at the loudness to normalized ratio.

The video you linked has 100% to 100% loudness to normalized ratio, which means it's not getting normalized by YouTube. Therefore the script won't make a difference.

The script will only work for audio that has been normalized to be lower than it's actual loudness. For example, a video with 100%/37% ratio will work.

cc: @V-Coba @Eisys

Yes, I already figured it out. Thanks.

Shut the F up, you spammed my email with offtopic garbage you pieces of sht vcoba and madfranko.

LOL!!! 😄
Yeah, something we went too far with offtopic.

@V-Coba
Copy link

V-Coba commented Mar 5, 2023

I don't want to continue offtopic, but I'll post the LAST video, clearly proving that macOS does not support smooth scrolling when a regular mouse with a wheel is connected to the Mac. Here is the video itself («you can admire» jerks while scrolling starting from the 15th second): «Smooth scrolling with Microsoft mouse on Mac OS»
Moreover, it is clearly visible that in the video the person is running macOS and the Chrome web browser is open. And there are many such videos on YouTube.

From the video description:
«The Mos utility smoothes out jumpy scrolling for 3rd party mice on Mac OS. It also lets you turn off (un)natural scrolling direction for the mouse while leaving it on for Trackpad. Per application control as well.»

Moreover, the video was posted on YouTube on February 14, 2019. That is, about 4 years ago!

If you read the comments below the video, you will notice that other Mac users also do not have any smooth scrolling after connecting a third-party mouse to the Mac.
«Not only MS Mice, all Mice behave like this under macOS except the Apple Mice.
I wish something liek this would be available for Windows either»

«I dont understand why Apple doesn't try to make the scrolling on other mice work. very annoying. its like if it is not Apple, dont use it kind of attitude»

«I don't generally comment on videos. But this video saved me from buying a new mouse since I was thinking that my mouse is not compatible.»

«OMG! I've been looking for something like this from months! Thanks a lot dude for helping me out, I've changed and replaced 3 third party mousses thinking being faulty or not properly compatible with MacBook. Thanks from the heart, Have a great day!»

«I was so annoyed with the Mac scrolling. Thank you so much for sharing!»

«Thaks so much man! I was thinking that this was the problem of my mouse.Finally after 1 year I got my smooth scrolling in my mac!»

«Thanks a lot, dude. I was thinking this was trouble in my mouse.»

«holy crap, i was looking for a mouse that have smooth scrolling.. but found this! amazing! thx.»

«Thanks for the video dude! I finally set my MacBook up in closed-lid mode with a bigger external monitor and keyboard, but the though of having to buy an Apple mouse to fix this has been bugging me for a while now!»

«Thanks, perfect smooth scrolling experience!»

«Thank you so much! I have a microsoft mouse I disassembled several times thinking of an hardware issue, thanks to you, now it's working perfectly.»

«I really don't understand why on macs third party mouse are note well optimised. Scrolling is not really smooth, it is step by step. I have recently bought the m1 mac air and my mouse was struggling at scrolling smoothly. In my old Windows laptop intel 8th gen i3 scrolling was sooo smoth with the same mouse trackpad. Really don't understand why...»

Who are these people and why do they all complain about the lack of smooth scrolling when connecting a third-party mouse to a Mac? Losers whose arms do not grow from their shoulders? I don't think so ^^ It looks like only one person has smooth scrolling — MadFranko008. So I have no idea why it works for him, and everyone else does not, but I have given evidence for my words, in my opinion, already enough 😎

If macOS had no problems with smooth scrolling when connecting regular mice to a Mac, then there would not be so many various utilities that implement smooth scrolling. It simply wouldn't be necessary. But in this case, we see that demand creates supply.

P. S. Maybe for MadFranko008 smooth scrolling, is it step by step scrolling? 🤔 In general, I don’t know why this person is so unique… 🤭

@V-Coba
Copy link

V-Coba commented Mar 7, 2023

The script will only work for audio that has been normalized to be lower than it's actual loudness. For example, a video with 100%/37% ratio will work.

Yes, indeed, on these videos, the volume with this script increases dramatically:
[Special Clip] Dreamcatcher(드림캐쳐) 지유 '사건의 지평선' Cover
[MV] LOONA/ODD EYE CIRCLE "LOONATIC (Official Lyric Video)”
[ENG/Portuguese SUB] [팩트M/V] 이달의 소녀 오드아이서클 (LOONA ODD EYE CIRCLE) - Sweet Crazy Love [60f]

Thanks!
👍

@V-Coba
Copy link

V-Coba commented Mar 7, 2023

I wonder if this forced volume normalization on YouTube somehow affects the quality of the sound itself or not? 🤔 Or just the volume becomes quieter and that's it? Does anyone know?

@hakimzulkufli
Copy link

@V-Coba There's no quality difference. It's only perceived loudness. According to human ears, louder is better, until your ears bleed that is. However, normalization usually is more than just lowering the volume. Think of it like a compressor. The audio is compressed so that it's not too quiet and not too loud, while also maintaining the same loudness levels throughout the audio file.

@V-Coba
Copy link

V-Coba commented Mar 11, 2023

I wouldn't take the word of a person who claims that macOS supports smooth scrolling when connecting third-party mice to the Mac, and who has not provided any proof of his words.

@abec2304
Copy link
Author

Some notes:

This should go without saying but please be kind to each other.

The initial version of my script would fail in certain circumstances, as mentioned in an earlier comment. The current version should work fine and today I added a line to allow it to run under frames, i.e. embedded player instances.

The method that h264ify uses to "disable" volume normalization is reactively changing the player volume, which may cause a blip of lowered volume when YouTube attempts to engage normalization. My script instead hides volume control from YouTube's code, hence the additional code to restore the functionality of the volume bar.

My script is fairly minimal. It doesn't provide an option to disable itself, nor does it modify the text displayed under "Stats for nerds". So it's always enabled.

I believe DRC could be disabled with a userscript (or extension) - last time I saw DRC, YouTube was still returning regular audio stream URLs alongside those for DRC; it's just a matter of making the player use them. This is beyond the scope of my script.

As other commenters gathered, some YouTube videos simply have low volume and disabling normalization won't make a difference. I would like to make a script to address this, but it would be a separate script.

@LordKobra
Copy link

Thanks for the script! I was running Firefox with PulseAudio and noticed how the Volume Tab in PulseAudio always lowered to 66% when skipping through the video. Because my volume is always set to 100% and controlled by external DAC, it was quite annoying. Neither PulseAudio nor Firefox provided adequate solutions to this (e.g. blocking the control), in fac this bug is open for 5 years: https://bugzilla.mozilla.org/show_bug.cgi?id=1422637
So this script really saves the day!

@paul5917
Copy link

paul5917 commented May 29, 2023

https://gist.github.com/fa7ad/fa995474f5cb9fe91fb209686881373d
I used the script from this link and yours at the same time, and everything sounds at the right volume.
Automatically, even without pressing a button!

@V-Coba
Copy link

V-Coba commented May 29, 2023

This script alone is enough to make everything work automatically 😉

@AshleighTheCutie
Copy link

FINALLY AFTER YEARS OF SEARCHING AFTER IRIDIUM NUKED ITSELF, MY MUSIC SOUNDS GOOD AGAIN

@Eisys
Copy link

Eisys commented Oct 26, 2023

@AshleighTheCutie Check out Nova Youtube. https://github.com/raingart/Nova-YouTube-extension

It includes the function of this script and many many more things, similar to Iridium.

@antoniu200
Copy link

antoniu200 commented Nov 17, 2023

Ahoy, guys!
DRC is back! It is present on YouTube for Android as "Stable volume" and it is optional.
I am curious whether or not this is present on PC browsers and whether it is optional there as well.
Screenshot_20231117-162753_YouTube~2

EDIT 2: It seems like DRC is not present at all on music, as far as I can tell (for now):
Screenshot_20231117-171429_YouTube~2
Screenshot_20231117-171445_YouTube~2

EDIT 3: I don't see any option on PC to enable it. YouTube links also does not identify more than 2 audio streams (AAC and Opus).

@epicfacethe3rd
Copy link

this script isn't working for me, any advice on how to fix this?
thanks

@Comitant
Copy link

Comitant commented Mar 1, 2024

Is there a script out there that removes DRC? This script is no longer effective for removing normalization, at least for me since everything is DRC normalized.

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