mirror of
https://github.com/Yetangitu/ampache
synced 2025-10-05 10:49:37 +02:00
Add first Plex music transcode support
This commit is contained in:
parent
d502982f44
commit
2576210ef1
3 changed files with 79 additions and 18 deletions
|
@ -381,10 +381,24 @@ class Plex_Api
|
||||||
|
|
||||||
public static function replay_header($ch, $header)
|
public static function replay_header($ch, $header)
|
||||||
{
|
{
|
||||||
header($header);
|
$rheader = trim($header);
|
||||||
|
$rhpart = explode(':', $rheader);
|
||||||
|
if (!empty($rheader) && count($rhpart) > 1) {
|
||||||
|
if ($rhpart[0] != "Transfer-Encoding") {
|
||||||
|
header($rheader);
|
||||||
|
}
|
||||||
|
}
|
||||||
return strlen($header);
|
return strlen($header);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function replay_body($ch, $data)
|
||||||
|
{
|
||||||
|
echo $data;
|
||||||
|
ob_flush();
|
||||||
|
|
||||||
|
return strlen($data);
|
||||||
|
}
|
||||||
|
|
||||||
protected static function myPlexRequest($action, $curlopts = array(), $headers = array(), $proxy = false)
|
protected static function myPlexRequest($action, $curlopts = array(), $headers = array(), $proxy = false)
|
||||||
{
|
{
|
||||||
$server = Plex_XML_Data::getServerUri();
|
$server = Plex_XML_Data::getServerUri();
|
||||||
|
@ -486,6 +500,17 @@ class Plex_Api
|
||||||
$height = $_REQUEST['height'];
|
$height = $_REQUEST['height'];
|
||||||
$url = $_REQUEST['url'];
|
$url = $_REQUEST['url'];
|
||||||
|
|
||||||
|
// Replace 32400 request port with the real listening port
|
||||||
|
// *** To `Plex Inc`: ***
|
||||||
|
// Please allow listening port server configuration for your Plex server
|
||||||
|
// and fix your clients to not request resources so hard-coded on 127.0.0.1:32400.
|
||||||
|
// May be ok on Apple & UPnP world but that's really ugly for a server...
|
||||||
|
// Yes, it's a little hack but it works.
|
||||||
|
$localrs = "http://127.0.0.1:32400/";
|
||||||
|
if (strpos($url, $localrs) !== false) {
|
||||||
|
$url = "http://127.0.0.1:" . Plex_XML_Data::getServerPort() . "/" . substr($url, strlen($localrs));
|
||||||
|
}
|
||||||
|
|
||||||
if ($width && $height && $url) {
|
if ($width && $height && $url) {
|
||||||
$request = Requests::get($url);
|
$request = Requests::get($url);
|
||||||
if ($request->status_code == 200) {
|
if ($request->status_code == 200) {
|
||||||
|
@ -504,9 +529,36 @@ class Plex_Api
|
||||||
|
|
||||||
public static function music($params)
|
public static function music($params)
|
||||||
{
|
{
|
||||||
$r = Plex_XML_Data::createPluginContainer();
|
if (count($params) > 2) {
|
||||||
Plex_XML_Data::setContainerSize($r);
|
if ($params[0] == ':' && $params[1] == 'transcode') {
|
||||||
self::apiOutput($r->asXML());
|
if (count($params) == 3) {
|
||||||
|
$format = $_REQUEST['format'] ?: pathinfo($params[2], PATHINFO_EXTENSION);
|
||||||
|
$url = $_REQUEST['url'];
|
||||||
|
$br = $_REQUEST['audioBitrate'];
|
||||||
|
if (preg_match("/\/parts\/([0-9]+)\//", $url, $matches)) {
|
||||||
|
$song_id = Plex_XML_Data::getAmpacheId($matches[1]);
|
||||||
|
}
|
||||||
|
} elseif (count($params) == 4 && $params[2] == 'universal') {
|
||||||
|
$format = pathinfo($params[3], PATHINFO_EXTENSION);
|
||||||
|
$path = $_REQUEST['path'];
|
||||||
|
// Should be the maximal allowed bitrate, not necessary the bitrate used but Ampache doesn't support this kind of option yet
|
||||||
|
$br = $_REQUEST['maxAudioBitrate'];
|
||||||
|
if (preg_match("/\/metadata\/([0-9]+)/", $path, $matches)) {
|
||||||
|
$song_id = Plex_XML_Data::getAmpacheId($matches[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($format) && !empty($song_id)) {
|
||||||
|
$urlparams = '&transcode_to=' . $format;
|
||||||
|
if (!empty($br)) {
|
||||||
|
$urlparams .= '&bitrate=' . $br;
|
||||||
|
}
|
||||||
|
|
||||||
|
$url = Song::play_url($song_id, $urlparams);
|
||||||
|
self::stream_url($url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function video($params)
|
public static function video($params)
|
||||||
|
@ -627,6 +679,26 @@ class Plex_Api
|
||||||
self::apiOutput($r->asXML());
|
self::apiOutput($r->asXML());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static function stream_url($url) {
|
||||||
|
// header("Location: " . $url);
|
||||||
|
set_time_limit(0);
|
||||||
|
|
||||||
|
$ch = curl_init($url);
|
||||||
|
curl_setopt_array($ch, array(
|
||||||
|
CURLOPT_HTTPHEADER => array("User-Agent: Plex"),
|
||||||
|
CURLOPT_HEADER => false,
|
||||||
|
CURLOPT_RETURNTRANSFER => false,
|
||||||
|
CURLOPT_FOLLOWLOCATION => true,
|
||||||
|
CURLOPT_WRITEFUNCTION => array('Plex_Api', 'replay_body'),
|
||||||
|
CURLOPT_HEADERFUNCTION => array('Plex_Api', 'replay_header'),
|
||||||
|
CURLOPT_SSL_VERIFYPEER => false,
|
||||||
|
CURLOPT_SSL_VERIFYHOST => false,
|
||||||
|
CURLOPT_TIMEOUT => 0
|
||||||
|
));
|
||||||
|
curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
}
|
||||||
|
|
||||||
public static function library_parts($params)
|
public static function library_parts($params)
|
||||||
{
|
{
|
||||||
$n = count($params);
|
$n = count($params);
|
||||||
|
@ -639,19 +711,7 @@ class Plex_Api
|
||||||
$song = new Song($id);
|
$song = new Song($id);
|
||||||
if ($song->id) {
|
if ($song->id) {
|
||||||
$url = Song::play_url($id);
|
$url = Song::play_url($id);
|
||||||
|
self::stream_url($url);
|
||||||
// header("Location: " . $url);
|
|
||||||
$ch = curl_init($url);
|
|
||||||
curl_setopt_array($ch, array(
|
|
||||||
CURLOPT_HEADER => false,
|
|
||||||
CURLOPT_RETURNTRANSFER => false,
|
|
||||||
CURLOPT_FOLLOWLOCATION => true,
|
|
||||||
CURLOPT_HEADERFUNCTION => array('Plex_Api', 'replay_header'),
|
|
||||||
CURLOPT_SSL_VERIFYPEER => false,
|
|
||||||
CURLOPT_SSL_VERIFYHOST => false
|
|
||||||
));
|
|
||||||
curl_exec($ch);
|
|
||||||
curl_close($ch);
|
|
||||||
} else {
|
} else {
|
||||||
self::createError(404);
|
self::createError(404);
|
||||||
}
|
}
|
||||||
|
|
|
@ -323,7 +323,7 @@ class Plex_XML_Data
|
||||||
$xml->addAttribute('requestParametersInCookie', '1');
|
$xml->addAttribute('requestParametersInCookie', '1');
|
||||||
$xml->addAttribute('sync', '1');
|
$xml->addAttribute('sync', '1');
|
||||||
$xml->addAttribute('transcoderActiveVideoSessions', '0');
|
$xml->addAttribute('transcoderActiveVideoSessions', '0');
|
||||||
$xml->addAttribute('transcoderAudio', '0');
|
$xml->addAttribute('transcoderAudio', '1');
|
||||||
$xml->addAttribute('transcoderVideo', '0');
|
$xml->addAttribute('transcoderVideo', '0');
|
||||||
|
|
||||||
$xml->addAttribute('updatedAt', self::getLastUpdate($catalogs));
|
$xml->addAttribute('updatedAt', self::getLastUpdate($catalogs));
|
||||||
|
|
|
@ -454,6 +454,7 @@ do {
|
||||||
: min(2048, $stream_size - $bytes_streamed);
|
: min(2048, $stream_size - $bytes_streamed);
|
||||||
$buf = fread($fp, $read_size);
|
$buf = fread($fp, $read_size);
|
||||||
print($buf);
|
print($buf);
|
||||||
|
ob_flush();
|
||||||
$bytes_streamed += strlen($buf);
|
$bytes_streamed += strlen($buf);
|
||||||
} while (!feof($fp) && (connection_status() == 0) && ($transcode || $bytes_streamed < $stream_size));
|
} while (!feof($fp) && (connection_status() == 0) && ($transcode || $bytes_streamed < $stream_size));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue