summaryrefslogtreecommitdiff
path: root/media/video.go
diff options
context:
space:
mode:
authorJames Mills <prologic@shortcircuit.net.au>2020-03-21 09:55:06 +1000
committerJames Mills <prologic@shortcircuit.net.au>2020-03-21 09:55:06 +1000
commit4dae45a68ff1ff32a251a5835b2a2723ae125d1c (patch)
treeb3abe641f39f50ac444aee2bb617d7b4ab44187d /media/video.go
parent76fa613b7eaf1c8626d2a51901c623af7bf1a665 (diff)
Restructured source code layout, Added rice support for templates and static assets
Diffstat (limited to 'media/video.go')
-rw-r--r--media/video.go77
1 files changed, 77 insertions, 0 deletions
diff --git a/media/video.go b/media/video.go
new file mode 100644
index 0000000..9340e86
--- /dev/null
+++ b/media/video.go
@@ -0,0 +1,77 @@
+package media
+
+import (
+ "os"
+ "path"
+ "strings"
+ "time"
+
+ "github.com/dhowden/tag"
+)
+
+// Video represents metadata for a single video.
+type Video struct {
+ ID string
+ Title string
+ Album string
+ Description string
+ Thumb []byte
+ ThumbType string
+ Modified string
+ Size int64
+ Path string
+ Timestamp time.Time
+}
+
+// ParseVideo parses a video file's metadata and returns a Video.
+func ParseVideo(p *Path, name string) (*Video, error) {
+ pth := path.Join(p.Path, name)
+ f, err := os.Open(pth)
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+ info, err := f.Stat()
+ if err != nil {
+ return nil, err
+ }
+ size := info.Size()
+ timestamp := info.ModTime()
+ modified := timestamp.Format("2006-01-02 03:04 PM")
+ // ID is name without extension
+ idx := strings.LastIndex(name, ".")
+ if idx == -1 {
+ idx = len(name)
+ }
+ id := name[:idx]
+ if len(p.Prefix) > 0 {
+ // if there's a prefix prepend it to the ID
+ id = path.Join(p.Prefix, name[:idx])
+ }
+ m, err := tag.ReadFrom(f)
+ if err != nil {
+ return nil, err
+ }
+ title := m.Title()
+ // Default title is filename
+ if title == "" {
+ title = name
+ }
+ v := &Video{
+ ID: id,
+ Title: title,
+ Album: m.Album(),
+ Description: m.Comment(),
+ Modified: modified,
+ Size: size,
+ Path: pth,
+ Timestamp: timestamp,
+ }
+ // Add thumbnail (if exists)
+ pic := m.Picture()
+ if pic != nil {
+ v.Thumb = pic.Data
+ v.ThumbType = pic.MIMEType
+ }
+ return v, nil
+}