使用 PHP 下载普通的 M3u8 视频流媒体文件
<?php
error_reporting(E_ALL & ~E_WARNING);
if ($argc < 1) {
exit();
}
$url = $argv[1] ?? '';
if (filter_var($url, FILTER_VALIDATE_URL)) {
echo "Parse M3u8 Url: {$url} \n";
$file = md5($url) . '.ts';
foreach (explode("\n", getRemoteContent($url)) as $line) {
if (pathinfo($line)['extension'] ?? '' === 'ts') {
file_put_contents($file, getRemoteContent(dirname($url) . '/' . $line), FILE_APPEND);
}
}
} else {
echo "Error Url: {$url} \n";
}
function getRemoteContent($url)
{
$context = stream_context_create(['http' => ['header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, as - is) Chrome/100.0.0.0 Safari/537.36"]]);
$content = file_get_contents($url, false, $context);
if ($content !== false) {
return $content;
} else {
return '';
}
}