thumbnail/video.go

68 lines
1.1 KiB
Go
Raw Normal View History

2016-11-18 06:44:27 +00:00
package thumbnail
import (
"bytes"
"fmt"
"io"
2016-11-18 06:44:27 +00:00
"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:
2016-12-05 08:55:28 +00:00
vcodec = "mjpeg" // yes really
case OUTPUT_PNG_CRUSH:
vcodec = "png"
default:
return nil, ErrInvalidOption
}
2016-11-18 06:44:27 +00:00
cmd := exec.Command(
"ffmpeg",
"-loglevel", "0",
"-timelimit", "10", // seconds
2016-11-18 06:44:27 +00:00
"-an",
"-i", absPath,
"-vf", scaleCmd,
2016-11-18 06:44:27 +00:00
"-frames:v", "1",
"-f", "image2pipe",
"-c:v", vcodec,
2016-11-18 06:44:27 +00:00
"-",
)
// -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
2016-11-18 06:44:27 +00:00
}