2021-04-11 23:04:40 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/corona10/goimagehash"
|
|
|
|
"github.com/disintegration/imaging"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/ffmpeg"
|
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PhashGenerator struct {
|
|
|
|
Info *GeneratorInfo
|
|
|
|
|
|
|
|
VideoChecksum string
|
|
|
|
Columns int
|
|
|
|
Rows int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPhashGenerator(videoFile ffmpeg.VideoFile, checksum string) (*PhashGenerator, error) {
|
|
|
|
exists, err := utils.FileExists(videoFile.Path)
|
|
|
|
if !exists {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
generator, err := newGeneratorInfo(videoFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PhashGenerator{
|
|
|
|
Info: generator,
|
|
|
|
VideoChecksum: checksum,
|
|
|
|
Columns: 5,
|
|
|
|
Rows: 5,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *PhashGenerator) Generate() (*uint64, error) {
|
|
|
|
encoder := ffmpeg.NewEncoder(instance.FFMPEGPath)
|
|
|
|
|
|
|
|
sprite, err := g.generateSprite(&encoder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := goimagehash.PerceptionHash(sprite)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hashValue := hash.GetHash()
|
|
|
|
return &hashValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *PhashGenerator) generateSprite(encoder *ffmpeg.Encoder) (image.Image, error) {
|
|
|
|
logger.Infof("[generator] generating phash sprite for %s", g.Info.VideoFile.Path)
|
|
|
|
|
|
|
|
// Generate sprite image offset by 5% on each end to avoid intro/outros
|
|
|
|
chunkCount := g.Columns * g.Rows
|
|
|
|
offset := 0.05 * g.Info.VideoFile.Duration
|
|
|
|
stepSize := (0.9 * g.Info.VideoFile.Duration) / float64(chunkCount)
|
2021-05-05 03:22:05 +00:00
|
|
|
var images []image.Image
|
2021-04-11 23:04:40 +00:00
|
|
|
for i := 0; i < chunkCount; i++ {
|
|
|
|
time := offset + (float64(i) * stepSize)
|
|
|
|
|
2021-05-05 03:22:05 +00:00
|
|
|
options := ffmpeg.SpriteScreenshotOptions{
|
|
|
|
Time: time,
|
|
|
|
Width: 160,
|
2021-04-11 23:04:40 +00:00
|
|
|
}
|
2021-05-05 03:22:05 +00:00
|
|
|
img, err := encoder.SpriteScreenshot(g.Info.VideoFile, options)
|
2021-04-11 23:04:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
images = append(images, img)
|
|
|
|
}
|
|
|
|
|
2021-05-05 03:22:05 +00:00
|
|
|
// Combine all of the thumbnails into a sprite image
|
2021-04-11 23:04:40 +00:00
|
|
|
if len(images) == 0 {
|
|
|
|
return nil, fmt.Errorf("images slice is empty, failed to generate phash sprite for %s", g.Info.VideoFile.Path)
|
|
|
|
}
|
|
|
|
width := images[0].Bounds().Size().X
|
|
|
|
height := images[0].Bounds().Size().Y
|
|
|
|
canvasWidth := width * g.Columns
|
|
|
|
canvasHeight := height * g.Rows
|
|
|
|
montage := imaging.New(canvasWidth, canvasHeight, color.NRGBA{})
|
|
|
|
for index := 0; index < len(images); index++ {
|
|
|
|
x := width * (index % g.Columns)
|
|
|
|
y := height * int(math.Floor(float64(index)/float64(g.Rows)))
|
|
|
|
img := images[index]
|
|
|
|
montage = imaging.Paste(montage, img, image.Pt(x, y))
|
|
|
|
}
|
|
|
|
|
|
|
|
return montage, nil
|
|
|
|
}
|