Fix image thumbnail generation (#2524)

* Better logging for thumbnail generation errors
* Reduce verbosity for thumbnail generation
* Provide stdin during thumbnail generation
This commit is contained in:
WithoutPants 2022-04-25 15:56:06 +10:00 committed by GitHub
parent 9e606feb76
commit 1b91937004
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 4 deletions

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"net/http" "net/http"
"os/exec"
"strconv" "strconv"
"github.com/go-chi/chi" "github.com/go-chi/chi"
@ -50,6 +51,11 @@ func (rs imageRoutes) Thumbnail(w http.ResponseWriter, r *http.Request) {
// don't log for unsupported image format // don't log for unsupported image format
if !errors.Is(err, image.ErrNotSupportedForThumbnail) { if !errors.Is(err, image.ErrNotSupportedForThumbnail) {
logger.Errorf("error generating thumbnail for image: %s", err.Error()) logger.Errorf("error generating thumbnail for image: %s", err.Error())
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
logger.Errorf("stderr: %s", string(exitErr.Stderr))
}
} }
// backwards compatibility - fallback to original image instead // backwards compatibility - fallback to original image instead

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"errors" "errors"
"os/exec"
"path/filepath" "path/filepath"
"time" "time"
@ -158,6 +159,11 @@ func (t *ScanTask) generateThumbnail(i *models.Image) {
// don't log for animated images // don't log for animated images
if !errors.Is(err, image.ErrNotSupportedForThumbnail) { if !errors.Is(err, image.ErrNotSupportedForThumbnail) {
logger.Errorf("error getting thumbnail for image %s: %s", i.Path, err.Error()) logger.Errorf("error getting thumbnail for image %s: %s", i.Path, err.Error())
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
logger.Errorf("stderr: %s", string(exitErr.Stderr))
}
} }
return return
} }

View file

@ -5,6 +5,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io"
"os/exec" "os/exec"
"strings" "strings"
) )
@ -35,8 +36,14 @@ func (f FFMpeg) Generate(ctx context.Context, args Args) error {
} }
// GenerateOutput runs ffmpeg with the given args and returns it standard output. // GenerateOutput runs ffmpeg with the given args and returns it standard output.
func (f FFMpeg) GenerateOutput(ctx context.Context, args []string) ([]byte, error) { func (f FFMpeg) GenerateOutput(ctx context.Context, args []string, stdin io.Reader) ([]byte, error) {
cmd := f.Command(ctx, args) cmd := f.Command(ctx, args)
cmd.Stdin = stdin
return cmd.Output() ret, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("error running ffmpeg command <%s>: %w", strings.Join(args, " "), err)
}
return ret, nil
} }

View file

@ -20,6 +20,8 @@ func ImageThumbnail(input string, options ImageThumbnailOptions) ffmpeg.Args {
videoFilter = videoFilter.ScaleMaxSize(options.MaxDimensions) videoFilter = videoFilter.ScaleMaxSize(options.MaxDimensions)
var args ffmpeg.Args var args ffmpeg.Args
args = append(args, "-hide_banner")
args = args.LogLevel(ffmpeg.LogLevelError)
args = args.Overwrite(). args = args.Overwrite().
ImageFormat(options.InputFormat). ImageFormat(options.InputFormat).

View file

@ -44,7 +44,7 @@ func generateSpriteScreenshot(encoder ffmpeg.FFMpeg, input string, t float64) (i
} }
args := transcoder.ScreenshotTime(input, t, options) args := transcoder.ScreenshotTime(input, t, options)
data, err := encoder.GenerateOutput(context.Background(), args) data, err := encoder.GenerateOutput(context.Background(), args, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -117,5 +117,5 @@ func (e *ThumbnailEncoder) ffmpegImageThumbnail(image *bytes.Buffer, format stri
Quality: ffmpegImageQuality, Quality: ffmpegImageQuality,
}) })
return e.ffmpeg.GenerateOutput(context.TODO(), args) return e.ffmpeg.GenerateOutput(context.TODO(), args, image)
} }