技术 通过 php 合并多个链接并设置 ua

balgain · 2024年06月23日 · 最后由 mengzehe 回复于 2024年06月24日 · 1147 次阅读

coding 大佬的 pixman 更新到了 1.3.6,内置的播放源也更多了。通过 php 可以合并多个链接并设置 ua,解决某些播放器不能播放 itv 的问题。

<?php
// 设置多个M3U文件的URL数组
$m3uUrls = [
    'http://example.com/path/to/first.m3u',
    'http://example.com/path/to/second.m3u',
    'http://example.com/path/to/third.m3u',
    // 添加更多URL...
];

// 设置User-Agent
$userAgent = 'MyCustomUserAgent/1.0';

// 创建HTTP上下文选项
$options = [
    'http' => [
        'header' => "User-Agent: $userAgent\r\n"
    ]
];

// 创建上下文资源
$context = stream_context_create($options);

// 读取M3U文件的内容
function getM3UContent($url, $context) {
    $content = file_get_contents($url, false, $context);
    if ($content === false) {
        die("无法读取M3U文件:$url");
    }
    return $content;
}

// 提取文件名
function getFileNameFromUrl($url) {
    return pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME);
}

// 初始化内容存储数组
$groups = [];
$groupTitleMap = [];

// 读取并处理多个M3U文件的内容
foreach ($m3uUrls as $url) {
    $content = getM3UContent($url, $context);
    $fileName = getFileNameFromUrl($url);
    // 移除头部
    $content = preg_replace('/^#EXTM3U\s*/', '', $content);

    // 按行拆分内容
    $lines = explode("\n", $content);
    $currentGroup = '';
    $currentTvgId = '';
    $currentEntry = '';

    foreach ($lines as $line) {
        if (preg_match('/#EXTINF.*group-title="([^"]*)".*tvg-id="([^"]*)".*,/', $line, $matches)) {
            $originalGroup = $matches[1];
            $currentTvgId = $matches[2];

            // 如果group-title重复,则在末尾添加文件名
            $newGroup = $originalGroup;
            if (isset($groupTitleMap[$originalGroup])) {
                $newGroup .= ' (' . $fileName . ')';
            }
            $groupTitleMap[$originalGroup] = true;

            $line = preg_replace('/group-title="[^"]*"/', 'group-title="' . $newGroup . '"', $line);

            $currentEntry = $line;
        } else if (!empty($line)) {
            $currentEntry .= "\n" . $line;
            $groups[$newGroup][$currentTvgId][] = $currentEntry;
            $currentEntry = '';
        }
    }
}

// 对每个group-title下的条目按tvg-id排序
foreach ($groups as $group => &$entries) {
    ksort($entries);
}

// 初始化合并内容变量
$mergedContent = "#EXTM3U\n";

// 合并并生成新的M3U内容
foreach ($groups as $group => $entries) {
    foreach ($entries as $tvgIdEntries) {
        foreach ($tvgIdEntries as $entry) {
            $mergedContent .= $entry . "\n";
        }
    }
}

// 设置HTTP头部,使得PHP输出被浏览器识别为M3U文件
header('Content-Type: audio/x-mpegurl');
header('Content-Disposition: attachment; filename="merged.m3u"');

// 输出合并后的内容
echo $mergedContent;

目前存在的问题是多个 m3u 里面的相同的 group-title 会合并到一起。

又学到了

运行了一下这段代码好像不能使用呢,螺柱运行成功了吗?我的提示以下错误: Warning: Undefined variable $newGroup in /var/www/html/LiveOnline.php on line 73

需要 登录 后方可回复, 如果你还没有账号请 注册新账号