🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2026/01/10 17:07:56
🔄 下次更新时间:2026/01/10 18:07:56
手动刷新缓存

YouTube Default Audio Track

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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==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);
    }
}