Refactored thumbnail generation with configurable timeout and in-sycn with video transcoding

This commit is contained in:
James Mills 2020-03-26 14:34:19 +10:00
parent 7fa7a38167
commit 8727285bc5
No known key found for this signature in database
GPG key ID: AC4C014F1440EBD6
5 changed files with 57 additions and 32 deletions

View file

@ -241,6 +241,8 @@ func (a *App) uploadHandler(w http.ResponseWriter, r *http.Request) {
a.Library.Paths[collection].Path, a.Library.Paths[collection].Path,
fmt.Sprintf("%s.mp4", shortuuid.New()), fmt.Sprintf("%s.mp4", shortuuid.New()),
) )
thumbFn1 := fmt.Sprintf("%s.jpg", strings.TrimSuffix(tf.Name(), filepath.Ext(tf.Name())))
thumbFn2 := fmt.Sprintf("%s.jpg", strings.TrimSuffix(vf, filepath.Ext(vf)))
// TODO: Use a proper Job Queue and make this async // TODO: Use a proper Job Queue and make this async
if err := utils.RunCmd( if err := utils.RunCmd(
@ -260,6 +262,27 @@ func (a *App) uploadHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if err := utils.RunCmd(
a.Config.Thumbnailer.Timeout,
"mt",
"-b",
"-s",
"-n", "1",
tf.Name(),
); err != nil {
err := fmt.Errorf("error generating thumbnail: %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := os.Rename(thumbFn1, thumbFn2); err != nil {
err := fmt.Errorf("error renaming generated thumbnail: %w", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := os.Rename(tf.Name(), vf); err != nil { if err := os.Rename(tf.Name(), vf); err != nil {
err := fmt.Errorf("error renaming transcoded video: %w", err) err := fmt.Errorf("error renaming transcoded video: %w", err)
log.Error(err) log.Error(err)

View file

@ -9,6 +9,7 @@ import (
type Config struct { type Config struct {
Library []*PathConfig `json:"library"` Library []*PathConfig `json:"library"`
Server *ServerConfig `json:"server"` Server *ServerConfig `json:"server"`
Thumbnailer *ThumbnailerConfig `json:"thumbnailer"`
Transcoder *TranscoderConfig `json:"transcoder"` Transcoder *TranscoderConfig `json:"transcoder"`
Feed *FeedConfig `json:"feed"` Feed *FeedConfig `json:"feed"`
} }
@ -27,6 +28,11 @@ type ServerConfig struct {
UploadPath string `json:"upload_path"` UploadPath string `json:"upload_path"`
} }
// ThumbnailerConfig settings for Transcoder
type ThumbnailerConfig struct {
Timeout int `json:"timeout"`
}
// TranscoderConfig settings for Transcoder // TranscoderConfig settings for Transcoder
type TranscoderConfig struct { type TranscoderConfig struct {
Timeout int `json:"timeout"` Timeout int `json:"timeout"`

File diff suppressed because one or more lines are too long

View file

@ -11,8 +11,11 @@
"store_path": "tube.db", "store_path": "tube.db",
"upload_path": "uploads" "upload_path": "uploads"
}, },
"thumbnailer": {
"timeout": 60
},
"transcoder": { "transcoder": {
"timeout": 120 "timeout": 300
}, },
"feed": { "feed": {
"external_url": "", "external_url": "",

View file

@ -9,9 +9,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/apex/log"
"github.com/dhowden/tag" "github.com/dhowden/tag"
"github.com/wybiral/tube/utils" "github.com/wybiral/tube/utils"
) )
@ -80,15 +78,9 @@ func ParseVideo(p *Path, name string) (*Video, error) {
if pic != nil { if pic != nil {
v.Thumb = pic.Data v.Thumb = pic.Data
v.ThumbType = pic.MIMEType v.ThumbType = pic.MIMEType
} else if utils.CmdExists("mt") { } else {
thumbFn := fmt.Sprintf("%s.jpg", strings.TrimSuffix(pth, filepath.Ext(pth))) thumbFn := fmt.Sprintf("%s.jpg", strings.TrimSuffix(pth, filepath.Ext(pth)))
if !utils.FileExists(thumbFn) { if utils.FileExists(thumbFn) {
if err := utils.RunCmd(30, "mt", "-b", "-s", "-n", "1", pth); err != nil {
err := fmt.Errorf("error generating thumbnail: %w", err)
log.Error(err.Error())
return nil, err
}
}
data, err := ioutil.ReadFile(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 { if err != nil {
return nil, err return nil, err
@ -96,6 +88,7 @@ func ParseVideo(p *Path, name string) (*Video, error) {
v.Thumb = data v.Thumb = data
v.ThumbType = "image/jpeg" v.ThumbType = "image/jpeg"
} }
}
return v, nil return v, nil
} }