2016-11-18 06:44:27 +00:00
|
|
|
package thumbnail
|
|
|
|
|
|
|
|
import (
|
2016-11-18 07:02:22 +00:00
|
|
|
"bytes"
|
2016-12-05 06:01:00 +00:00
|
|
|
"fmt"
|
2016-11-18 07:02:22 +00:00
|
|
|
"io"
|
2016-11-18 06:44:27 +00:00
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (this *Thumbnailer) RenderScaledFfmpeg(absPath string) ([]byte, error) {
|
|
|
|
|
2016-12-05 06:01:00 +00:00
|
|
|
scaleCmd := fmt.Sprintf(
|
|
|
|
`thumbnail,scale='if(gt(a,%d/%d),%d,-1)':'if(gt(a,%d/%d),-1,%d)'`,
|
2016-12-05 08:48:48 +00:00
|
|
|
this.height, this.width, this.height,
|
|
|
|
this.height, this.width, this.width,
|
2016-12-05 06:01:00 +00:00
|
|
|
)
|
|
|
|
|
2016-12-05 08:48:48 +00:00
|
|
|
// 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
|
2016-12-05 08:48:48 +00:00
|
|
|
case OUTPUT_PNG_CRUSH:
|
|
|
|
vcodec = "png"
|
|
|
|
default:
|
|
|
|
return nil, ErrInvalidOption
|
|
|
|
}
|
|
|
|
|
2016-11-18 06:44:27 +00:00
|
|
|
cmd := exec.Command(
|
|
|
|
"ffmpeg",
|
|
|
|
"-loglevel", "0",
|
2016-12-05 09:10:39 +00:00
|
|
|
"-timelimit", "10", // seconds
|
2016-11-18 06:44:27 +00:00
|
|
|
"-an",
|
|
|
|
"-i", absPath,
|
2016-12-05 06:01:00 +00:00
|
|
|
"-vf", scaleCmd,
|
2016-11-18 06:44:27 +00:00
|
|
|
"-frames:v", "1",
|
|
|
|
"-f", "image2pipe",
|
2016-12-05 08:48:48 +00:00
|
|
|
"-c:v", vcodec,
|
2016-11-18 06:44:27 +00:00
|
|
|
"-",
|
|
|
|
)
|
|
|
|
|
2016-12-05 09:10:39 +00:00
|
|
|
// -ss 00:00:30
|
|
|
|
|
2016-11-18 07:02:22 +00:00
|
|
|
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
|
|
|
}
|