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 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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 });
})();