5 if (php_sapi_name() == 'cli') {
7 errorInput('No URL given as command line parameter');
11 if (!isset($_SERVER['CONTENT_TYPE'])) {
12 errorInput('Content type header missing');
13 } else if ($_SERVER['CONTENT_TYPE'] != 'text/plain') {
14 errorInput('Content type is not text/plain but ' . $_SERVER['CONTENT_TYPE']);
16 $pageUrl = file_get_contents('php://input');
18 $parts = parse_url($pageUrl);
19 if ($parts === false) {
20 errorInput('Invalid URL in POST data');
21 } else if (!isset($parts['scheme'])) {
22 errorInput('Invalid URL in POST data: No scheme');
23 } else if ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https') {
24 errorInput('Invalid URL in POST data: Non-HTTP scheme');
29 function getYoutubeDlJson($pageUrl, $youtubedlPath)
32 . ' --no-playlist'//would otherwise cause multiple json blocks
35 . ' ' . escapeshellarg($pageUrl)
38 $lastLine = exec($cmd, $output, $exitCode);
39 if ($exitCode !== 0) {
40 if (strpos($lastLine, 'Unsupported URL') !== false) {
42 'Unsupported URL at ' . $pageUrl,
43 '406 Unsupported URL (No video found)'
46 errorOut('youtube-dl error: ' . $lastLine);
50 $json = implode("\n", $output);
54 function extractVideoUrlFromJson($json)
56 $data = json_decode($json);
58 errorOut('Cannot decode JSON: ' . json_last_error_msg());
62 foreach ($data->formats as $format) {
63 if (strpos($format->format, 'hls') !== false) {
64 //dreambox 7080hd does not play hls files
67 if ($format->protocol == 'http_dash_segments') {
68 //split up into multiple small files
75 //use URL chosen by youtube-dl
82 '406 No video URL found'
88 function playVideoOnDreambox($videoUrl, $dreamboxHost)
90 ini_set('track_errors', 1);
91 $xml = @file_get_contents('http://' . $dreamboxHost . '/web/session');
93 errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
95 $sx = simplexml_load_string($xml);
96 $token = (string) $sx;
98 $playUrl = 'http://' . $dreamboxHost
99 . '/web/mediaplayerplay'
100 . '?file=4097:0:1:0:0:0:0:0:0:0:'
101 . str_replace('%3A', '%253A', rawurlencode($videoUrl));
103 $ctx = stream_context_create(
107 'header' => 'Content-type: application/x-www-form-urlencoded',
108 'content' => 'sessionid=' . $token,
109 //'ignore_errors' => true
113 $ret = file_get_contents($playUrl, false, $ctx);
114 if ($ret !== false) {
115 if (php_sapi_name() != 'cli') {
116 header('HTTP/1.0 200 OK');
118 echo "Video play request sent to dreambox\n";
122 'Failed to send video play request to dreambox: ' . $php_errormsg