Skip to content

Commit

Permalink
Add option to generate image thumbnails during generate (#4602)
Browse files Browse the repository at this point in the history
* Add option to generate image thumbnails
* Limit number of concurrent image thumbnail generation ops
  • Loading branch information
WithoutPants committed Feb 22, 2024
1 parent c4a91d1 commit a8c909e
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 222 deletions.
2 changes: 2 additions & 0 deletions graphql/schema/types/metadata.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ input GenerateMetadataInput {
forceTranscodes: Boolean
phashes: Boolean
interactiveHeatmapsSpeeds: Boolean
imageThumbnails: Boolean
clipPreviews: Boolean

"scene ids to generate for"
Expand Down Expand Up @@ -48,6 +49,7 @@ type GenerateMetadataOptions {
transcodes: Boolean
phashes: Boolean
interactiveHeatmapsSpeeds: Boolean
imageThumbnails: Boolean
clipPreviews: Boolean
}

Expand Down
8 changes: 7 additions & 1 deletion internal/api/routes_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func (rs imageRoutes) Routes() chi.Router {
}

func (rs imageRoutes) Thumbnail(w http.ResponseWriter, r *http.Request) {
mgr := manager.GetInstance()
img := r.Context().Value(imageKey).(*models.Image)
filepath := manager.GetInstance().Paths.Generated.GetThumbnailPath(img.Checksum, models.DefaultGthumbWidth)
filepath := mgr.Paths.Generated.GetThumbnailPath(img.Checksum, models.DefaultGthumbWidth)

// if the thumbnail doesn't exist, encode on the fly
exists, _ := fsutil.FileExists(filepath)
Expand All @@ -62,6 +63,11 @@ func (rs imageRoutes) Thumbnail(w http.ResponseWriter, r *http.Request) {
return
}

// use the image thumbnail generate wait group to limit the number of concurrent thumbnail generation tasks
wg := &mgr.ImageThumbnailGenerateWaitGroup
wg.Add()
defer wg.Done()

clipPreviewOptions := image.ClipPreviewOptions{
InputArgs: manager.GetInstance().Config.GetTranscodeInputArgs(),
OutputArgs: manager.GetInstance().Config.GetTranscodeOutputArgs(),
Expand Down
3 changes: 3 additions & 0 deletions internal/manager/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/remeh/sizedwaitgroup"
"github.com/stashapp/stash/internal/desktop"
"github.com/stashapp/stash/internal/dlna"
"github.com/stashapp/stash/internal/log"
Expand Down Expand Up @@ -80,6 +81,8 @@ func Initialize(cfg *config.Config, l *log.Logger) (*Manager, error) {

Paths: mgrPaths,

ImageThumbnailGenerateWaitGroup: sizedwaitgroup.New(1),

JobManager: initJobManager(cfg),
ReadLockManager: fsutil.NewReadLockManager(),

Expand Down
7 changes: 7 additions & 0 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"
"time"

"github.com/remeh/sizedwaitgroup"
"github.com/stashapp/stash/internal/dlna"
"github.com/stashapp/stash/internal/log"
"github.com/stashapp/stash/internal/manager/config"
Expand All @@ -33,6 +34,10 @@ type Manager struct {
Config *config.Config
Logger *log.Logger

// ImageThumbnailGenerateWaitGroup is the global wait group image thumbnail generation
// It uses the parallel tasks setting from the configuration.
ImageThumbnailGenerateWaitGroup sizedwaitgroup.SizedWaitGroup

Paths *paths.Paths

FFMpeg *ffmpeg.FFMpeg
Expand Down Expand Up @@ -107,6 +112,8 @@ func (s *Manager) RefreshConfig() {
if err := fsutil.EnsureDir(s.Paths.Generated.InteractiveHeatmap); err != nil {
logger.Warnf("could not create interactive heatmaps directory: %v", err)
}

s.ImageThumbnailGenerateWaitGroup.Size = cfg.GetParallelTasksWithAutoDetection()
}
}

Expand Down

0 comments on commit a8c909e

Please sign in to comment.