From a74fbce48e0239c3b60f78b517d1d6e8e44177b7 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 22 Jan 2024 00:37:54 +0100 Subject: [PATCH] Resize thumbnails and cap max number of thumbnails --- transcoder/src/thumbnails.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/transcoder/src/thumbnails.go b/transcoder/src/thumbnails.go index 2fa30db9..540b5cb1 100644 --- a/transcoder/src/thumbnails.go +++ b/transcoder/src/thumbnails.go @@ -15,6 +15,9 @@ import ( // We want to have a thumbnail every ${interval} seconds. 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 { created map[string]string @@ -71,7 +74,7 @@ func extractThumbnail(path string, name string) (string, error) { sprite_path := fmt.Sprintf("%s/sprite.png", 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 } @@ -84,39 +87,36 @@ func extractThumbnail(path string, name string) (string, error) { gen.Fast = true - // truncate duration to full seconds - // this prevents empty/black images when the movie is some milliseconds longer - // ffmpeg then sometimes takes a black screenshot AFTER the movie finished for some reason - duration := 1000 * (int(gen.Duration) / 1000) - var numcaps, interval int + duration := int(gen.Duration) / 1000 + var numcaps int if default_interval < duration { - numcaps = duration / default_interval * 1000 - interval = default_interval + numcaps = duration / default_interval } else { numcaps = duration / 10 - interval = duration / numcaps } + numcaps = min(numcaps, max_numcaps) + interval := duration / numcaps columns := int(math.Sqrt(float64(numcaps))) rows := int(math.Ceil(float64(numcaps) / float64(columns))) - width := gen.Width() - height := gen.Height() + height := 144 + width := int(float64(height) / float64(gen.Height()) * float64(gen.Width())) sprite := imaging.New(width*columns, height*rows, color.Black) vtt := "WEBVTT\n\n" + log.Printf("Extracting %d thumbnails for %s (interval of %d).", numcaps, path, interval) + ts := 0 for i := 0; i < numcaps; i++ { - img, err := gen.Image(int64(ts)) + img, err := gen.ImageWxH(int64(ts*1000), width, height) if err != nil { log.Printf("Could not generate screenshot %s", err) return "", err } - // TODO: resize image - - x := i % width - y := i / width + x := (i % columns) * width + y := (i / columns) * height sprite = imaging.Paste(sprite, img, image.Pt(x, y)) timestamps := ts