将 m3u8 播放列表文件转换为 XML 列表

Convert m3u8 playlist files to XML list

我想知道是否有任何方法可以使用 Linux shell 或 PHP 将 M3U8 播放列表转换为 XML?例如。

M3U8 播放列表文件

#EXTM3U
#EXTINF:-1 tvg-name="Canal 26" tvg-logo="https://demo.com/xDjOUuz.png" group-title="Argentina",
https://demolivevideo1.com/playlist.m3u8
#EXTINF:-1 tvg-name="LN"  tvg-logo="https://demo2.com/vJYzGt1.png" group-title="Argentina",
https://demolivevideo2.com/playlist.m3u8
#EXTINF:-1 tvg-name="ABC" tvg-logo="https://demo3.com/5CVl5EF.png" group-title="Australia",
https://demolivevideo3.com/playlist.m3u8

XML 转换后的文件结构。

<?xml version="1.0" encoding="utf-8"?>
<data>
  <channels>
    <name>Canal 26</name>
    <banner>https://demo.com/xDjOUuz.png</banner>
    <url>https://demolivevideo1.com/playlist.m3u8</url>
    <country>Australia</country>
  </channels>
  <channels>
    <name>LN</name>
    <banner>https://demo.com/xDjOUuz.png</banner>
    <url>https://demolivevideo2.com/playlist.m3u8</url>
    <country>Australia</country>
  </channels>
  <channels>
    <name>ABC</name>
    <banner>https://demo.com/xDjOUuz.png</banner>
    <url>https://demolivevideo3.com/playlist.m3u8</url>
    <country>Australia</country>
  </channels>
</data>

这有点(实际上是很多)hack(一些字符串替换和拆分),但它应该让你到达那里,或者足够接近(在 PHP 中):

$m3u = '[your playlist above]
';            

$newFile = '<?xml version="1.0" encoding="utf-8"?>
<data></data>
';   

$doc = new DOMDocument();
$doc->loadXML($newFile);
$xpath = new DOMXpath($doc);
$destination = $xpath->query('/data');

#some of the steps next can be combined, but I left them separate for readability
$step1 = explode('#EXTINF:-1 ',str_replace('#EXTM3U','',$m3u));
$step2 = array_slice($step1,1);

foreach ($step2 as $item) {
    $step3 = str_replace('" ','" xxx',explode(',', $item));
    $step4 = explode('xxx',$step3[0]);
    $link = explode(',',$item)[1];
    $params = [];
    foreach ($step4 as $step)
       {
       $param = explode('=',$step)[1];
       array_push($params,$param);
       }

    #create you <channels> template
    $chan = "
    <channels>
    <name>{$params[0]}</name>
    <banner>{$params[1]}</banner>
    <url>{$link}</url>
    <country>{$params[2]}</country>  
    </channels>";

    $template = $doc->createDocumentFragment();
    $template->appendXML($chan);
    $destination[0]->appendChild($template);
       
};

echo $doc->saveXml();

这应该是您的预期输出。