mirror of
https://github.com/zimplexing/OrionTV.git
synced 2026-05-18 14:57:30 +08:00
Update
This commit is contained in:
55
services/m3u8.ts
Normal file
55
services/m3u8.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
interface CacheEntry {
|
||||
resolution: string | null;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
const resolutionCache: { [url: string]: CacheEntry } = {};
|
||||
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
|
||||
|
||||
export const getResolutionFromM3U8 = async (
|
||||
url: string
|
||||
): Promise<string | null> => {
|
||||
// 1. Check cache first
|
||||
const cachedEntry = resolutionCache[url];
|
||||
if (cachedEntry && Date.now() - cachedEntry.timestamp < CACHE_DURATION) {
|
||||
return cachedEntry.resolution;
|
||||
}
|
||||
|
||||
if (!url.toLowerCase().endsWith(".m3u8")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
const playlist = await response.text();
|
||||
const lines = playlist.split("\n");
|
||||
let highestResolution = 0;
|
||||
let resolutionString: string | null = null;
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("#EXT-X-STREAM-INF")) {
|
||||
const resolutionMatch = line.match(/RESOLUTION=(\d+)x(\d+)/);
|
||||
if (resolutionMatch) {
|
||||
const height = parseInt(resolutionMatch[2], 10);
|
||||
if (height > highestResolution) {
|
||||
highestResolution = height;
|
||||
resolutionString = `${height}p`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Store result in cache
|
||||
resolutionCache[url] = {
|
||||
resolution: resolutionString,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
return resolutionString;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user