Greasy Fork

Greasy Fork is available in English.

YouTube H.264 (h264ify)

https://github.com/erkserkserks/h264ify

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

// ==UserScript==
// @name YouTube H.264 (h264ify)
// @name:ru YouTube H.264 (h264ify)
// @namespace https://www.youtube.com
// @version 2023.11.19.2
// @description https://github.com/erkserkserks/h264ify
// @description:ru https://github.com/erkserkserks/h264ify
// @match *://*.youtube.com/*
// @match *://*.youtube-nocookie.com/*
// @match *://*.youtubekids.com/*
// @license MIT
// @grant none
// @run-at document-start
// ==/UserScript==

(function() {
    'use strict';

    // Video settings
    const videoOptions = {
        block60fps: true,
        disallowedTypes: ['webm', 'vp8', 'vp9', 'av01']
    };

    // Pre-compiled regular expressions for performance
    const disallowedTypeRegex = new RegExp(videoOptions.disallowedTypes.join('|'), 'i');
    const frameRateRegex = /framerate=(\d+)/;
    const typeSupportCache = {};

    // Modify the video type checker with the provided settings
    modifyVideoTypeChecker(videoOptions);

    function modifyVideoTypeChecker({ block60fps }) {
        const mediaSource = window.MediaSource;

        if (mediaSource === undefined) return;

        const originalIsTypeSupported = mediaSource.isTypeSupported.bind(mediaSource);
        mediaSource.isTypeSupported = modifiedTypeChecker;

        function modifiedTypeChecker(type) {
            if (typeof type !== 'string') return '';

            // Check cache first
            if (type in typeSupportCache) {
                return typeSupportCache[type];
            }

            // Check for disallowed types using a single regex
            if (disallowedTypeRegex.test(type)) {
                typeSupportCache[type] = '';
                return '';
            }

            // Check for frame rate restrictions
            if (block60fps && frameRateRegex.test(type)) {
                const [, frameRate] = frameRateRegex.exec(type);
                if (frameRate > 30) {
                    typeSupportCache[type] = '';
                    return '';
                }
            }

            // Cache and return the result of the original checker
            const isSupported = originalIsTypeSupported(type);
            typeSupportCache[type] = isSupported;
            return isSupported;
        }
    }
})();