Resize thumbnails and cap max number of thumbnails

This commit is contained in:
Zoe Roux 2024-01-22 00:37:54 +01:00
parent cecf7b0f74
commit a74fbce48e

View File

@ -15,6 +15,9 @@ import (
// We want to have a thumbnail every ${interval} seconds. // We want to have a thumbnail every ${interval} seconds.
var default_interval = 10 var default_interval = 10
// The maximim number of thumbnails per video.
// Setting this too high allows really long processing times.
var max_numcaps = 150
type ThumbnailsCreator struct { type ThumbnailsCreator struct {
created map[string]string created map[string]string
@ -71,7 +74,7 @@ func extractThumbnail(path string, name string) (string, error) {
sprite_path := fmt.Sprintf("%s/sprite.png", out) sprite_path := fmt.Sprintf("%s/sprite.png", out)
vtt_path := fmt.Sprintf("%s/sprite.vtt", out) vtt_path := fmt.Sprintf("%s/sprite.vtt", out)
if _, err := os.Stat(sprite_path); err != nil { if _, err := os.Stat(sprite_path); err == nil {
return out, nil return out, nil
} }
@ -84,39 +87,36 @@ func extractThumbnail(path string, name string) (string, error) {
gen.Fast = true gen.Fast = true
// truncate duration to full seconds duration := int(gen.Duration) / 1000
// this prevents empty/black images when the movie is some milliseconds longer var numcaps int
// ffmpeg then sometimes takes a black screenshot AFTER the movie finished for some reason
duration := 1000 * (int(gen.Duration) / 1000)
var numcaps, interval int
if default_interval < duration { if default_interval < duration {
numcaps = duration / default_interval * 1000 numcaps = duration / default_interval
interval = default_interval
} else { } else {
numcaps = duration / 10 numcaps = duration / 10
interval = duration / numcaps
} }
numcaps = min(numcaps, max_numcaps)
interval := duration / numcaps
columns := int(math.Sqrt(float64(numcaps))) columns := int(math.Sqrt(float64(numcaps)))
rows := int(math.Ceil(float64(numcaps) / float64(columns))) rows := int(math.Ceil(float64(numcaps) / float64(columns)))
width := gen.Width() height := 144
height := gen.Height() width := int(float64(height) / float64(gen.Height()) * float64(gen.Width()))
sprite := imaging.New(width*columns, height*rows, color.Black) sprite := imaging.New(width*columns, height*rows, color.Black)
vtt := "WEBVTT\n\n" vtt := "WEBVTT\n\n"
log.Printf("Extracting %d thumbnails for %s (interval of %d).", numcaps, path, interval)
ts := 0 ts := 0
for i := 0; i < numcaps; i++ { for i := 0; i < numcaps; i++ {
img, err := gen.Image(int64(ts)) img, err := gen.ImageWxH(int64(ts*1000), width, height)
if err != nil { if err != nil {
log.Printf("Could not generate screenshot %s", err) log.Printf("Could not generate screenshot %s", err)
return "", err return "", err
} }
// TODO: resize image x := (i % columns) * width
y := (i / columns) * height
x := i % width
y := i / width
sprite = imaging.Paste(sprite, img, image.Pt(x, y)) sprite = imaging.Paste(sprite, img, image.Pt(x, y))
timestamps := ts timestamps := ts