Skip to content

Commit

Permalink
feat(local): add thumbnail for video with ffmpeg (AlistGo#3556)
Browse files Browse the repository at this point in the history
* feat(local): add ffmpeg

* fix: missed `+`

---------

Co-authored-by: Andy Hsu <[email protected]>
  • Loading branch information
2 people authored and walleoo committed Feb 26, 2023
1 parent df6714a commit f73eee4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
32 changes: 23 additions & 9 deletions drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"github.com/alist-org/alist/v3/server/common"
"io"
"io/ioutil"
"net/http"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/sign"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common"
"github.com/disintegration/imaging"
_ "golang.org/x/image/webp"
)
Expand Down Expand Up @@ -71,10 +71,13 @@ func (d *Local) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
continue
}
thumb := ""
if d.Thumbnail && utils.GetFileType(f.Name()) == conf.IMAGE {
thumb = common.GetApiUrl(nil) + stdpath.Join("/d", args.ReqPath, f.Name())
thumb = utils.EncodePath(thumb, true)
thumb += "?type=thumb&sign=" + sign.Sign(stdpath.Join(args.ReqPath, f.Name()))
if d.Thumbnail {
typeName := utils.GetFileType(f.Name())
if typeName == conf.IMAGE || typeName == conf.VIDEO {
thumb = common.GetApiUrl(nil) + stdpath.Join("/d", args.ReqPath, f.Name())
thumb = utils.EncodePath(thumb, true)
thumb += "?type=thumb&sign=" + sign.Sign(stdpath.Join(args.ReqPath, f.Name()))
}
}
isFolder := f.IsDir() || isSymlinkDir(f, fullPath)
var size int64
Expand Down Expand Up @@ -126,11 +129,22 @@ func (d *Local) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (
fullPath := file.GetPath()
var link model.Link
if args.Type == "thumb" && utils.Ext(file.GetName()) != "svg" {
imgData, err := ioutil.ReadFile(fullPath)
if err != nil {
return nil, err
var srcBuf *bytes.Buffer
if utils.GetFileType(file.GetName()) == conf.VIDEO {
videoBuf, err := GetSnapshot(fullPath, 10)
if err != nil {
return nil, err
}
srcBuf = videoBuf
} else {
imgData, err := ioutil.ReadFile(fullPath)
if err != nil {
return nil, err
}
imgBuf := bytes.NewBuffer(imgData)
srcBuf = imgBuf
}
srcBuf := bytes.NewBuffer(imgData)

image, err := imaging.Decode(srcBuf)
if err != nil {
return nil, err
Expand Down
16 changes: 16 additions & 0 deletions drivers/local/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package local

import (
"bytes"
"fmt"
ffmpeg "github.com/u2takey/ffmpeg-go"
"io/fs"
"os"
"path/filepath"
Expand All @@ -23,3 +26,16 @@ func isSymlinkDir(f fs.FileInfo, path string) bool {
}
return false
}

func GetSnapshot(videoPath string, frameNum int) (imgData *bytes.Buffer, err error) {
srcBuf := bytes.NewBuffer(nil)
err = ffmpeg.Input(videoPath).Filter("select", ffmpeg.Args{fmt.Sprintf("gte(n,%d)", frameNum)}).
Output("pipe:", ffmpeg.KwArgs{"vframes": 1, "format": "image2", "vcodec": "mjpeg"}).
WithOutput(srcBuf, os.Stdout).
Run()

if err != nil {
return nil, err
}
return srcBuf, nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ require (
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/u2takey/ffmpeg-go v0.4.1 // indirect
github.com/u2takey/go-utils v0.3.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
go.etcd.io/bbolt v1.3.5 // indirect
golang.org/x/sys v0.5.0 // indirect
Expand Down
Loading

0 comments on commit f73eee4

Please sign in to comment.