Refactored thumbnail generation with configurable timeout and in-sycn with video transcoding
This commit is contained in:
parent
7fa7a38167
commit
8727285bc5
5 changed files with 57 additions and 32 deletions
23
app/app.go
23
app/app.go
|
@ -241,6 +241,8 @@ func (a *App) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||
a.Library.Paths[collection].Path,
|
||||
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
|
||||
if err := utils.RunCmd(
|
||||
|
@ -260,6 +262,27 @@ func (a *App) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||
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 {
|
||||
err := fmt.Errorf("error renaming transcoded video: %w", err)
|
||||
log.Error(err)
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
type Config struct {
|
||||
Library []*PathConfig `json:"library"`
|
||||
Server *ServerConfig `json:"server"`
|
||||
Thumbnailer *ThumbnailerConfig `json:"thumbnailer"`
|
||||
Transcoder *TranscoderConfig `json:"transcoder"`
|
||||
Feed *FeedConfig `json:"feed"`
|
||||
}
|
||||
|
@ -27,6 +28,11 @@ type ServerConfig struct {
|
|||
UploadPath string `json:"upload_path"`
|
||||
}
|
||||
|
||||
// ThumbnailerConfig settings for Transcoder
|
||||
type ThumbnailerConfig struct {
|
||||
Timeout int `json:"timeout"`
|
||||
}
|
||||
|
||||
// TranscoderConfig settings for Transcoder
|
||||
type TranscoderConfig struct {
|
||||
Timeout int `json:"timeout"`
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -11,8 +11,11 @@
|
|||
"store_path": "tube.db",
|
||||
"upload_path": "uploads"
|
||||
},
|
||||
"thumbnailer": {
|
||||
"timeout": 60
|
||||
},
|
||||
"transcoder": {
|
||||
"timeout": 120
|
||||
"timeout": 300
|
||||
},
|
||||
"feed": {
|
||||
"external_url": "",
|
||||
|
|
|
@ -9,9 +9,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/dhowden/tag"
|
||||
|
||||
"github.com/wybiral/tube/utils"
|
||||
)
|
||||
|
||||
|
@ -80,15 +78,9 @@ func ParseVideo(p *Path, name string) (*Video, error) {
|
|||
if pic != nil {
|
||||
v.Thumb = pic.Data
|
||||
v.ThumbType = pic.MIMEType
|
||||
} else if utils.CmdExists("mt") {
|
||||
} else {
|
||||
thumbFn := fmt.Sprintf("%s.jpg", strings.TrimSuffix(pth, filepath.Ext(pth)))
|
||||
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
|
||||
}
|
||||
}
|
||||
if utils.FileExists(thumbFn) {
|
||||
data, err := ioutil.ReadFile(fmt.Sprintf("%s.jpg", strings.TrimSuffix(pth, filepath.Ext(pth))))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -96,6 +88,7 @@ func ParseVideo(p *Path, name string) (*Video, error) {
|
|||
v.Thumb = data
|
||||
v.ThumbType = "image/jpeg"
|
||||
}
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue