34 lines
555 B
Go
34 lines
555 B
Go
|
package m3u
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
type Playlist struct {
|
||
|
items []*PlaylistItem
|
||
|
}
|
||
|
|
||
|
type PlaylistItem struct {
|
||
|
Name string
|
||
|
Path string
|
||
|
Time int
|
||
|
}
|
||
|
|
||
|
func (p Playlist) WriteTo(w io.Writer) (int64, error) {
|
||
|
var buf bytes.Buffer
|
||
|
buf.Write([]byte("#EXTM3U\n"))
|
||
|
for i, item := range p.items {
|
||
|
buf.WriteString(fmt.Sprintf("#EXTINF:%d,%s\n%s", item.Time, item.Name, item.Path))
|
||
|
if i+1 < len(p.items) {
|
||
|
buf.WriteString("\n")
|
||
|
}
|
||
|
}
|
||
|
return io.Copy(w, &buf)
|
||
|
}
|
||
|
|
||
|
func (p *Playlist) Add(item *PlaylistItem) {
|
||
|
p.items = append(p.items, item)
|
||
|
}
|