Skip to content

Commit

Permalink
feat(thumb): use ffmpeg to generate thumb
Browse files Browse the repository at this point in the history
  • Loading branch information
HFO4 committed Apr 7, 2023
1 parent b553444 commit 98b86b3
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 8 deletions.
2 changes: 2 additions & 0 deletions models/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; verti
{Name: "thumb_vips_path", Value: "vips", Type: "thumb"},
{Name: "thumb_vips_exts", Value: "csv,mat,img,hdr,pbm,pgm,ppm,pfm,pnm,svg,svgz,j2k,jp2,jpt,j2c,jpc,gif,png,jpg,jpeg,jpe,webp,tif,tiff,fits,fit,fts,exr,jxl,pdf,heic,heif,avif,svs,vms,vmu,ndpi,scn,mrxs,svslide,bif,raw", Type: "thumb"},
{Name: "thumb_ffmpeg_path", Value: "ffmpeg", Type: "thumb"},
{Name: "thumb_ffmpeg_exts", Value: "3g2,3gp,asf,asx,avi,divx,flv,m2ts,m2v,m4v,mkv,mov,mp4,mpeg,mpg,mts,mxf,ogv,rm,swf,webm,wmv", Type: "thumb"},
{Name: "thumb_ffmpeg_seek", Value: "00:00:01.00", Type: "thumb"},
{Name: "thumb_proxy_enabled", Value: "0", Type: "thumb"},
{Name: "thumb_proxy_policy", Value: "[]", Type: "thumb"},
{Name: "thumb_max_src_size", Value: "31457280", Type: "thumb"},
Expand Down
8 changes: 7 additions & 1 deletion pkg/filesystem/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) e
}
defer source.Close()

thumbPath, err := thumb.Generators.Generate(ctx, source, file.Name, model.GetSettingByNames(
// Provide file source path for local policy files
src := ""
if file.GetPolicy().Type == "local" {
src = file.SourceName
}

thumbPath, err := thumb.Generators.Generate(ctx, source, src, file.Name, model.GetSettingByNames(
"thumb_width",
"thumb_height",
"thumb_builtin_enabled",
Expand Down
2 changes: 1 addition & 1 deletion pkg/thumb/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (image *Thumb) CreateAvatar(uid uint) error {

type Builtin struct{}

func (b Builtin) Generate(ctx context.Context, file io.Reader, name string, options map[string]string) (string, error) {
func (b Builtin) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (string, error) {
img, err := NewThumbFromFile(file, name)
if err != nil {
return "", err
Expand Down
94 changes: 94 additions & 0 deletions pkg/thumb/ffmpeg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package thumb

import (
"bytes"
"context"
"fmt"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)

func init() {
RegisterGenerator(&FfmpegGenerator{})
}

type FfmpegGenerator struct {
exts []string
lastRawExts string
}

func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (string, error) {
ffmpegOpts := model.GetSettingByNames("thumb_ffmpeg_path", "thumb_ffmpeg_exts", "thumb_ffmpeg_seek", "thumb_encode_method", "temp_path")

if f.lastRawExts != ffmpegOpts["thumb_ffmpeg_exts"] {
f.exts = strings.Split(ffmpegOpts["thumb_ffmpeg_exts"], ",")
}

if !util.IsInExtensionList(f.exts, name) {
return "", fmt.Errorf("unsupported video format: %w", ErrPassThrough)
}

tempOutputPath := filepath.Join(
util.RelativePath(ffmpegOpts["temp_path"]),
"thumb",
fmt.Sprintf("thumb_%s.%s", uuid.Must(uuid.NewV4()).String(), ffmpegOpts["thumb_encode_method"]),
)

tempInputPath := src
if tempInputPath == "" {
// If not local policy files, download to temp folder
tempInputPath = filepath.Join(
util.RelativePath(ffmpegOpts["temp_path"]),
"thumb",
fmt.Sprintf("ffmpeg_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)),
)

// Due to limitations of ffmpeg, we need to write the input file to disk first
tempInputFile, err := util.CreatNestedFile(tempInputPath)
if err != nil {
return "", fmt.Errorf("failed to create temp file: %w", err)
}

defer os.Remove(tempInputPath)
defer tempInputFile.Close()

if _, err = io.Copy(tempInputFile, file); err != nil {
return "", fmt.Errorf("failed to write input file: %w", err)
}

tempInputFile.Close()
}

// Invoke ffmpeg
scaleOpt := fmt.Sprintf("scale=%s:%s:force_original_aspect_ratio=decrease", options["thumb_width"], options["thumb_height"])
inputFormat := filepath.Ext(name)[1:]
cmd := exec.CommandContext(ctx,
ffmpegOpts["thumb_ffmpeg_path"], "-ss", ffmpegOpts["thumb_ffmpeg_seek"], "-f", inputFormat, "-i", tempInputPath,
"-vf", scaleOpt, "-vframes", "1", tempOutputPath)

// Redirect IO
var stdErr bytes.Buffer
cmd.Stdin = file
cmd.Stderr = &stdErr

if err := cmd.Run(); err != nil {
util.Log().Warning("Failed to invoke ffmpeg: %s", stdErr.String())
return "", fmt.Errorf("failed to invoke ffmpeg: %w", err)
}

return tempOutputPath, nil
}

func (f *FfmpegGenerator) Priority() int {
return 200
}

func (f *FfmpegGenerator) EnableFlag() string {
return "thumb_ffmpeg_enabled"
}
8 changes: 5 additions & 3 deletions pkg/thumb/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (

// Generator generates a thumbnail for a given reader.
type Generator interface {
Generate(ctx context.Context, file io.Reader, name string, options map[string]string) (string, error)
// Generate generates a thumbnail for a given reader. Src is the original file path, only provided
// for local policy files.
Generate(ctx context.Context, file io.Reader, src string, name string, options map[string]string) (string, error)

// Priority of execution order, smaller value means higher priority.
Priority() int
Expand Down Expand Up @@ -52,10 +54,10 @@ func RegisterGenerator(generator Generator) {
sort.Sort(Generators)
}

func (p GeneratorList) Generate(ctx context.Context, file io.Reader, name string, options map[string]string) (string, error) {
func (p GeneratorList) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (string, error) {
for _, generator := range p {
if model.IsTrueVal(options[generator.EnableFlag()]) {
res, err := generator.Generate(ctx, file, name, options)
res, err := generator.Generate(ctx, file, src, name, options)
if errors.Is(err, ErrPassThrough) {
util.Log().Debug("Failed to generate thumbnail for %s: %s, passing through to next generator.", name, err)
continue
Expand Down
6 changes: 3 additions & 3 deletions pkg/thumb/vips.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type VipsGenerator struct {
lastRawExts string
}

func (v VipsGenerator) Generate(ctx context.Context, file io.Reader, name string, options map[string]string) (string, error) {
func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (string, error) {
vipsOpts := model.GetSettingByNames("thumb_vips_path", "thumb_vips_exts", "thumb_encode_quality", "thumb_encode_method", "temp_path")

if v.lastRawExts != vipsOpts["thumb_vips_exts"] {
Expand Down Expand Up @@ -69,10 +69,10 @@ func (v VipsGenerator) Generate(ctx context.Context, file io.Reader, name string
return tempPath, nil
}

func (v VipsGenerator) Priority() int {
func (v *VipsGenerator) Priority() int {
return 100
}

func (v VipsGenerator) EnableFlag() string {
func (v *VipsGenerator) EnableFlag() string {
return "thumb_vips_enabled"
}

0 comments on commit 98b86b3

Please sign in to comment.