YouTube Music Opus Codec

Reloads songs on YouTube Music using the regular YouTube video backend to force playback with the Opus audio codec, since desktop YouTube Music seems to be unable to play in Opus codec always defaults to AAC. This also bypasses YouTube Music's quality restriction for non-premium users.

目前為 2025-04-08 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name                YouTube Music Opus Codec
// @icon                https://www.youtube.com/img/favicon_48.png
// @author              ElectroKnight22
// @namespace           electroknight22_youtube_music_opus_codec_namespace
// @version             1.0.0
// @match               *://music.youtube.com/*
// @grant               none
// @license             MIT
// @description         Reloads songs on YouTube Music using the regular YouTube video backend to force playback with the Opus audio codec, since desktop YouTube Music seems to be unable to play in Opus codec always defaults to AAC. This also bypasses YouTube Music's quality restriction for non-premium users.
// ==/UserScript==

/*jshint esversion: 11 */

(function () {
    "use strict";

    let moviePlayer = null;
    let musicPlayer = null;
    let videoElement = null;
    let currentTime = 0;

    function handleSongLoad() {
        try {
            const videoId = moviePlayer.getVideoData().video_id;
            moviePlayer.loadVideoById({
                videoId: videoId,
                startSeconds: currentTime
            });
        } catch (error) {
            console.error("Failed to handle song load. Error: ", error);
        }
    }

    // currently no additional behaviour other than to load the song
    // may change in the future
    function handleMusicPlayerModeChange() {
        try {
            handleSongLoad();
        } catch (error) {
            console.error("Failed to handle music player mode change. Error: ", error);
        }
    }

    function observeSongTitleChange() {
        try {
            const titleElement = document.querySelector('.title.ytmusic-player-bar');
            if (!titleElement) throw new Error("Title element not found.");
            let currentTitle = titleElement.textContent.trim();
            handleSongLoad();
            const observer = new MutationObserver(() => {
                const newTitle = titleElement.textContent.trim();
                if (newTitle && newTitle !== currentTitle) {
                    currentTitle = newTitle;
                    currentTime = 0;
                    handleSongLoad();
                }
            });

            observer.observe(titleElement, {
                childList: true,
                subtree: true,
            });
        } catch (error) {
            throw new Error("Failed to handle song title change. Error: " + error);
        }
    }

    // playback mode can be in "OMV_PREFERRED" for video and '"ATV_PREFERRED" for audio only
    function observeMusicPlayerModeChange() {
        try {
            if (!musicPlayer) throw new Error("Music player not found.");
            const observer = new MutationObserver(() => {
                handleMusicPlayerModeChange();
            });
            observer.observe(musicPlayer, {
                attributes: true,
                attributeFilter: ['playback-mode']
            });
        } catch (error) {
            throw new Error("Failed to handle music player mode change. Error: " + error);
        }
    }

    function initialize() {
        try {
            moviePlayer = document.querySelector('#movie_player');
            musicPlayer = document.querySelector('ytmusic-player');
            videoElement = musicPlayer.querySelector('video');
            musicPlayer.addEventListener('timeupdate', () => {
                currentTime = videoElement?.currentTime ?? 0;
            }, true);
            observeSongTitleChange();
            observeMusicPlayerModeChange();
        } catch (error) {
            console.error(`Error when initializing script: ${error}. Aborting script.`);
        }
    }

    window.addEventListener('pageshow', initialize, { once: true });
})();