-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.php
More file actions
executable file
·144 lines (126 loc) · 3.83 KB
/
convert.php
File metadata and controls
executable file
·144 lines (126 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env php
<?php
include 'vendor/autoload.php';
use M3uParser\M3uParser;
$obj = new M3uParser();
$obj->addDefaultTags();
$files = glob('m3u/*.m3u');
$a = [];
foreach ($files as $file) {
$data = $obj->parseFile($file);
foreach ($data as $entry) {
$suffix = '';
$normalizedChannelName = normalizeChannelName($entry->getExtTags()[0]->getTitle());
$normalizedChannelKey = normalizeChannelKey($normalizedChannelName);
// check for duplicate channel names
if (isset($a[$normalizedChannelKey])) {
if ($a[$normalizedChannelKey]['path'] == $entry->getPath()) {
echo "Skipping duplicate entry for '$normalizedChannelName'." . PHP_EOL;
continue;
}
// distinguish them by URL hash
$suffix = ' | ' . sha1($entry->getPath());
}
// check if URL is valid
if (!validUrl($entry->getPath())) {
echo "'$normalizedChannelName' does not have a valid URL; skipping." . PHP_EOL;
continue;
}
// check if stream is playable
if (!validStream($entry->getPath())) {
echo "'$normalizedChannelName' does not have a valid stream; skipping." . PHP_EOL;
continue;
}
if (strlen($suffix)) {
$entry->getExtTags()[0]->setTitle($normalizedChannelName . $suffix);
$entry->getExtTags()[0]->setAttribute('tvg-name', $normalizedChannelName . $suffix);
}
$a[$normalizedChannelKey . $suffix] = [
'entry' => $entry->__toString(),
];
}
}
ksort($a);
print_r($a);
writePlaylist($a);
function normalizeChannelKey($channelName)
{
$channelName = preg_replace('/[^\w]/', '', $channelName);
return strtolower($channelName);
}
function normalizeChannelName($channelName)
{
$channelName = trim($channelName, ':');
$channelName = trim($channelName);
if (strlen($channelName) == 0) {
$channelName = 'Unknown';
}
return $channelName;
}
function validUrl($url)
{
if (filter_var($url, FILTER_VALIDATE_URL)) {
$components = parse_url($url);
// enable caching
static $urlCache = [];
$cacheKey = $components['scheme'] . $components['host'] . @$components['port'];
if (isset($urlCache[$cacheKey])) {
echo "Returning response for $url from cache." . PHP_EOL;
return $urlCache[$cacheKey];
}
$live = pingPort($components['host'], $components['scheme'], @$components['port']);
$urlCache[$cacheKey] = $live;
if ($live) {
return true;
}
echo "Url $url is not live." . PHP_EOL;
}
return false;
}
// vlc returns 0 even if a stream is broken, so we parse the output instead
function validStream($url)
{
$context = stream_context_create([
'http' => [
'timeout' => 5,
],
]);
$size = 1024;
$section = file_get_contents($url, false, $context, 0, $size);
return strlen($section) === $size;
}
function writePlaylist($array, $filename = 'output.m3u')
{
if (!count($array)) {
echo 'No channels available.' . PHP_EOL;
return;
}
$fp = fopen($filename, 'w');
fwrite($fp, "#EXTM3U\n");
foreach ($array as $channel) {
fwrite($fp, $channel['entry'] . "\n");
}
fclose($fp);
}
function pingPort($host, $proto, $port = null)
{
if ($port === null) {
switch ($proto) {
case 'http':
$port = 80;
break;
case 'https':
$port = 443;
break;
case 'rtmp':
$port = 1935;
break;
case 'rtsp':
$port = 554;
break;
default:
break;
}
}
return (bool) fsockopen($host, $port, $errno, $errstr, 5);
}