YouTube Optimizer

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

Author
simeonleni
Daily installs
3
Total installs
719
Ratings
3 0 0
Version
1.0
Created
2023-07-01
Updated
2023-07-01
License
N/A
Applies to

Description:

YouTube Optimizer is a user script that enhances your YouTube experience with optimized video playback, improved audio quality, bass boost, and enhanced buffering for maximum performance and enjoyment.

Features:

  • Optimized video playback for smoother and higher quality viewing.
  • Improved audio quality for a better listening experience.
  • Bass boost effect to enhance the audio with deeper and richer bass tones.
  • Enhanced buffering to reduce interruptions and provide a seamless streaming experience.

Instructions:

  1. Install a user script manager extension for your browser (e.g., Tampermonkey, Greasemonkey).
  2. Install this script by copying the code below and creating a new script in your user script manager:

(function() {
  // Paste the entire code below into your user script manager
  // Example: Tampermonkey

  // ==UserScript==
  // @name         YouTube Optimizer
  // @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      1.0
  // ==/UserScript==

  // Configuration
  const MAX_QUALITY = "2160p"; // Maximum video quality to buffer (change as needed)
  const BASS_BOOST_LEVEL = 3; // Bass boost level (adjust as needed)

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

  function main() {
    const player = getPlayer();
    player.addEventListener("onStateChange", function (event) {
      if (event.data === 1) {
        updateSettings(player);
      }
    });
  }

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

      const currentQuality = player.getPlaybackQuality();
      const currentAudioQuality = player.getPlaybackRate();

      if (currentQuality !== MAX_QUALITY || currentAudioQuality === null) {
        const availableQualities = player.getAvailableQualityLevels();
        const targetQualityIndex = availableQualities.indexOf(MAX_QUALITY);
        if (targetQualityIndex !== -1) {
          const targetQuality = availableQualities[targetQualityIndex];
          player.setPlaybackQuality(targetQuality);
        } else {
          throw new Error(`Target quality '${MAX_QUALITY}' not available.`);
        }
      }

      if (currentAudioQuality === null) {
        const availableAudioQualities = player.getAvailablePlaybackRates();
        const targetAudioQualityIndex = findBestAudioQualityIndex(availableAudioQualities);
        if (targetAudioQualityIndex !== -1) {
          const targetAudioQuality = availableAudioQualities[targetAudioQualityIndex];
          player.setPlaybackRate(targetAudioQuality);
        } else {
          throw new Error("No available audio qualities found.");
        }
      }

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

  function isPlayerAvailable(player) {
    return (
      player &&
      /https:\/\/www\.youtube\.com\/watch\?v=.*/.test(window.location.href) &&
      document.getElementById("live-chat-iframe") === null
    );
  }

  function findBestAudioQualityIndex(availableAudioQualities) {
    // Find the index of the highest available audio quality
    let maxQualityIndex = -1;
    let maxQuality = -Infinity;
    availableAudioQualities.forEach((quality, index) => {
      const qualityNumber = Number(quality);
      if (!isNaN(qualityNumber) && qualityNumber > maxQuality) {
        maxQuality = qualityNumber;
        maxQualityIndex = index;
      }
    });
    return maxQualityIndex;
  }

  function setBassBoost(player, level) {
    const bassBoostEffect = {
      name: "bassBoost",
      parameters: {
        gain: level,
      },
    };
    player.applyFilter(bassBoostEffect);
  }

  function getPlayer() {
    const player = document.getElementById("movie_player");
    if (!player) {
      throw new Error("YouTube player not found.");
    }
    return player;
  }

})();

Compatibility:

This script is compatible with the latest version of the YouTube website and its Player API. Please note that updates to the YouTube website may affect the script's functionality. In case of any issues, please check for script updates or report them on the GreasyFork page.

Author: Simeon Tuyoleni

Version: 1.0