Created
February 27, 2011 13:52
-
-
Save Cifro/846196 to your computer and use it in GitHub Desktop.
Getting and parsing VLC playlist from VLC HTTP interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
<title>VLC playlist na webe</title> | |
<meta charset="utf-8"> | |
</head> | |
<pre> | |
<?php | |
class VLCPlaylist{ | |
/** @var array */ | |
public $ports = array( | |
'movies' => '8080', | |
'serials' => '8081', | |
'broadcast' => '8082', | |
); | |
/** @var string */ | |
public $ipAddress = '192.168.6.27'; | |
/** | |
* Gets playlist of desired type | |
* @param string Type of playlist. E.g. 'movies' | |
* @return SimpleXMLElement | false | |
*/ | |
public function getPlaylist($type) | |
{ | |
if(!isset($this->ports[$type])) | |
throw new Exception('Unknown type of playlist. Use one of these: ' . implode(', ', array_keys($this->ports))); | |
$playlist = $this->load($type); | |
if ($playlist === false) { | |
throw new Exception('Error during loading playlist. Probably VLC is down.'); | |
}else{ | |
return $playlist->node[0]; | |
} | |
} | |
/** | |
* Formats to human readable form of duration of track | |
* @param string|int Duration of track | |
* @return string Human readable duration of track in form hh:mm:ss | |
*/ | |
public function formatDuration($s) | |
{ | |
$s = (int) $s; | |
$s = $s / 1000000; | |
$hours = floor($s / 3600); | |
$minutes = floor(($s / 60) % 60); | |
$seconds = floor($s % 60); | |
if($hours < 10) $hours = "0" . $hours; | |
if($minutes < 10) $minutes = "0" . $minutes; | |
if($seconds < 10) $seconds = "0" . $seconds; | |
return $hours . ":" . $minutes . ":" . $seconds; | |
} | |
/** | |
* Loads playlist from server | |
* @param string Type of playlist. E.g. movies | |
* @param int Timeout in seconds for fscokopen() | |
* @return SimpleXmlElement|false | |
*/ | |
private function load($type, $timeout = 1) | |
{ | |
$path = '/requests/playlist.xml'; | |
$fp = @fsockopen($this->ipAddress, (int) $this->ports[$type], $errno, $errstr, $timeout); | |
if($fp !== false) { | |
// make request | |
$out = "GET {$path} HTTP/1.1\r\n"; | |
$out .= "Host: {$this->ipAddress}\r\n"; | |
$out .= "Connection: Close\r\n\r\n"; | |
fwrite($fp, $out); | |
// get response | |
$resp = ''; | |
while (!feof($fp)) { | |
$resp .= fgets($fp); | |
} | |
fclose($fp); | |
// check status is 200 | |
$status_regex = "/HTTP\/1\.\d\s(\d+)/"; | |
if(preg_match($status_regex, $resp, $matches) && $matches[1] == '200') { | |
$parts = explode('<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>', $resp); | |
return @simplexml_load_string($parts[1], "SimpleXMLElement", LIBXML_NOCDATA); | |
} | |
}else{ | |
return false; | |
} | |
} | |
} | |
function output($vlcPlaylist, $playlistType) | |
{ | |
$out = ''; | |
// VLC 0.8.6d | |
if(!empty($playlistType->node)){ | |
foreach($playlistType->node->leaf as $leaf){ | |
$title = $leaf['name']; | |
$duration = $vlcPlaylist->formatDuration($leaf['duration']); | |
if(isset($leaf['current'])){ | |
$out .= "<strong>Aktualne beží: [{$duration}] {$title} </strong>\n"; | |
}else{ | |
$out .= " [{$duration}] {$title}\n"; | |
} | |
} | |
}else{ | |
foreach($playlistType->leaf as $leaf){ | |
$title = !empty($leaf->title) ? $leaf->title : $leaf['name']; | |
$duration = $vlcPlaylist->formatDuration($leaf['duration']); | |
if(isset($leaf['current'])){ | |
$out .= "<strong>Aktualne beží: [{$duration}] {$title} </strong>\n"; | |
}else{ | |
$out .= " [{$duration}] {$title}\n"; | |
} | |
} | |
} | |
return $out; | |
} | |
$vlcPlaylist = new VLCPlaylist; | |
echo "<h2>Vysielanie</h2>"; | |
try{ | |
$broadcast = $vlcPlaylist->getPlaylist('broadcast'); | |
echo output($vlcPlaylist, $broadcast); | |
}catch(Exception $e){ | |
echo $e->getMessage(); | |
} | |
echo "<h2>Filmy</h2>"; | |
try{ | |
$movies = $vlcPlaylist->getPlaylist('movies'); | |
echo output($vlcPlaylist, $movies); | |
}catch(Exception $e){ | |
echo $e->getMessage(); | |
} | |
echo "<h2>Serialy</h2>"; | |
try{ | |
$serials = $vlcPlaylist->getPlaylist('serials'); | |
echo output($vlcPlaylist, $serials); | |
}catch(Exception $e){ | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment