thumbnail/video.go

68 lines
1.1 KiB
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,
)
// BUG(): always produces ASPECT_SVELTE shape - need to re-pad for ASPECT_PADDED
var vcodec string
switch this.ofmt {
case OUTPUT_JPG:
vcodec = "mjpeg" // yes really
case OUTPUT_PNG_CRUSH:
vcodec = "png"
default:
return nil, ErrInvalidOption
}
cmd := exec.Command(
"ffmpeg",
"-loglevel", "0",
"-timelimit", "10", // seconds
"-an",
"-i", absPath,
"-vf", scaleCmd,
"-frames:v", "1",
"-f", "image2pipe",
"-c:v", vcodec,
"-",
)
// -ss 00:00:30
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
}