Use YouTube AV1

Use AV1 for video playback on YouTube

  1. // ==UserScript==
  2. // @name Use YouTube AV1
  3. // @description Use AV1 for video playback on YouTube
  4. // @name:zh-TW 使用 YouTube AV1
  5. // @description:zh-TW 使用 AV1 進行 YouTube 影片播放
  6. // @name:zh-HK 使用 YouTube AV1
  7. // @description:zh-HK 使用 AV1 進行 YouTube 影片播放
  8. // @name:zh-CN 使用 YouTube AV1
  9. // @description:zh-CN 使用 AV1 进行 YouTube 视频播放
  10. // @name:ja YouTube AV1 の使用
  11. // @description:ja YouTube の動画再生に AV1 を使用する
  12. // @name:ko YouTube AV1 사용
  13. // @description:ko YouTube의 동영상 재생에 AV1을 사용하기
  14. // @name:vi Sử dụng YouTube AV1
  15. // @description:vi Sử dụng AV1 để phát video trên YouTube
  16. // @name:de YouTube AV1 verwenden
  17. // @description:de Verwende AV1 für die Videowiedergabe auf YouTube
  18. // @name:fr Utiliser YouTube AV1
  19. // @description:fr Utiliser AV1 pour la lecture des vidéos sur YouTube
  20. // @name:it Usa YouTube AV1
  21. // @description:it Usa AV1 per la riproduzione dei video su YouTube
  22. // @name:es Usar AV1 en YouTube
  23. // @description:es Usar 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. //
  38. // @compatible firefox Violentmonkey
  39. // @compatible firefox Tampermonkey
  40. // @compatible firefox FireMonkey
  41. // @compatible chrome Violentmonkey
  42. // @compatible chrome Tampermonkey
  43. // @compatible opera Violentmonkey
  44. // @compatible opera Tampermonkey
  45. // @compatible safari Stay
  46. // @compatible edge Violentmonkey
  47. // @compatible edge Tampermonkey
  48. // @compatible brave Violentmonkey
  49. // @compatible brave Tampermonkey
  50. //
  51. // @unwrap
  52. // @allFrames true
  53. // @inject-into page
  54. // ==/UserScript==
  55.  
  56.  
  57.  
  58. (function (__Promise__) {
  59. 'use strict';
  60.  
  61. /** @type {globalThis.PromiseConstructor} */
  62. const Promise = (async () => { })().constructor; // YouTube hacks Promise in WaterFox Classic and "Promise.resolve(0)" nevers resolve.
  63.  
  64. console.debug("force-youtube-av1", "injected");
  65.  
  66.  
  67.  
  68. const flagConfig = () => {
  69.  
  70. let firstDa = true;
  71. let cid = 0;
  72. const { setInterval, clearInterval, setTimeout } = window;
  73. const tn = () => {
  74.  
  75. const da = (window.ytcfg && window.ytcfg.data_) ? window.ytcfg.data_ : null;
  76. if (!da) return;
  77.  
  78. const isFirstDa = firstDa;
  79. firstDa = false;
  80.  
  81. for (const EXPERIMENT_FLAGS of [da.EXPERIMENT_FLAGS, da.EXPERIMENTS_FORCED_FLAGS]) {
  82.  
  83. if (EXPERIMENT_FLAGS) {
  84. // EXPERIMENT_FLAGS.html5_disable_av1_hdr = false;
  85. // EXPERIMENT_FLAGS.html5_prefer_hbr_vp9_over_av1 = false;
  86. // EXPERIMENT_FLAGS.html5_account_onesie_format_selection_during_format_filter = false;
  87. }
  88.  
  89. }
  90.  
  91. if (isFirstDa) {
  92.  
  93.  
  94. let mo = new MutationObserver(() => {
  95.  
  96. mo.disconnect();
  97. mo.takeRecords();
  98. mo = null;
  99. setTimeout(() => {
  100. cid && clearInterval.call(window, cid);
  101. cid = 0;
  102. tn();
  103. })
  104. });
  105. mo.observe(document, { subtree: true, childList: true });
  106.  
  107.  
  108. }
  109.  
  110.  
  111. };
  112. cid = setInterval.call(window, tn);
  113.  
  114. };
  115.  
  116.  
  117. const supportedFormatsConfig = () => {
  118.  
  119.  
  120. function typeTest(type) {
  121.  
  122. if (typeof type === 'string' && type.startsWith('video/')) {
  123. if (type.includes('av01')) {
  124. if (/codecs[\x20-\x7F]+\bav01\b/.test(type)) return true;
  125. } else if (type.includes('av1')) {
  126. if (/codecs[\x20-\x7F]+\bav1\b/.test(type)) return true;
  127. }
  128. }
  129.  
  130. }
  131.  
  132. // return a custom MIME type checker that can defer to the original function
  133. function makeModifiedTypeChecker(origChecker, dx) {
  134. // Check if a video type is allowed
  135. return function (type) {
  136. let res = undefined;
  137. if (type === undefined) res = false;
  138. else res = typeTest(type);
  139. if (res === undefined) res = origChecker.apply(this, arguments);
  140. else res = !dx ? res : (res ? "probably" : "");
  141.  
  142. // console.debug(20, type, res)
  143.  
  144. return res;
  145. };
  146. }
  147.  
  148. // Override video element canPlayType() function
  149. const proto = (HTMLVideoElement || 0).prototype;
  150. if (proto && typeof proto.canPlayType == 'function') {
  151. proto.canPlayType = makeModifiedTypeChecker(proto.canPlayType, true);
  152. }
  153.  
  154. // Override media source extension isTypeSupported() function
  155. const mse = window.MediaSource;
  156. // Check for MSE support before use
  157. if (mse && typeof mse.isTypeSupported == 'function') {
  158. mse.isTypeSupported = makeModifiedTypeChecker(mse.isTypeSupported);
  159. }
  160.  
  161.  
  162. }
  163.  
  164. function enableAV1() {
  165.  
  166.  
  167. // This is the setting to force AV1
  168. // localStorage['yt-player-av1-pref'] = '8192';
  169. try {
  170. Object.defineProperty(localStorage.constructor.prototype, 'yt-player-av1-pref', {
  171. get() {
  172. if (this === localStorage) return '8192';
  173. return this.getItem('yt-player-av1-pref');
  174. },
  175. set(nv) {
  176. this.setItem('yt-player-av1-pref', nv);
  177. return true;
  178. },
  179. enumerable: true,
  180. configurable: true
  181. });
  182. } catch (e) {
  183. // localStorage['yt-player-av1-pref'] = '8192';
  184. }
  185.  
  186. if (localStorage['yt-player-av1-pref'] !== '8192') {
  187.  
  188. console.warn('Use YouTube AV1 is not supported in your browser.');
  189. return;
  190. }
  191.  
  192.  
  193. console.debug("force-youtube-av1", "AV1 enabled");
  194.  
  195.  
  196. // flagConfig();
  197. supportedFormatsConfig();
  198.  
  199.  
  200.  
  201. }
  202.  
  203.  
  204.  
  205.  
  206. let promise = null;
  207.  
  208. try {
  209. promise = navigator.mediaCapabilities.decodingInfo({
  210. type: "file",
  211. video: {
  212. contentType: "video/mp4; codecs=av01.0.05M.08.0.110.05.01.06.0",
  213. height: 1080,
  214. width: 1920,
  215. framerate: 30,
  216. bitrate: 2826848,
  217. },
  218. audio: {
  219. contentType: "audio/webm; codecs=opus",
  220. channels: "2.1",
  221. samplerate: 44100,
  222. bitrate: 255236,
  223. }
  224. });
  225. } catch (e) {
  226. promise = null;
  227. }
  228.  
  229.  
  230. const callback = (result) => {
  231.  
  232. if (result && result.supported && result.smooth) enableAV1();
  233. else {
  234. console.warn("force-youtube-av1", 'Your browser does not support AV1. You might conside to use the latest version of Google Chrome or Mozilla FireFox.');
  235. }
  236. };
  237.  
  238. (promise || Promise.resolve(0)).catch(callback).then(callback);
  239.  
  240.  
  241.  
  242. })(Promise);