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"
"errors"
"net/http"
"os/exec"
"strconv"
"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
if !errors.Is(err, image.ErrNotSupportedForThumbnail) {
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

View File

@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
"os/exec"
"path/filepath"
"time"
@ -158,6 +159,11 @@ func (t *ScanTask) generateThumbnail(i *models.Image) {
// don't log for animated images
if !errors.Is(err, image.ErrNotSupportedForThumbnail) {
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
}

View File

@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os/exec"
"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.
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.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)
var args ffmpeg.Args
args = append(args, "-hide_banner")
args = args.LogLevel(ffmpeg.LogLevelError)
args = args.Overwrite().
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)
data, err := encoder.GenerateOutput(context.Background(), args)
data, err := encoder.GenerateOutput(context.Background(), args, nil)
if err != nil {
return nil, err
}

View File

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