YouTube Optimizer with Bass Boost

Enhances your YouTube experience with optimized video playback, improved audio quality, bass boost, and enhanced buffering for maximum performance and enjoyment.

当前为 2025-07-04 提交的版本,查看 最新版本

// ==UserScript==
// @name         YouTube Optimizer with Bass Boost
// @namespace    https://greasyfork.org/en/users/1116584-simeonleni
// @description  Enhances your YouTube experience with optimized video playback, improved audio quality, bass boost, and enhanced buffering for maximum performance and enjoyment.
// @include      https://www.youtube.com/*
// @grant        none
// @run-at       document-end
// @version      2.1
// ==/UserScript==

// Configuration
const MAX_QUALITY = "hd2160"; // Maximum video quality to buffer (change as needed)
const BASS_BOOST_GAIN = 12; // Gain level for bass boost (in dB)

// Entry point
window.addEventListener("DOMContentLoaded", main);

function main() {
  const player = getPlayer();
  
  if (!player) {
    console.error("Player not found.");
    return;
  }

  // Check when the player is ready and set the quality and apply bass boost
  player.addEventListener('onStateChange', function(event) {
    if (event.data === 1) { // Player is playing
      updateSettings(player);
      applyBassBoost();
    }
  });
}

function updateSettings(player) {
  try {
    if (!isPlayerAvailable(player)) {
      throw new Error("YouTube player not available.");
    }

    // Setting maximum quality
    const availableQualities = player.getAvailableQualityLevels();
    if (!availableQualities.includes(MAX_QUALITY)) {
      console.warn(`Desired quality '${MAX_QUALITY}' not available, setting to highest available.`);
      player.setPlaybackQuality(availableQualities[0]);
    } else {
      player.setPlaybackQuality(MAX_QUALITY);
    }

  } catch (error) {
    console.error("An error occurred:", error.message);
  }
}

function applyBassBoost() {
  const audioContext = new (window.AudioContext || window.webkitAudioContext)();
  
  // Access the YouTube video player audio
  const player = getPlayer();
  const videoElement = player.querySelector("video");

  if (!videoElement) {
    console.error("Video element not found.");
    return;
  }

  // Create an audio source node from the video element
  const sourceNode = audioContext.createMediaElementSource(videoElement);

  // Create a bass boost filter
  const bassBoost = audioContext.createBiquadFilter();
  bassBoost.type = "lowshelf";
  bassBoost.frequency.value = 200; // Adjust frequency for bass
  bassBoost.gain.value = BASS_BOOST_GAIN; // Apply gain (bass boost)

  // Connect the source node to the bass boost filter, then to the destination (output)
  sourceNode.connect(bassBoost);
  bassBoost.connect(audioContext.destination);

  console.log("Bass boost applied.");
}

function isPlayerAvailable(player) {
  return player && player.getAvailableQualityLevels && player.getPlaybackQuality;
}

function getPlayer() {
  const player = document.getElementById("movie_player");
  return player;
}