-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbnail.go
44 lines (36 loc) · 927 Bytes
/
thumbnail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"bytes"
"io"
"log"
"os/exec"
)
func createThumbnail(inputData []byte) ([]byte, error) {
var cmdOut, cmdErr bytes.Buffer
var err error
// ffmpeg will work with stdin and stdout
// we set those up next
cmd := exec.Command(
"ffmpeg", "-i", "pipe:0", "-vf", "scale='iw/2:ih/2'", "-f", "image2", "pipe:1",
)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
cmd.Stdout = &cmdOut
cmd.Stderr = &cmdErr
go func() {
defer stdin.Close()
_, err = io.WriteString(stdin, string(inputData))
}()
// we cannot use CombinedOutput() here as it returns both
// stdout and stderr
// stdout will contain our transformed image when successful
// we also want to report the entire error when there is a problem
// so, we have to separate out stdout and stderr explicitly
err = cmd.Run()
if err != nil {
log.Println(string(cmdErr.Bytes()))
}
return cmdOut.Bytes(), err
}