UNPKG

@zezosoft/zezo-ott-react-native-video-player

Version:

React Native OTT Video Player for Android & iOS. Supports playlists, seasons, auto-next playback, subtitles, theming, analytics, fullscreen mode, and custom controls. 🚀 Powered by ZezoSoft.

88 lines (86 loc) • 2.6 kB
"use strict"; import { useEffect, useState } from 'react'; import axios from 'axios'; async function getHLSBandwidthAndResolutions(m3u8Url) { try { if (!m3u8Url?.endsWith('.m3u8')) return []; const { data } = await axios.get(m3u8Url); if (!data) return []; const lines = data.split('\n'); const streams = []; let bandwidth = null; let width = null; let height = null; for (const line of lines) { if (line.startsWith('#EXT-X-STREAM-INF')) { const bandwidthMatch = line.match(/BANDWIDTH=(\d+)/); const resolutionMatch = line.match(/RESOLUTION=(\d+)x(\d+)/); bandwidth = bandwidthMatch ? Number(bandwidthMatch[1]) : null; if (resolutionMatch) { width = Number(resolutionMatch[1]); height = Number(resolutionMatch[2]); } } if (line.trim().endsWith('.m3u8') && bandwidth !== null && width !== null && height !== null) { streams.push({ bandwidth, width, height }); bandwidth = null; width = null; height = null; } } streams.sort((a, b) => b.height - a.height); const autoStream = { bandwidth: 0, width: 'auto', height: 'auto' }; return [autoStream, ...streams]; } catch (error) { console.error('Error fetching or parsing HLS stream:', error); return []; } } // Simple in-memory cache const resolutionCache = {}; export const useVideoResolutions = track => { const [resolutions, setResolutions] = useState([{ height: 'auto', bandwidth: null }]); useEffect(() => { if (!track) return; const source = track.isTrailer ? track.trailerSource : track.sourceLink; if (!source) return; // Cache hit if (resolutionCache[source]) { setResolutions(resolutionCache[source]); return; } const fetchResolutions = async () => { try { const data = await getHLSBandwidthAndResolutions(source); const filteredData = data.filter(item => typeof item.height === 'number'); const newResolutions = [{ height: 'auto', bandwidth: null }, ...filteredData.map(item => ({ height: item.height, bandwidth: item.bandwidth }))]; resolutionCache[source] = newResolutions; // cache setResolutions(newResolutions); } catch (error) { console.error('Failed to fetch video resolutions', error); } }; fetchResolutions(); }, [track]); return resolutions; }; //# sourceMappingURL=useVideoResolutions.js.map