Skip to content

Commit da54f29

Browse files
author
boyska
committed
extended M3U formatter
1 parent d5db8f2 commit da54f29

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

Diff for: formats/M3uFormat.php

+52-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,65 @@ public function stringify()
1414

1515
foreach ($this->getItems() as $item) {
1616
$itemArray = $item->toArray();
17-
var_dump($itemArray);
17+
$m3uitem = new M3uItem();
1818
// if (!empty($itemTitle)) {
1919
// $entryTitle = $document->createElement('title');
2020
// $entry->appendChild($entryTitle);
2121
// $entryTitle->appendChild($document->createTextNode($itemTitle));
2222
// }
23-
if (isset($itemArray['itunes']) && isset($itemArray['enclosure'])) {
24-
$contents .= $itemArray['enclosure']['url'] . "\n";
23+
24+
if (isset($itemArray['enclosure'])) {
25+
$m3uitem->url = $itemArray['enclosure']['url'];
26+
$m3uitem->bytes = $itemArray['enclosure']['length'];
27+
}
28+
if (isset($itemArray['itunes']) && isset($itemArray['itunes']['duration'])) {
29+
$m3uitem->duration = parse_duration($itemArray['itunes']['duration']);
30+
}
31+
if (isset($itemArray['title'])) {
32+
$m3uitem->title = $itemArray['title'];
2533
}
34+
$contents .= $m3uitem->render();
2635
}
2736
return mb_convert_encoding($contents, $this->getCharset(), 'UTF-8');
2837
}
2938
}
39+
40+
function parse_duration($duration_string)
41+
{
42+
$seconds = 0;
43+
$parts = explode(':', $duration_string);
44+
for ($i = 0; $i < count($parts); $i++) {
45+
$seconds += intval($parts[count($parts) - $i - 1]) * pow(60, $i);
46+
}
47+
return $seconds;
48+
}
49+
50+
class M3uItem
51+
{
52+
public $duration = null;
53+
public $title = null;
54+
public $url = null;
55+
public $bytes = null;
56+
57+
public function render()
58+
{
59+
if ($this->url === null) {
60+
return '';
61+
}
62+
$text = '';
63+
$commentParts = [];
64+
if ($this->duration !== null && $this->duration > 0) {
65+
$commentParts[] = $this->duration;
66+
}
67+
if ($this->title !== null) {
68+
$commentParts[] = $this->title;
69+
}
70+
71+
if (count($commentParts) !== 0) {
72+
$text .= '#EXTINF:' . implode(',', $commentParts) . "\n";
73+
}
74+
75+
$text .= $this->url . "\n";
76+
return $text;
77+
}
78+
}

0 commit comments

Comments
 (0)