Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read M2TS stream / MP3 / MP4 #496

Open
ayrtonbardiot opened this issue Dec 20, 2024 · 1 comment
Open

Read M2TS stream / MP3 / MP4 #496

ayrtonbardiot opened this issue Dec 20, 2024 · 1 comment

Comments

@ayrtonbardiot
Copy link

ayrtonbardiot commented Dec 20, 2024

Is your feature request related to a problem? Please describe.
IPTVNator doesn't play MPEG-TS stream, only M3U8/HLS, but TS is a major used format for live channels.

Describe the solution you'd like
Add MPEG.JS to demux MPEG-TS and make it playable.

Additional context
So, I started to make an example in JS/Electron to got a functional HTML5 player with MPEG.JS, and this is how I did to detect stream type after the xtream-code redirect on the /live/ endpoint. I'm sure there is a better way, but that's an idea.

        const url = `${SERVER_URL}/live/${userData.username}/${PASSWORD}/${stream.stream_id}.ts`;
        $.ajax({
            url: url,
            type: "GET",
            processData: false,
            xhrFields: {
                onprogress: function (e) {
                    const response = e.currentTarget.response;
                    if (!response) return;
                    const encoder = new TextEncoder();
                    const encodedData = encoder.encode(response);
                    this.abort();
                    if(encodedData[0] === 35 && encodedData[1] === 69 && encodedData[2] === 88 && encodedData[3] === 84 && encodedData[4] === 77) {
                        playM3U8(url);
                    } else if(encodedData[0] === 71 && encodedData[1] === 64 && encodedData[2] === 17) {
                        playM2TS(url);
                    } else if(encodedData[4] === 102 && encodedData[5] === 116 && encodedData[6] === 121 || encodedData[0] === 239 && encodedData[1] === 191 && encodedData[2] === 189) {
                        playMP4(url);
                    } else {
                        console.error("Unsupported stream type.");
                    }
                }
            }
        })

function playM2TS(url) {
    if (mpegts.getFeatureList().mseLivePlayback) {
        // Get the video element
        const videoEl = document.getElementById("videoplayer");

        // Ensure the video element exists
        if (!videoEl || !(videoEl instanceof HTMLMediaElement)) {
            console.error("Error: Video element not found or invalid.");
        } else {
            // Create the player
            const player = mpegts.createPlayer({
                type: 'mpegts',
                isLive: true,
                url: url
            });

            // Attach player to the video element
            player.attachMediaElement(videoEl);

            // Load and start playback
            player.load();

            // Wait for the video element to be ready before calling play
            videoEl.addEventListener('canplay', () => {
                player.play();
                console.log("Playback started successfully.");
            });

            // Error handling
            player.on(mpegts.Events.ERROR, (type, details) => {
                console.error("MPEG-TS Player Error:", type, details);
            });
        }
    } else {
        console.error("MPEG-TS playback is not supported in this browser.");
    }
}

function playM3U8(url) {
    const video = document.getElementById('videoplayer');
    if (Hls.isSupported()) {
        const hls = new Hls();
        hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
            video.play();
        });
        hls.loadSource(url);
        hls.attachMedia(video);
    } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
        video.src = url;
        video.play();
    } else {
        alert("HLS not supported in this browser.");
    }
}

function playMP4(url) {
    const video = document.getElementById('videoplayer');
    video.src = url;
    video.play();

    video.onended = () => {
        playMP4(url);
    }
}

I didn't tried to implement this in IPTVNator.

Have a good day, and merry christmas!

@newspeer
Copy link

That would be great. I love the player, but can't use it as my provider delivers .ts streams.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants