summaryrefslogtreecommitdiff
path: root/pkg/media
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/media')
-rw-r--r--pkg/media/library.go56
-rw-r--r--pkg/media/playlist.go15
-rw-r--r--pkg/media/video.go40
3 files changed, 111 insertions, 0 deletions
diff --git a/pkg/media/library.go b/pkg/media/library.go
new file mode 100644
index 0000000..b18fe3a
--- /dev/null
+++ b/pkg/media/library.go
@@ -0,0 +1,56 @@
+package media
+
+import (
+ "io/ioutil"
+ "sort"
+ "strings"
+)
+
+type Library struct {
+ Videos map[string]*Video
+}
+
+func NewLibrary() *Library {
+ lib := &Library{
+ Videos: make(map[string]*Video),
+ }
+ return lib
+}
+
+func (lib *Library) Import(path string) error {
+ files, err := ioutil.ReadDir(path)
+ if err != nil {
+ return err
+ }
+ for _, info := range files {
+ name := info.Name()
+ v, err := ParseVideo(path + "/" + name)
+ if err != nil {
+ // Ignore files that can't be parsed
+ continue
+ }
+ // Set modified date property
+ v.Modified = info.ModTime().Format("2006-01-02")
+ // Default title is filename
+ if v.Title == "" {
+ v.Title = name
+ }
+ // ID is name without extension
+ idx := strings.LastIndex(name, ".")
+ if idx == -1 {
+ idx = len(name)
+ }
+ v.ID = name[:idx]
+ lib.Videos[v.ID] = v
+ }
+ return nil
+}
+
+func (lib *Library) Playlist() Playlist {
+ pl := make(Playlist, 0)
+ for _, v := range lib.Videos {
+ pl = append(pl, v)
+ }
+ sort.Sort(pl)
+ return pl
+}
diff --git a/pkg/media/playlist.go b/pkg/media/playlist.go
new file mode 100644
index 0000000..ed22f27
--- /dev/null
+++ b/pkg/media/playlist.go
@@ -0,0 +1,15 @@
+package media
+
+type Playlist []*Video
+
+func (p Playlist) Len() int {
+ return len(p)
+}
+
+func (p Playlist) Swap(i, j int) {
+ p[i], p[j] = p[j], p[i]
+}
+
+func (p Playlist) Less(i, j int) bool {
+ return p[i].ID < p[j].ID
+}
diff --git a/pkg/media/video.go b/pkg/media/video.go
new file mode 100644
index 0000000..bd0def8
--- /dev/null
+++ b/pkg/media/video.go
@@ -0,0 +1,40 @@
+package media
+
+import (
+ "os"
+
+ "github.com/dhowden/tag"
+)
+
+type Video struct {
+ ID string
+ Title string
+ Album string
+ Description string
+ Thumb []byte
+ ThumbType string
+ Modified string
+}
+
+func ParseVideo(path string) (*Video, error) {
+ f, err := os.Open(path)
+ if err != nil {
+ return nil, err
+ }
+ m, err := tag.ReadFrom(f)
+ if err != nil {
+ return nil, err
+ }
+ v := &Video{
+ Title: m.Title(),
+ Album: m.Album(),
+ Description: m.Comment(),
+ }
+ // Add thumbnail (if exists)
+ p := m.Picture()
+ if p != nil {
+ v.Thumb = p.Data
+ v.ThumbType = p.MIMEType
+ }
+ return v, nil
+}