Jump to content

How do I get a live hls stream from api in latest version of emby?


Recommended Posts

Posted

I have an api key and a channel id, but I can't seem to get a valid stream or m3u8 file.

 

The .ts files don't pass api key so I upgraded and now nothing works.

 

WHat's the easiest way to get a valid m3u8  stream via curl?

 

 

Posted

which parameters are required?

Posted (edited)

Can someone get this code to work for me? I am not sure which api calls I need to make:

I'm not getting any media sources back from response when clicking a Channel in the filter.
 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Emby Live TV</title>
    <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
    <style>
        #channel-list-container {
            max-height: 300px;
            overflow-y: scroll;
            border: 1px solid #ccc;
            margin-bottom: 20px;
        }
        #channel-list {
            list-style-type: none;
            padding: 0;
            margin: 0;
        }
        #channel-list li {
            padding: 8px;
            cursor: pointer;
        }
        #channel-list li:hover {
            background-color: #f0f0f0;
        }
        #filter-input {
            margin-bottom: 10px;
            padding: 5px;
            width: 100%;
        }
    </style>
</head>
<body>
    <h1>Emby Live TV Channels</h1>
    <input type="text" id="filter-input" placeholder="Type to filter channels...">
    <div id="channel-list-container">
        <ul id="channel-list"></ul>
    </div>
    <video id="video" controls width="640" height="360"></video>

    <script>
        const apiKey = 'xxx';
        const embyServerAddress = 'https://emby.xxx.com';
        const deviceId = generateUUID();

        document.addEventListener('DOMContentLoaded', function() {
            fetchChannels();
            document.getElementById('filter-input').addEventListener('input', filterChannels);
        });

        function fetchChannels() {
            fetch(`${embyServerAddress}/emby/LiveTV/Channels?api_key=${apiKey}`)
                .then(response => response.json())
                .then(data => {
                    const channelList = document.getElementById('channel-list');
                    data.Items.forEach(channel => {
                        const listItem = document.createElement('li');
                        listItem.textContent = channel.Name;
                        listItem.dataset.name = channel.Name.toLowerCase();
                        listItem.onclick = () => startPlayback(channel.Id);
                        channelList.appendChild(listItem);
                    });
                })
                .catch(error => console.error('Error fetching channels:', error));
        }

        function filterChannels() {
            const filterValue = document.getElementById('filter-input').value.toLowerCase();
            const channelListItems = document.getElementById('channel-list').getElementsByTagName('li');
            Array.from(channelListItems).forEach(item => {
                if (item.dataset.name.includes(filterValue)) {
                    item.style.display = '';
                } else {
                    item.style.display = 'none';
                }
            });
        }

        function startPlayback(channelId) {
            fetch(`${embyServerAddress}/emby/LiveTV/Channels/${channelId}?api_key=${apiKey}`)
                .then(response => response.json())
                .then(data => {
                    if (!data.MediaSources || data.MediaSources.length === 0) {
                        console.error('No media sources found for this channel');
                        return;
                    }
                    const mediaSourceId = data.MediaSources[0].Id;
                    fetchLiveStreamId(channelId, mediaSourceId);
                })
                .catch(error => console.error('Error fetching channel details:', error));
        }

        function fetchLiveStreamId(channelId, mediaSourceId) {
            fetch(`${embyServerAddress}/emby/LiveTV/LiveStreamFiles?api_key=${apiKey}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    MediaSourceId: mediaSourceId,
                    DeviceId: deviceId,
                    LiveStreamRequest: {
                        ChannelId: channelId
                    }
                })
            })
            .then(response => response.json())
            .then(data => {
                const liveStreamId = data.LiveStreamId;
                playStream(liveStreamId);
            })
            .catch(error => console.error('Error fetching live stream ID:', error));
        }

        function playStream(liveStreamId) {
            const video = document.getElementById('video');
            const streamUrl = `${embyServerAddress}/emby/Videos/${liveStreamId}/master.m3u8?api_key=${apiKey}&DeviceId=${deviceId}`;
            if (Hls.isSupported()) {
                const hls = new Hls();
                hls.loadSource(streamUrl);
                hls.attachMedia(video);
                hls.on(Hls.Events.MANIFEST_PARSED, function () {
                    video.play();
                });
            } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
                video.src = streamUrl;
                video.addEventListener('canplay', function () {
                    video.play();
                });
            }
        }

        function generateUUID() {
            var d = new Date().getTime();
            var d2 = (performance && performance.now && (performance.now()*1000)) || 0;
            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                var r = Math.random() * 16;
                if(d > 0){
                    r = (d + r)%16 | 0;
                    d = Math.floor(d/16);
                } else {
                    r = (d2 + r)%16 | 0;
                    d2 = Math.floor(d2/16);
                }
                return (c === 'x' ? r : (r&0x3|0x8)).toString(16);
            });
        }
    </script>
</body>
</html>

 

Edited by chovyfu
Posted

I have an api key and a channel id. How can I get a working m3u8 stream url for VLC?

Posted

From what I gather I need to create a playback request of some sort, but I'm having trouble figuring out what api call to make so I get back valid master.m3u8 stream urls.

 

Posted (edited)

```

curl 'https://emby.example.com/videos/3549/live.m3u8?DeviceId=1ec39e95-dc46-43bd-ac00-0988ec46967f&MediaSourceId=c5e1795b695cc690c6140a2010433d2d&PlaySessionId=5e8637ad8f73444cb3cf393a97e731b7&api_key=xxxx&LiveStreamId=06044cf0e6f93cdae5f285c9ecfaaeb4_01413a525b3a9622ce6fdf19f7dde354_c5e1795b695cc690c6140a2010433d2d&VideoCodec=h264&AudioCodec=aac&VideoBitrate=199808000&AudioBitrate=192000&MaxWidth=640&MaxHeight=360&AudioStreamIndex=1&TranscodingMaxAudioChannels=2&SegmentContainer=ts&MinSegments=1&BreakOnNonKeyFrames=True&SubtitleStreamIndexes=-1&ManifestSubtitles=vtt?api_key=xxx' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0'

```

In the firefox browser I get an NS_BINDING_ABORTED error on most streams....In chrome I get a `(cancelled)` error.

 

Any idea why this is happening?

 

Edited by chovyfu
remove key

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...