thumbnail/video.go

53 lines
798 B
Go

package thumbnail
import (
"bytes"
"fmt"
"io"
"os/exec"
)
func (this *Thumbnailer) RenderScaledFfmpeg(absPath string) ([]byte, error) {
scaleCmd := fmt.Sprintf(
`thumbnail,scale='if(gt(a,%d/%d),%d,-1)':'if(gt(a,%d/%d),-1,%d)'`,
this.Height, this.Width, this.Height,
this.Height, this.Width, this.Width,
)
cmd := exec.Command(
"ffmpeg",
"-loglevel", "0",
"-an",
"-i", absPath,
"-vf", scaleCmd,
"-frames:v", "1",
"-f", "image2pipe",
"-c:v", "png",
"-",
)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
err = cmd.Start()
if err != nil {
return nil, err
}
out := bytes.Buffer{}
_, err = io.Copy(&out, stdout)
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
return nil, err
}
return out.Bytes(), nil
}