2021-12-13 02:41:07 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
"github.com/stashapp/stash/pkg/file/video"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/fsutil"
|
2021-12-13 02:41:07 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GenerateInteractiveHeatmapSpeedTask struct {
|
|
|
|
Scene models.Scene
|
|
|
|
Overwrite bool
|
|
|
|
fileNamingAlgorithm models.HashAlgorithm
|
2022-07-13 06:30:54 +00:00
|
|
|
TxnManager Repository
|
2021-12-13 02:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GenerateInteractiveHeatmapSpeedTask) GetDescription() string {
|
2022-07-13 06:30:54 +00:00
|
|
|
return fmt.Sprintf("Generating heatmap and speed for %s", t.Scene.Path())
|
2021-12-13 02:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GenerateInteractiveHeatmapSpeedTask) Start(ctx context.Context) {
|
|
|
|
if !t.shouldGenerate() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
videoChecksum := t.Scene.GetHash(t.fileNamingAlgorithm)
|
2022-07-13 06:30:54 +00:00
|
|
|
funscriptPath := video.GetFunscriptPath(t.Scene.Path())
|
2021-12-13 02:41:07 +00:00
|
|
|
heatmapPath := instance.Paths.Scene.GetInteractiveHeatmapPath(videoChecksum)
|
|
|
|
|
|
|
|
generator := NewInteractiveHeatmapSpeedGenerator(funscriptPath, heatmapPath)
|
|
|
|
|
|
|
|
err := generator.Generate()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("error generating heatmap: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
median := generator.InteractiveSpeed
|
2021-12-13 02:41:07 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := t.TxnManager.WithTxn(ctx, func(ctx context.Context) error {
|
2022-07-13 06:30:54 +00:00
|
|
|
primaryFile := t.Scene.PrimaryFile()
|
|
|
|
primaryFile.InteractiveSpeed = &median
|
|
|
|
qb := t.TxnManager.File
|
|
|
|
return qb.Update(ctx, primaryFile)
|
2021-12-13 02:41:07 +00:00
|
|
|
}); err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GenerateInteractiveHeatmapSpeedTask) shouldGenerate() bool {
|
2022-07-13 06:30:54 +00:00
|
|
|
primaryFile := t.Scene.PrimaryFile()
|
|
|
|
if primaryFile == nil || !primaryFile.Interactive {
|
2021-12-13 02:41:07 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
sceneHash := t.Scene.GetHash(t.fileNamingAlgorithm)
|
|
|
|
return !t.doesHeatmapExist(sceneHash) || t.Overwrite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GenerateInteractiveHeatmapSpeedTask) doesHeatmapExist(sceneChecksum string) bool {
|
|
|
|
if sceneChecksum == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
imageExists, _ := fsutil.FileExists(instance.Paths.Scene.GetInteractiveHeatmapPath(sceneChecksum))
|
2021-12-13 02:41:07 +00:00
|
|
|
return imageExists
|
|
|
|
}
|