From e1090a7583cfbb75fa03eb56d2b26d68732282cf Mon Sep 17 00:00:00 2001 From: davy Date: Wed, 26 Jun 2019 14:02:31 -0500 Subject: import code --- pkg/media/library.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ pkg/media/playlist.go | 15 ++++++++++++++ pkg/media/video.go | 40 ++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 pkg/media/library.go create mode 100644 pkg/media/playlist.go create mode 100644 pkg/media/video.go (limited to 'pkg') 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 +} -- cgit v1.2.3