YouTube Default Audio Track

Makes it possible to select the desired Audio Track played by default.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Default Audio Track
// @namespace    bp-yt-audio-track-default
// @version      1.4
// @description  Makes it possible to select the desired Audio Track played by default.
// @author       BuIlDaLiBlE
// @match        https://www.youtube.com/*
// @match        https://www.youtube-nocookie.com/*
// @icon         https://www.youtube.com/favicon.ico
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==
"use strict";

const DESIRED_AUDIO_TRACK = "original"; // The desired audio track name (can be partial)

window.addEventListener("yt-navigate-finish", main, true);
const observer = new MutationObserver(
    (mutations, shortsReady = false, videoPlayerReady = false) =>
    {
        outer: for(const mutation of mutations)
        {
            for(const node of mutation.addedNodes)
            {
                if(!shortsReady)
                {
                    shortsReady = node.tagName === "YTD-SHORTS";
                }
                if(!videoPlayerReady)
                {
                    videoPlayerReady = typeof node.className === "string" && node.className.includes("html5-main-video");
                }
                if(shortsReady || videoPlayerReady)
                {
                    observer.disconnect();
                    main();
                    break outer;
                }
            }
        }
    }
);
observer.observe(document.documentElement,
{
    childList: true,
    subtree: true,
});

async function main()
{
    let player = getPlayer();
    while(!player)
    {
        player = getPlayer();
        await new Promise(resolve => setTimeout(resolve, 5));
    }
    forceAudioTrack(player);
}

function getPlayer()
{
    let player;
    if(window.location.href.includes("youtube.com/shorts"))
    {
        player = document.querySelector("#shorts-player");
    }
    else
    {
        player = document.querySelector("#movie_player");
    }
    return player;
}

function forceAudioTrack(player)
{
    try
    {
        const availableAudioTracks = player.getAvailableAudioTracks();
        const currentAudioTrack = player.getAudioTrack();
        let languageObject;

        if(availableAudioTracks === undefined || availableAudioTracks.length == 0)
        {
            return;
        }

        for(let object in currentAudioTrack)
        {
            if(currentAudioTrack[object].name)
            {
                languageObject = object;
                break;
            }
        }

        if(!currentAudioTrack[languageObject].name.toLowerCase().includes(DESIRED_AUDIO_TRACK.toLowerCase()))
        {
            for(const track of availableAudioTracks)
            {
                if(track[languageObject].name.toLowerCase().includes(DESIRED_AUDIO_TRACK.toLowerCase()))
                {
                    player.setAudioTrack(track);
                    break;
                }
            }
        }
    }
    catch(error)
    {
        console.error("Error setting Audio Track:", error.message);
    }
}