diff --git a/formats/M3uFormat.php b/formats/M3uFormat.php index adfdebc6854a..1d408c9ecce8 100644 --- a/formats/M3uFormat.php +++ b/formats/M3uFormat.php @@ -14,16 +14,65 @@ public function stringify() foreach ($this->getItems() as $item) { $itemArray = $item->toArray(); - var_dump($itemArray); + $m3uitem = new M3uItem(); // if (!empty($itemTitle)) { // $entryTitle = $document->createElement('title'); // $entry->appendChild($entryTitle); // $entryTitle->appendChild($document->createTextNode($itemTitle)); // } - if (isset($itemArray['itunes']) && isset($itemArray['enclosure'])) { - $contents .= $itemArray['enclosure']['url'] . "\n"; + + if (isset($itemArray['enclosure'])) { + $m3uitem->url = $itemArray['enclosure']['url']; + $m3uitem->bytes = $itemArray['enclosure']['length']; + } + if (isset($itemArray['itunes']) && isset($itemArray['itunes']['duration'])) { + $m3uitem->duration = parse_duration($itemArray['itunes']['duration']); + } + if (isset($itemArray['title'])) { + $m3uitem->title = $itemArray['title']; } + $contents .= $m3uitem->render(); } return mb_convert_encoding($contents, $this->getCharset(), 'UTF-8'); } } + +function parse_duration($duration_string) +{ + $seconds = 0; + $parts = explode(':', $duration_string); + for ($i = 0; $i < count($parts); $i++) { + $seconds += intval($parts[count($parts) - $i - 1]) * pow(60, $i); + } + return $seconds; +} + +class M3uItem +{ + public $duration = null; + public $title = null; + public $url = null; + public $bytes = null; + + public function render() + { + if ($this->url === null) { + return ''; + } + $text = ''; + $commentParts = []; + if ($this->duration !== null && $this->duration > 0) { + $commentParts[] = $this->duration; + } + if ($this->title !== null) { + $commentParts[] = $this->title; + } + + if (count($commentParts) !== 0) { + $text .= '#EXTINF:' . implode(',', $commentParts) . "\n"; + } + + $text .= $this->url . "\n"; + return $text; + } +}