-
Notifications
You must be signed in to change notification settings - Fork 1
/
youtubeLoudnessCorrection.user.js
66 lines (61 loc) · 1.88 KB
/
youtubeLoudnessCorrection.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// ==UserScript==
// @name Youtube Loudness Correction
// @description Amplifies any youtube video with loudness lower than 0dB
// @namespace Septolum
// @include https://www.youtube.com/*
// @include https://m.youtube.com/*
// @icon https://www.youtube.com/favicon.ico
// @version 1.2
// @grant none
// @run-at idle
// ==/UserScript==
// I created this script because Youtube does not amplify quiet videos, it only quietens loud ones
// from: https://stackoverflow.com/a/18997637
setInterval(function () {
if (this.lastHrefStr !== location.href || this.lastHrefStr === null) {
this.lastHrefStr = location.href;
console.log("page change");
gmMain();
}
}, 111);
function gmMain() {
"use strict";
var req = new XMLHttpRequest();
req.open(
"GET",
"https://" +
window.location.host +
"/watch?v=" +
/v=(.+?)(?:(?:&.*?)|$)/.exec(window.location.href)[1],
false
);
req.send(null);
if (req.status == 200) {
var loudness = parseFloat(
/"loudnessDb":([-0-9.]+),/.exec(req.responseText)[1]
);
}
if (loudness < 0) {
console.log("Loudness Corrected");
loudness = 10 ** ((loudness * -1) / 20);
// from https://stackoverflow.com/questions/43794356/html5-volume-increase-past-100#comment99251398_43794379
if (window["_gainNode"]) {
window["_gainNode"].gain.value = loudness;
return;
}
var v = document.querySelector("video");
var audioCtx = new AudioContext();
var source = audioCtx.createMediaElementSource(v);
var gainNode = audioCtx.createGain();
gainNode.gain.value = loudness;
source.connect(gainNode);
gainNode.connect(audioCtx.destination);
window["_gainNode"] = gainNode;
} else {
if (window["_gainNode"]) {
console.log("Loudness Reset");
window["_gainNode"].gain.value = 1;
return;
}
}
}