使用 YouTube AV1

使用 AV1 进行 YouTube 视频播放

目前为 2023-07-22 提交的版本。查看 最新版本

  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 1.0.8
  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. // @unwrap
  41. // @allFrames true
  42. // @inject-into page
  43. // ==/UserScript==
  44.  
  45. (function (__Promise__) {
  46. 'use strict';
  47.  
  48. /** @type {globalThis.PromiseConstructor} */
  49. const Promise = (async () => { })().constructor; // YouTube hacks Promise in WaterFox Classic and "Promise.resolve(0)" nevers resolve.
  50.  
  51. console.debug("force-youtube-av1", "injected");
  52.  
  53. function enableAV1() {
  54.  
  55. console.debug("force-youtube-av1", "AV1 enabled");
  56.  
  57.  
  58. // This is the setting to force AV1
  59. // localStorage['yt-player-av1-pref'] = '8192';
  60. Object.defineProperty(localStorage, 'yt-player-av1-pref', { value: '8192', writable: true, enumerable: true, configurable: true });
  61.  
  62. function typeTest(type) {
  63.  
  64.  
  65. let disallowed_types = ['vp8', 'vp9'];
  66. // mp4a is a container for AAC. In most cases (<192kbps), Opus is better than AAC.
  67. // vp09 will be also disabled if av1 is enabled.
  68. for (const disallowed_type of disallowed_types) {
  69. if (type.includes(disallowed_type)) return false;
  70. }
  71.  
  72. let force_allow_types = ['av1', 'av01', 'hev1'];
  73. // av1 is currently supported by Firefox and Chrome except Edge
  74. for (const force_allow_type of force_allow_types) {
  75. if (type.includes(force_allow_type)) return true;
  76. }
  77.  
  78. }
  79.  
  80. // return a custom MIME type checker that can defer to the original function
  81. function makeModifiedTypeChecker(origChecker) {
  82. // Check if a video type is allowed
  83. return function (type) {
  84. let res = undefined;
  85. if (type === undefined) res = false;
  86. else {
  87. res = typeTest(type);
  88. }
  89. if (res === undefined) res = origChecker.apply(this, arguments);
  90.  
  91. // console.debug(20, type, res)
  92.  
  93. return res;
  94. };
  95. }
  96.  
  97. // Override video element canPlayType() function
  98. const proto = (HTMLVideoElement || 0).prototype;
  99. if (proto && typeof proto.canPlayType == 'function') {
  100. proto.canPlayType = makeModifiedTypeChecker(proto.canPlayType);
  101. }
  102.  
  103. // Override media source extension isTypeSupported() function
  104. const mse = window.MediaSource;
  105. // Check for MSE support before use
  106. if (mse && typeof mse.isTypeSupported == 'function') {
  107. mse.isTypeSupported = makeModifiedTypeChecker(mse.isTypeSupported);
  108. }
  109.  
  110. }
  111.  
  112.  
  113. let promise = null;
  114.  
  115. function callback(result) {
  116.  
  117. if (result && result.supported && result.smooth) enableAV1();
  118. else {
  119. 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.');
  120.  
  121. }
  122. }
  123.  
  124.  
  125. try {
  126. promise = navigator.mediaCapabilities.decodingInfo({
  127. type: "file",
  128. video: {
  129. contentType: "video/mp4; codecs=av01.0.05M.08.0.110.05.01.06.0",
  130. height: 1080,
  131. width: 1920,
  132. framerate: 30,
  133. bitrate: 2826848,
  134. },
  135. audio: {
  136. contentType: "audio/webm; codecs=opus",
  137. channels: "2.1",
  138. samplerate: 44100,
  139. bitrate: 255236,
  140. }
  141. }).then(callback).catch(callback);
  142.  
  143. } catch (e) {
  144. promise = null;
  145. }
  146.  
  147. if (!promise) promise = Promise.resolve(0).then(callback).catch(callback);
  148.  
  149.  
  150.  
  151.  
  152. })(Promise);