DPlayer

repository·master·Indexed 12 days ago

https://github.com/diygod/dplayer

A feature-rich HTML5 video player specializing in danmaku (bullet comments) and support for streaming protocols including HLS, FLV, MPEG DASH, and WebTorrent. Version 1.27.1 includes capabilities for quality switching, screenshots, subtitles, and a public API for playback and danmaku management.

Tokens
9.4K
Snippets
26
Records
37
Agent score
94%

What's inside DPlayer

  1. Overview of DPlayer features and supported formats

    master

    DPlayer is an HTML5 danmaku video player designed to facilitate the easy integration of video and danmaku (bullet comments) into web projects.

    Supported Streaming Formats

    • HLS (via hls.js)
    • FLV (via flv.js)
    • MPEG DASH (via dash.js)
    • WebTorrent
    • Any other custom streaming formats

    Supported Media Formats

    • MP4 H.264
    • WebM
    • Ogg Theora Vorbis

    Key Features

    • Danmaku (bullet comments)
    • Screenshot capability
    • Hotkeys
    • Quality switching
    • Thumbnails
    • Subtitle support
  2. Configure Live streaming and Danmaku via WebSocket

    master

    To use DPlayer for live streams with danmaku, set live: true and provide an apiBackend object to handle WebSocket communication.

    Initialization

    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        live: true,
        danmaku: true,
        apiBackend: {
            read: function (options) {
                // Logic to receive danmaku via WebSocket
                // Call options.success(danmakuArray) when data arrives
                options.success([]);
            },
            send: function (options) {
                // Logic to send danmaku via WebSocket
                options.success();
            },
        },
        video: {
            url: 'demo.m3u8',
            type: 'hls',
        },
    });

    Manually drawing danmaku You can manually trigger a danmaku to appear on the screen using dp.danmaku.draw():

    const danmaku = {
        text: 'Get a danmaku via WebSocket',
        color: '#fff',
        type: 'right',
    };
    dp.danmaku.draw(danmaku);
    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        live: true,
        danmaku: true,
        apiBackend: {
            read: function (options) {
                console.log('Pretend to connect WebSocket');
                options.success([]);
            },
            send: function (options) {
                console.log('Pretend to send danmaku via WebSocket', options.data);
                options.success();
            },
        },
        video: {
            url: 'demo.m3u8',
            type: 'hls',
        },
    });
  3. Prevent memory leaks when using customType (MSE)

    master

    When implementing a custom MSE library (like Hls.js) via the customType option, you must manually manage the lifecycle of the external library instance to avoid memory leaks.

    Specifically, you should listen for the destroy and quality_end events on the DPlayer instance to call the cleanup method (e.g., hls.destroy()) of your library. If a quality switch occurs, ensure you only destroy the old instance if it is no longer the active one.

    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            quality: [
                { name: 'HD', url: 'demo_hd.m3u8', type: 'Hls' },
                { name: 'SD', url: 'demo_sd.m3u8', type: 'Hls' },
            ],
            defaultQuality: 0,
            customType: {
                Hls: (video, player) => {
                    const hls = new window.Hls();
                    hls.loadSource(video.src);
                    hls.attachMedia(video);
    
                    hls.sessionId = crypto.randomUUID();
                    player.sessionId = hls.sessionId;
    
                    player.events.on('destroy', () => {
                        hls.destroy();
                    });
    
                    player.events.on('quality_end', () => {
                        if (hls.sessionId !== player.sessionId) {
                            hls.destroy();
                        }
                    });
                },
            },
        },
    });
  4. Enable FLV support in DPlayer

    master

    To support FLV, load the flv.js library before DPlayer.min.js.

    Option 1: Using built-in FLV support

    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'demo.flv',
            type: 'flv',
        },
        pluginOptions: {
            flv: {
                mediaDataSource: {
                    // mediaDataSource config
                },
                config: {
                    // config
                },
            },
        },
    });
    console.log(dp.plugins.flv); // Access the flv instance

    Option 2: Using customType

    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'demo.flv',
            type: 'customFlv',
            customType: {
                customFlv: function (video, player) {
                    const flvPlayer = flvjs.createPlayer({
                        type: 'flv',
                        url: video.src,
                    });
                    flvPlayer.attachMediaElement(video);
                    flvPlayer.load();
                },
            },
        },
    });
    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'demo.flv',
            type: 'flv',
        },
        pluginOptions: {
            flv: {
                mediaDataSource: {
                    // mediaDataSource config
                },
                config: {
                    // config
                },
            },
        },
    });
  5. Quick Start: Initialize DPlayer with a module bundler

    master

    If you are using a module bundler (like Webpack or Vite), import the DPlayer class and instantiate it with an options object.

    import DPlayer from 'dplayer';
    
    const dp = new DPlayer(options);
  6. Enable HLS support in DPlayer

    master

    To support HLS (.m3u8), you must load the hls.js library before DPlayer.min.js. You can use the built-in hls type and pass configuration via pluginOptions.

    Option 1: Using built-in HLS support

    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'demo.m3u8',
            type: 'hls',
        },
        pluginOptions: {
            hls: {
                // hls config
            },
        },
    });
    console.log(dp.plugins.hls); // Access the Hls instance

    Option 2: Using customType If you need manual control, define a customType function:

    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'demo.m3u8',
            type: 'customHls',
            customType: {
                customHls: function (video, player) {
                    const hls = new Hls();
                    hls.loadSource(video.src);
                    hls.attachMedia(video);
                },
            },
        },
    });
    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'demo.m3u8',
            type: 'hls',
        },
        pluginOptions: {
            hls: {
                // hls config
            },
        },
    });
  7. Enable WebTorrent support in DPlayer

    master

    To support WebTorrent (magnet links), load the webtorrent library before DPlayer.min.js.

    Option 1: Using built-in WebTorrent support

    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'magnet:demo',
            type: 'webtorrent',
        },
        pluginOptions: {
            webtorrent: {
                // webtorrent config
            },
        },
    });
    console.log(dp.plugins.webtorrent); // Access the WebTorrent instance

    Option 2: Using customType

    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'magnet:demo',
            type: 'customWebTorrent',
            customType: {
                customWebTorrent: function (video, player) {
                    player.container.classList.add('dplayer-loading');
                    const client = new WebTorrent();
                    const torrentId = video.src;
                    client.add(torrentId, (torrent) => {
                        const file = torrent.files.find((file) => file.name.endsWith('.mp4'));
                        file.renderTo(
                            video,
                            {
                                autoplay: player.options.autoplay,
                            },
                            () => {
                                player.container.classList.remove('dplayer-loading');
                            }
                        );
                    });
                },
            },
        },
    });
    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'magnet:demo',
            type: 'webtorrent',
        },
        pluginOptions: {
            webtorrent: {
                // webtorrent config
            },
        },
    });
  8. Enable MPEG DASH support in DPlayer

    master

    To support MPEG DASH (.mpd), load the dash.js library before DPlayer.min.js.

    Option 1: Using built-in DASH support

    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'demo.mpd',
            type: 'dash',
        },
        pluginOptions: {
            dash: {
                // dash config
            },
        },
    });
    console.log(dp.plugins.dash); // Access the Dash instance

    Option 2: Using customType

    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'demo.mpd',
            type: 'customDash',
            customType: {
                customDash: function (video, player) {
                    dashjs.MediaPlayer().create().initialize(video, video.src, false);
                },
            },
        },
    });
    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: 'demo.mpd',
            type: 'dash',
        },
        pluginOptions: {
            dash: {
                // dash config
            },
        },
    });
  9. How DPlayer handles different video types

    master

    DPlayer supports multiple streaming protocols through specialized plugins. When initMSE is called, it detects the type or uses the provided type option to initialize the appropriate engine:

    • hls: Uses hls.js for HTTP Live Streaming.
    • flv: Uses flv.js for Flash Video.
    • dash: Uses dash.js for Dynamic Adaptive Streaming over HTTP.
    • webtorrent: Uses WebTorrent for P2P streaming.
    • normal: Standard HTML5 video.

    Note: For hls, flv, and dash, the corresponding libraries (window.Hls, window.flvjs, or window.dashjs) must be available in the global scope.