Skip to content

Instantly share code, notes, and snippets.

@Mr-Groch
Last active August 20, 2020 19:14
Show Gist options
  • Save Mr-Groch/5b7eb468e7c31ba7e5b3fcc37c9e1743 to your computer and use it in GitHub Desktop.
Save Mr-Groch/5b7eb468e7c31ba7e5b3fcc37c9e1743 to your computer and use it in GitHub Desktop.
PHP online converters for adding catchup-source tag for PVR IPTV Archive Client on the fly (requires https://github.com/ttodua/useful-php-scripts/blob/master/get-remote-url-content-data.php)
<?php
function get_flussonic_catchup_days($api_result)
{
if (empty($api_result)) {
return -1;
}
else {
$result = json_decode($api_result, true);
if (json_last_error() === 0) {
foreach($result as $key => $value) {
if (isset($value['error']) && $value['error'] == 'no_dvr') {
return 0;
}
else {
$from = $value['from'];
$to = time();
//$to = $value['to'];
return floor(($to-$from)/86400);
}
}
}
}
}
if (empty($_GET["url"])) {
echo '<form method="get" action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'">';
echo 'URL: <input type="text" name="url" size="100">';
echo '<input type="submit" value="Convert"><br>';
echo 'Output type: <select name="type"><option value="">Unchanged</option><option value="hls">HLS</option><option value="ts">MPEG-TS</option></select><br>';
echo '</form>';
} else {
require_once 'get-remote-url-content-data.php';
$type = $_GET['type'];
$content = get_remote_data(urldecode($_GET['url']));
if (empty($content)) {
echo 'BAD URL';
return;
}
foreach (preg_split('~[\r\n]+~', $content) as $line) {
if (preg_match('~^#EXTINF~i', $line) === 1) {
$prev_line = $line;
continue;
}
if (preg_match('~^(http[s]?://[^/]+)/([^/]+)/([^/\.\?]+)(?:\.m3u8)?(\?.+=.+)?$~i', $line, $matches) === 1 && isset($prev_line)) {
$catchup_days = get_flussonic_catchup_days(get_remote_data($matches[1].'/'.$matches[2].'/recording_status.json'.$matches[4]));
if ($type == 'hls') {
$matches[3] = 'video';
$line = $matches[1].'/'.$matches[2].'/'.$matches[3].'.m3u8'.$matches[4];
} elseif ($type == 'ts') {
$matches[3] = 'mpegts';
$line = $matches[1].'/'.$matches[2].'/'.$matches[3].$matches[4];
}
if ($catchup_days > 0) {
if ($matches[3] == 'mpegts') {
$catchup_str = ' catchup-days="'.$catchup_days.'" catchup="default" catchup-source="'.$matches[1].'/'.$matches[2].'/timeshift_abs-${start}.ts'.$matches[4].'"';
}
else {
$catchup_str = ' catchup-days="'.$catchup_days.'" catchup="default" catchup-source="'.$matches[1].'/'.$matches[2].'/'.$matches[3].'-${start}-14400.m3u8'.$matches[4].'"';
}
$new_prev_line = substr_replace($prev_line, $catchup_str, strpos($prev_line, ' '), 0);
$new_content .= $new_prev_line.PHP_EOL;
}
else {
$new_content .= $prev_line.PHP_EOL;
}
unset($prev_line);
} elseif (isset($prev_line)) {
$new_content .= $prev_line.PHP_EOL;
unset($prev_line);
}
$new_content .= $line.PHP_EOL;
}
echo $new_content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment