2 $youtubedlPath = '/usr/bin/youtube-dl';
3 $dreamboxHost = 'dreambox';
5 $cfgFile = __DIR__ . '/../data/config.php';
6 if (file_exists($cfgFile)) {
10 $pageUrl = getPageUrl();
11 $videoUrl = extractVideoUrl($pageUrl, $youtubedlPath);
12 if (php_sapi_name() == 'cli') {
13 echo $videoUrl . "\n";
15 header('Video-URL: ' . $videoUrl);
17 playVideoOnDreambox($videoUrl, $dreamboxHost);
22 if (php_sapi_name() == 'cli') {
24 errorInput('No URL given as command line parameter');
28 if (!isset($_SERVER['CONTENT_TYPE'])) {
29 errorInput('Content type header missing');
30 } else if ($_SERVER['CONTENT_TYPE'] != 'text/plain') {
31 errorInput('Content type is not text/plain but ' . $_SERVER['CONTENT_TYPE']);
33 $pageUrl = file_get_contents('php://input');
35 $parts = parse_url($pageUrl);
36 if ($parts === false) {
37 errorInput('Invalid URL in POST data');
38 } else if (!isset($parts['scheme'])) {
39 errorInput('Invalid URL in POST data: No scheme');
40 } else if ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https') {
41 errorInput('Invalid URL in POST data: Non-HTTP scheme');
46 function extractVideoUrl($pageUrl, $youtubedlPath)
51 . ' ' . escapeshellarg($pageUrl)
54 $lastLine = exec($cmd, $output, $exitCode);
55 if ($exitCode !== 0) {
56 if (strpos($lastLine, 'Unsupported URL') !== false) {
58 'Unsupported URL at ' . $pageUrl,
59 '406 Unsupported URL (No video found)'
62 errorOut('youtube-dl error: ' . $lastLine);
66 $json = implode("\n", $output);
67 $data = json_decode($json);
70 foreach ($data->formats as $format) {
71 if (strpos($format->format, 'hls') !== false) {
72 //dreambox 7080hd does not play hls files
75 if ($format->protocol == 'http_dash_segments') {
76 //split up into multiple small files
83 //use URL chosen by youtube-dl
90 '406 No video URL found'
96 function playVideoOnDreambox($videoUrl, $dreamboxHost)
98 ini_set('track_errors', 1);
99 $xml = @file_get_contents('http://' . $dreamboxHost . '/web/session');
100 if ($xml === false) {
101 errorOut('Failed to fetch dreambox session token: ' . $php_errormsg);
103 $sx = simplexml_load_string($xml);
104 $token = (string) $sx;
106 $playUrl = 'http://' . $dreamboxHost
107 . '/web/mediaplayerplay'
108 . '?file=4097:0:1:0:0:0:0:0:0:0:'
109 . str_replace('%3A', '%253A', rawurlencode($videoUrl));
111 $ctx = stream_context_create(
115 'header' => 'Content-type: application/x-www-form-urlencoded',
116 'content' => 'sessionid=' . $token,
117 //'ignore_errors' => true
121 $ret = file_get_contents($playUrl, false, $ctx);
122 if ($ret !== false) {
123 header('HTTP/1.0 200 OK');
124 echo "Video play request sent to dreambox\n";
128 'Failed to send video play request to dreambox: ' . $php_errormsg
133 function errorInput($msg)
135 header('HTTP/1.0 400 Bad Request');
136 header('Content-type: text/plain');
141 function errorOut($msg, $httpStatus = '500 Internal Server Error')
143 header('HTTP/1.0 ' . $httpStatus);
144 header('Content-type: text/plain');
146 syslog(LOG_ERR, 'playVideoOnDreamboxProxy: ' . $httpStatus . ': ' . $msg);