2021-04-11 23:04:40 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2021-10-22 23:48:42 +00:00
|
|
|
"fmt"
|
2021-04-11 23:04:40 +00:00
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
"github.com/stashapp/stash/pkg/hash/videophash"
|
2021-04-11 23:04:40 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GeneratePhashTask struct {
|
|
|
|
Scene models.Scene
|
2021-08-11 06:08:10 +00:00
|
|
|
Overwrite bool
|
2021-04-11 23:04:40 +00:00
|
|
|
fileNamingAlgorithm models.HashAlgorithm
|
2022-05-19 07:49:32 +00:00
|
|
|
txnManager models.Repository
|
2021-04-11 23:04:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 23:48:42 +00:00
|
|
|
func (t *GeneratePhashTask) GetDescription() string {
|
|
|
|
return fmt.Sprintf("Generating phash for %s", t.Scene.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GeneratePhashTask) Start(ctx context.Context) {
|
2021-04-11 23:04:40 +00:00
|
|
|
if !t.shouldGenerate() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-14 23:39:48 +00:00
|
|
|
ffprobe := instance.FFProbe
|
2022-04-18 00:50:10 +00:00
|
|
|
videoFile, err := ffprobe.NewVideoFile(t.Scene.Path)
|
2021-04-11 23:04:40 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("error reading video file: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
hash, err := videophash.Generate(instance.FFMPEG, videoFile)
|
2021-04-11 23:04:40 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("error generating phash: %s", err.Error())
|
2022-04-18 00:50:10 +00:00
|
|
|
logErrorOutput(err)
|
2021-04-11 23:04:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := t.txnManager.WithTxn(ctx, func(ctx context.Context) error {
|
|
|
|
qb := t.txnManager.Scene
|
2021-04-11 23:04:40 +00:00
|
|
|
hashValue := sql.NullInt64{Int64: int64(*hash), Valid: true}
|
|
|
|
scenePartial := models.ScenePartial{
|
|
|
|
ID: t.Scene.ID,
|
|
|
|
Phash: &hashValue,
|
|
|
|
}
|
2022-05-19 07:49:32 +00:00
|
|
|
_, err := qb.Update(ctx, scenePartial)
|
2021-04-11 23:04:40 +00:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GeneratePhashTask) shouldGenerate() bool {
|
2021-08-11 06:08:10 +00:00
|
|
|
return t.Overwrite || !t.Scene.Phash.Valid
|
2021-04-11 23:04:40 +00:00
|
|
|
}
|