停用 YouTube AV1

停用 YouTube 的 AV1 视频播放

目前为 2023-08-19 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Disable YouTube AV1
  3. // @description Disable AV1 for video playback on YouTube
  4. // @name:zh-TW 停用 YouTube AV1
  5. // @description:zh-TW 停用 YouTube 的 AV1 影片播放
  6. // @name:zh-HK 停用 YouTube AV1
  7. // @description:zh-HK 停用 YouTube 的 AV1 影片播放
  8. // @name:zh-CN 停用 YouTube AV1
  9. // @description:zh-CN 停用 YouTube 的 AV1 视频播放
  10. // @name:ja YouTube AV1 停用
  11. // @description:ja YouTube の動画再生に AV1 を停用する
  12. // @name:ko YouTube AV1 비활성화
  13. // @description:ko YouTube의 동영상 재생에 AV1을 비활성화하기
  14. // @name:vi Vô hiệu hóa YouTube AV1
  15. // @description:vi Vô hiệu hóa AV1 để phát video trên YouTube
  16. // @name:de YouTube AV1 deaktivieren
  17. // @description:de Deaktiviert AV1 für die Videowiedergabe auf YouTube
  18. // @name:fr Désactiver YouTube AV1
  19. // @description:fr Désactivez AV1 pour la lecture des vidéos sur YouTube
  20. // @name:it Disabilita YouTube AV1
  21. // @description:it Disabilita AV1 per la riproduzione dei video su YouTube
  22. // @name:es Desactivar AV1 en YouTube
  23. // @description:es Desactivar AV1 para la reproducción de videos en YouTube
  24. // @namespace http://tampermonkey.net/
  25. // @version 2.4.0
  26. // @author CY Fung
  27. // @match https://www.youtube.com/*
  28. // @match https://www.youtube.com/embed/*
  29. // @match https://www.youtube-nocookie.com/embed/*
  30. // @exclude https://www.youtube.com/live_chat*
  31. // @exclude https://www.youtube.com/live_chat_replay*
  32. // @exclude /^https?://\S+\.(txt|png|jpg|jpeg|gif|xml|svg|manifest|log|ini)[^\/]*$/
  33. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  34. // @grant none
  35. // @run-at document-start
  36. // @license MIT
  37. // @compatible chrome
  38. // @compatible firefox
  39. // @compatible opera
  40. // @compatible edge
  41. // @compatible safari
  42. // @unwrap
  43. // @allFrames true
  44. // @inject-into page
  45. // ==/UserScript==
  46.  
  47. (function () {
  48. 'use strict';
  49.  
  50. console.debug("disable-youtube-av1", "injected");
  51.  
  52.  
  53.  
  54. const flagConfig = () => {
  55.  
  56. let firstDa = true;
  57. let cid = 0;
  58. const { setInterval, clearInterval, setTimeout } = window;
  59. const tn = () => {
  60.  
  61. const da = (window.ytcfg && window.ytcfg.data_) ? window.ytcfg.data_ : null;
  62. if (!da) return;
  63.  
  64. const isFirstDa = firstDa;
  65. firstDa = false;
  66.  
  67. for (const EXPERIMENT_FLAGS of [da.EXPERIMENT_FLAGS, da.EXPERIMENTS_FORCED_FLAGS]) {
  68.  
  69. if (EXPERIMENT_FLAGS) {
  70. EXPERIMENT_FLAGS.html5_disable_av1_hdr = true;
  71. EXPERIMENT_FLAGS.html5_prefer_hbr_vp9_over_av1 = true;
  72. }
  73.  
  74. }
  75.  
  76. if (isFirstDa) {
  77.  
  78.  
  79. let mo = new MutationObserver(() => {
  80.  
  81. mo.disconnect();
  82. mo.takeRecords();
  83. mo = null;
  84. setTimeout(() => {
  85. cid && clearInterval.call(window, cid);
  86. cid = 0;
  87. tn();
  88. })
  89. });
  90. mo.observe(document, { subtree: true, childList: true });
  91.  
  92.  
  93. }
  94.  
  95.  
  96. };
  97. cid = setInterval.call(window, tn);
  98.  
  99. };
  100.  
  101. const supportedFormatsConfig = () => {
  102.  
  103. function typeTest(type) {
  104.  
  105. if (typeof type === 'string' && type.startsWith('video/')) {
  106. if (type.includes('av01')) {
  107. if (/codecs[^\w]+av01\b/.test(type)) return false;
  108. } else if (type.includes('av1')) {
  109. if (/codecs[^\w]+av1\b/.test(type)) return false;
  110. }
  111. }
  112.  
  113. }
  114.  
  115. // return a custom MIME type checker that can defer to the original function
  116. function makeModifiedTypeChecker(origChecker) {
  117. // Check if a video type is allowed
  118. return function (type) {
  119. let res = undefined;
  120. if (type === undefined) res = false;
  121. else {
  122. res = typeTest(type);
  123. }
  124. if (res === undefined) res = origChecker.apply(this, arguments);
  125.  
  126. // console.debug(20, type, res)
  127.  
  128. return res;
  129. };
  130. }
  131.  
  132. // Override video element canPlayType() function
  133. const proto = (HTMLVideoElement || 0).prototype;
  134. if (proto && typeof proto.canPlayType == 'function') {
  135. proto.canPlayType = makeModifiedTypeChecker(proto.canPlayType);
  136. }
  137.  
  138. // Override media source extension isTypeSupported() function
  139. const mse = window.MediaSource;
  140. // Check for MSE support before use
  141. if (mse && typeof mse.isTypeSupported == 'function') {
  142. mse.isTypeSupported = makeModifiedTypeChecker(mse.isTypeSupported);
  143. }
  144.  
  145. };
  146.  
  147. function disableAV1() {
  148.  
  149.  
  150.  
  151.  
  152. // This is the setting to disable AV1
  153. // localStorage['yt-player-av1-pref'] = '-1';
  154. try {
  155. Object.defineProperty(localStorage.constructor.prototype, 'yt-player-av1-pref', {
  156. get() {
  157. if (this === localStorage) return '-1';
  158. return this.getItem('yt-player-av1-pref');
  159. },
  160. set(nv) {
  161. this.setItem('yt-player-av1-pref', nv);
  162. return true;
  163. },
  164. enumerable: true,
  165. configurable: true
  166. });
  167. } catch (e) {
  168. // localStorage['yt-player-av1-pref'] = '-1';
  169. }
  170.  
  171. if (localStorage['yt-player-av1-pref'] !== '-1') {
  172.  
  173. console.warn('Disable YouTube AV1', '"yt-player-av1-pref = -1" is not supported in your browser.');
  174. return;
  175. }
  176.  
  177. console.debug("disable-youtube-av1", "AV1 disabled by yt-player-av1-pref = -1");
  178.  
  179.  
  180. }
  181.  
  182.  
  183. disableAV1();
  184.  
  185. flagConfig();
  186. supportedFormatsConfig();
  187.  
  188.  
  189.  
  190.  
  191. })();