2019-06-26 22:02:31 +03:00
|
|
|
package media
|
|
|
|
|
|
|
|
import (
|
2020-03-21 03:46:03 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2022-11-25 04:29:44 +03:00
|
|
|
"log"
|
2019-06-26 22:02:31 +03:00
|
|
|
"os"
|
2019-07-03 21:25:42 +03:00
|
|
|
"path"
|
2020-03-21 03:46:03 +03:00
|
|
|
"path/filepath"
|
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
|
|
|
|
2021-07-13 01:34:56 +03:00
|
|
|
"git.mills.io/prologic/tube/utils"
|
2022-11-25 04:29:44 +03:00
|
|
|
"github.com/dhowden/tag"
|
|
|
|
"gopkg.in/yaml.v3"
|
2019-06-26 22:02:31 +03:00
|
|
|
)
|
|
|
|
|
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-07-03 21:25:42 +03:00
|
|
|
Path string
|
2019-06-29 06:50:59 +03:00
|
|
|
Timestamp time.Time
|
2020-03-25 05:40:00 +03:00
|
|
|
|
|
|
|
Views int64
|
2019-06-26 22:02:31 +03:00
|
|
|
}
|
|
|
|
|
2022-11-25 04:29:44 +03:00
|
|
|
func getTagsFromYml(v *Video) error {
|
|
|
|
ymlFileName := fmt.Sprintf("%s.yml", strings.TrimSuffix(v.Path, filepath.Ext(v.Path)))
|
|
|
|
if !utils.FileExists(ymlFileName) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ymlFile, err := ioutil.ReadFile(ymlFileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(ymlFile, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Got tags from yml for", v.Path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-30 01:02:05 +03:00
|
|
|
// ParseVideo parses a video file's metadata and returns a Video.
|
2019-07-03 21:25:42 +03:00
|
|
|
func ParseVideo(p *Path, name string) (*Video, error) {
|
|
|
|
pth := path.Join(p.Path, name)
|
|
|
|
f, err := os.Open(pth)
|
2019-06-26 22:02:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-09 00:13:55 +03:00
|
|
|
defer f.Close()
|
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
|
|
|
// ID is name without extension
|
|
|
|
idx := strings.LastIndex(name, ".")
|
|
|
|
if idx == -1 {
|
|
|
|
idx = len(name)
|
|
|
|
}
|
|
|
|
id := name[:idx]
|
2019-07-03 21:25:42 +03:00
|
|
|
if len(p.Prefix) > 0 {
|
|
|
|
// if there's a prefix prepend it to the ID
|
|
|
|
id = path.Join(p.Prefix, 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 == "" {
|
2020-03-27 13:16:16 +03:00
|
|
|
title = strings.TrimSuffix(name, filepath.Ext(name))
|
2019-06-29 01:20:52 +03:00
|
|
|
}
|
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-07-03 21:25:42 +03:00
|
|
|
Path: pth,
|
2019-06-29 06:50:59 +03:00
|
|
|
Timestamp: timestamp,
|
2019-06-26 22:02:31 +03:00
|
|
|
}
|
2022-11-25 04:29:44 +03:00
|
|
|
// read yml if exists
|
|
|
|
err = getTagsFromYml(v)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to read yml for", v.Path)
|
|
|
|
}
|
|
|
|
|
2023-01-04 14:34:40 +03:00
|
|
|
// Add thumbnail from embedded tags (if exists)
|
2019-07-03 21:25:42 +03:00
|
|
|
pic := m.Picture()
|
|
|
|
if pic != nil {
|
|
|
|
v.Thumb = pic.Data
|
|
|
|
v.ThumbType = pic.MIMEType
|
2023-01-04 14:34:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add thumbnail from external file (if exists)
|
|
|
|
if utils.FileExists(fmt.Sprintf("%s.jpg", strings.TrimSuffix(pth, filepath.Ext(pth)))) {
|
|
|
|
data, err := ioutil.ReadFile(fmt.Sprintf("%s.jpg", strings.TrimSuffix(pth, filepath.Ext(pth))))
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to read thumbnail file for", v.Path)
|
|
|
|
return nil, err
|
2020-03-21 03:46:03 +03:00
|
|
|
}
|
2023-01-04 14:34:40 +03:00
|
|
|
v.Thumb = data
|
|
|
|
v.ThumbType = "image/jpeg"
|
2019-06-26 22:02:31 +03:00
|
|
|
}
|
2020-03-21 03:46:03 +03:00
|
|
|
|
2019-06-26 22:02:31 +03:00
|
|
|
return v, nil
|
|
|
|
}
|