Disable YouTube AV1

Disable AV1 for video playback on YouTube

  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.3
  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[\x20-\x7F]+\bav01\b/.test(type)) return false;
  108. } else if (type.includes('av1')) {
  109. if (/codecs[\x20-\x7F]+\bav1\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, dx) {
  117. // Check if a video type is allowed
  118. return function (type) {
  119. let res = undefined;
  120. if (type === undefined) res = false;
  121. else res = typeTest(type);
  122. if (res === undefined) res = origChecker.apply(this, arguments);
  123. else res = !dx ? res : (res ? "probably" : "");
  124.  
  125. // console.debug(20, type, res)
  126.  
  127. return res;
  128. };
  129. }
  130.  
  131. // Override video element canPlayType() function
  132. const proto = (HTMLVideoElement || 0).prototype;
  133. if (proto && typeof proto.canPlayType == 'function') {
  134. proto.canPlayType = makeModifiedTypeChecker(proto.canPlayType, true);
  135. }
  136.  
  137. // Override media source extension isTypeSupported() function
  138. const mse = window.MediaSource;
  139. // Check for MSE support before use
  140. if (mse && typeof mse.isTypeSupported == 'function') {
  141. mse.isTypeSupported = makeModifiedTypeChecker(mse.isTypeSupported);
  142. }
  143.  
  144. };
  145.  
  146. function disableAV1() {
  147.  
  148.  
  149.  
  150.  
  151. // This is the setting to disable AV1
  152. // localStorage['yt-player-av1-pref'] = '-1';
  153. try {
  154. Object.defineProperty(localStorage.constructor.prototype, 'yt-player-av1-pref', {
  155. get() {
  156. if (this === localStorage) return '-1';
  157. return this.getItem('yt-player-av1-pref');
  158. },
  159. set(nv) {
  160. this.setItem('yt-player-av1-pref', nv);
  161. return true;
  162. },
  163. enumerable: true,
  164. configurable: true
  165. });
  166. } catch (e) {
  167. // localStorage['yt-player-av1-pref'] = '-1';
  168. }
  169.  
  170. if (localStorage['yt-player-av1-pref'] !== '-1') {
  171.  
  172. console.warn('Disable YouTube AV1', '"yt-player-av1-pref = -1" is not supported in your browser.');
  173. return;
  174. }
  175.  
  176. console.debug("disable-youtube-av1", "AV1 disabled by yt-player-av1-pref = -1");
  177.  
  178.  
  179. }
  180.  
  181.  
  182. disableAV1();
  183.  
  184. // flagConfig();
  185. supportedFormatsConfig();
  186.  
  187.  
  188.  
  189.  
  190. })();