使用 YouTube AV1

使用 AV1 進行 YouTube 影片播放

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址