YouTube Prevent Auto-Dub

Forces all YouTube videos to play in their original language audio instead of the AI dubbed language. Works in both normal videos and shorts. Can be manually overridden per-video.

// ==UserScript==
// @name                            YouTube Prevent Auto-Dub
// @name:zh-TW                      YouTube 防止自動配音
// @name:zh-CN                      YouTube 防止自动配音
// @name:ja                         YouTube 自動吹き替え防止
// @icon                            https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @author                          ElectroKnight22
// @namespace                       electroknight22_prevent_auto_dub_namespace
// @version                         0.2.2
// @match                           *://www.youtube.com/*
// @match                           *://m.youtube.com/*
// @match                           *://www.youtube-nocookie.com/*
// @exclude                         *://www.youtube.com/live_chat*
// @require                         https://update.greasyfork.org/scripts/549881/1688974/YouTube%20Helper%20API.js
// @run-at                          document-start
// @grant                           none
// @inject-into                     page
// @license                         MIT
// @description                     Forces all YouTube videos to play in their original language audio instead of the AI dubbed language. Works in both normal videos and shorts. Can be manually overridden per-video.
// @description:zh-TW               強制所有 YouTube 影片以原始語言音訊播放,而不是 AI 配音的語言。適用於一般影片和 Shorts。可針對單一影片手動關閉。
// @description:zh-CN               强制所有 YouTube 视频以原始语言音频播放,而不是 AI 配音的语言。适用于一般视频和 Shorts。可针对单个视频手动关闭。
// @description:ja                  すべてのYouTube動画をAI吹き替えではなく、元の言語の音声で強制的に再生します。通常の動画とショート動画の両方で機能します。動画ごとに手動で無効にすることもできます。
// ==/UserScript==

/*jshint esversion: 11 */

(function () {
    'use strict';

    function main(event) {
        try {
            const playingLanguage = event.detail.video.playingLanguage;
            const originalLanguage = event.detail.video.originalLanguage;
            const isAutoDubbed = playingLanguage !== originalLanguage && `${playingLanguage}` !== 'Default';
            if (!isAutoDubbed) return;
            if (!originalLanguage) throw new Error('Unable to determine the original audio track.');
            console.log('Auto-dub detected, trying to undo...');
            window.youtubeHelperApi.player.playerObject.setAudioTrack(originalLanguage);
            console.log(`Auto-dub undo successful. Audio track reverted from ${playingLanguage} to ${originalLanguage}.`);
        } catch (error) {
            console.warn('Failed to prevent YouTube auto-dubbing.', error);
        }
    }

    window.youtubeHelperApi.eventTarget.addEventListener('yt-helper-api-ready', main);
})();