Youtube Auto Stable Volume

Automatically enables Stable Volume (DRC) if it detects you are playing on some kind of speakers, disable it otherwise.

  1. // ==UserScript==
  2. // @name Youtube Auto Stable Volume
  3. // @icon https://www.youtube.com/img/favicon_48.png
  4. // @author ElectroKnight22
  5. // @namespace electroknight22_youtube_auto_stable_volume_namespace
  6. // @version 0.1.1
  7. // @match *://www.youtube.com/*
  8. // @match *://www.youtube-nocookie.com/*
  9. // @exclude *://www.youtube.com/live_chat*
  10. // @license MIT
  11. // @description Automatically enables Stable Volume (DRC) if it detects you are playing on some kind of speakers, disable it otherwise.
  12. // ==/UserScript==
  13.  
  14. /*jshint esversion: 11 */
  15.  
  16. (function () {
  17. "use strict";
  18.  
  19. let moviePlayer = null;
  20.  
  21. async function updateDrcStatus() {
  22. console.log('Updating DRC status...');
  23. try {
  24. if (!moviePlayer) throw "Movie player not found.";
  25. if (!moviePlayer.hasDrcAudioTrack()) throw "Video does not have DRC.";
  26. const currentAudioOutputDevice = await getCurrentAudioOutputDevice();
  27. const shouldEnableDrc = currentAudioOutputDevice && currentAudioOutputDevice.toLowerCase().includes("speaker");
  28. if (typeof shouldEnableDrc === "undefined") throw "Failed to determine audio device type.";
  29. moviePlayer.setDrcUserPreference(shouldEnableDrc ? 1 : 0);
  30. } catch (error) {
  31. console.error("Failed to update DRC status: " + error);
  32. }
  33. }
  34.  
  35. async function getCurrentAudioOutputDevice() {
  36. try {
  37. const audioDevices = await navigator.mediaDevices.enumerateDevices();
  38. const defaultOutputDevice = audioDevices.find(device =>
  39. device.kind === 'audiooutput' && device.deviceId === 'default'
  40. );
  41. return defaultOutputDevice?.label;
  42. } catch (error) {
  43. console.error('Error enumerating devices: ', error);
  44. return null;
  45. }
  46. }
  47.  
  48. function processVideoLoad(event = null) {
  49. moviePlayer = event?.target?.player_ ?? document.querySelector('#movie_player');
  50. updateDrcStatus();
  51. }
  52.  
  53. function initialize() {
  54. navigator.mediaDevices.addEventListener("devicechange", () => {
  55. console.log("Audio outputs changed. Checking if DRC state should be updated.");
  56. updateDrcStatus();
  57. }, true);
  58.  
  59. window.addEventListener('pageshow', processVideoLoad, true);
  60. if (window.self === window.top) {
  61. window.addEventListener('yt-player-updated', processVideoLoad, true);
  62. }
  63. }
  64.  
  65. initialize();
  66. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址