2019-06-26 22:02:31 +03:00
|
|
|
package media
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-06-29 01:20:52 +03:00
|
|
|
"strings"
|
2019-06-29 06:50:59 +03:00
|
|
|
"time"
|
2019-06-26 22:02:31 +03:00
|
|
|
|
|
|
|
"github.com/dhowden/tag"
|
|
|
|
)
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// Video represents metadata for a single video.
|
2019-06-26 22:02:31 +03:00
|
|
|
type Video struct {
|
|
|
|
ID string
|
|
|
|
Title string
|
|
|
|
Album string
|
|
|
|
Description string
|
|
|
|
Thumb []byte
|
|
|
|
ThumbType string
|
|
|
|
Modified string
|
2019-06-29 19:16:01 +03:00
|
|
|
Size int64
|
2019-06-29 06:50:59 +03:00
|
|
|
Timestamp time.Time
|
2019-06-26 22:02:31 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// ParseVideo parses a video file's metadata and returns a Video.
|
2019-06-26 22:02:31 +03:00
|
|
|
func ParseVideo(path string) (*Video, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-29 01:20:52 +03:00
|
|
|
info, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-29 19:16:01 +03:00
|
|
|
size := info.Size()
|
2019-06-29 06:50:59 +03:00
|
|
|
timestamp := info.ModTime()
|
|
|
|
modified := timestamp.Format("2006-01-02 03:04 PM")
|
2019-06-29 01:20:52 +03:00
|
|
|
name := info.Name()
|
|
|
|
// ID is name without extension
|
|
|
|
idx := strings.LastIndex(name, ".")
|
|
|
|
if idx == -1 {
|
|
|
|
idx = len(name)
|
|
|
|
}
|
|
|
|
id := name[:idx]
|
2019-06-26 22:02:31 +03:00
|
|
|
m, err := tag.ReadFrom(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-29 01:20:52 +03:00
|
|
|
title := m.Title()
|
|
|
|
// Default title is filename
|
|
|
|
if title == "" {
|
|
|
|
title = name
|
|
|
|
}
|
2019-06-26 22:02:31 +03:00
|
|
|
v := &Video{
|
2019-06-29 01:20:52 +03:00
|
|
|
ID: id,
|
|
|
|
Title: title,
|
2019-06-26 22:02:31 +03:00
|
|
|
Album: m.Album(),
|
|
|
|
Description: m.Comment(),
|
2019-06-29 01:20:52 +03:00
|
|
|
Modified: modified,
|
2019-06-29 19:16:01 +03:00
|
|
|
Size: size,
|
2019-06-29 06:50:59 +03:00
|
|
|
Timestamp: timestamp,
|
2019-06-26 22:02:31 +03:00
|
|
|
}
|
|
|
|
// Add thumbnail (if exists)
|
|
|
|
p := m.Picture()
|
|
|
|
if p != nil {
|
|
|
|
v.Thumb = p.Data
|
|
|
|
v.ThumbType = p.MIMEType
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|