Skip to content

Commit

Permalink
extended M3U formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
boyska committed Mar 4, 2024
1 parent d5db8f2 commit da54f29
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions formats/M3uFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit da54f29

Please sign in to comment.