stash/pkg/manager/task_generate_sprite.go

63 lines
1.7 KiB
Go
Raw Normal View History

2019-02-10 10:57:56 +00:00
package manager
import (
"github.com/remeh/sizedwaitgroup"
2019-02-14 23:42:52 +00:00
"github.com/stashapp/stash/pkg/ffmpeg"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/utils"
2019-02-10 10:57:56 +00:00
)
type GenerateSpriteTask struct {
2020-08-06 01:21:14 +00:00
Scene models.Scene
Overwrite bool
fileNamingAlgorithm models.HashAlgorithm
2019-02-10 10:57:56 +00:00
}
func (t *GenerateSpriteTask) Start(wg *sizedwaitgroup.SizedWaitGroup) {
2019-02-10 20:15:36 +00:00
defer wg.Done()
2020-08-06 01:21:14 +00:00
if !t.Overwrite && !t.required() {
2019-02-10 10:57:56 +00:00
return
}
videoFile, err := ffmpeg.NewVideoFile(instance.FFProbePath, t.Scene.Path, false)
2019-02-10 10:57:56 +00:00
if err != nil {
logger.Errorf("error reading video file: %s", err.Error())
return
}
2020-08-06 01:21:14 +00:00
sceneHash := t.Scene.GetHash(t.fileNamingAlgorithm)
imagePath := instance.Paths.Scene.GetSpriteImageFilePath(sceneHash)
vttPath := instance.Paths.Scene.GetSpriteVttFilePath(sceneHash)
generator, err := NewSpriteGenerator(*videoFile, sceneHash, imagePath, vttPath, 9, 9)
2019-02-10 10:57:56 +00:00
if err != nil {
logger.Errorf("error creating sprite generator: %s", err.Error())
return
}
generator.Overwrite = t.Overwrite
2019-02-10 10:57:56 +00:00
if err := generator.Generate(); err != nil {
logger.Errorf("error generating sprite: %s", err.Error())
return
}
}
2020-08-06 01:21:14 +00:00
// required returns true if the sprite needs to be generated
func (t GenerateSpriteTask) required() bool {
sceneHash := t.Scene.GetHash(t.fileNamingAlgorithm)
return !t.doesSpriteExist(sceneHash)
}
2019-02-10 10:57:56 +00:00
func (t *GenerateSpriteTask) doesSpriteExist(sceneChecksum string) bool {
2020-08-06 01:21:14 +00:00
if sceneChecksum == "" {
return false
}
2019-02-10 10:57:56 +00:00
imageExists, _ := utils.FileExists(instance.Paths.Scene.GetSpriteImageFilePath(sceneChecksum))
vttExists, _ := utils.FileExists(instance.Paths.Scene.GetSpriteVttFilePath(sceneChecksum))
return imageExists && vttExists
}