-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.go
60 lines (54 loc) · 1.35 KB
/
preview.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"bytes"
"fmt"
ffmpeg "github.com/u2takey/ffmpeg-go"
)
func videoFirstFrame(name string) []byte {
return readFrameAsJpeg(name, 1)
}
func imagePreview(name string) []byte {
buf := bytes.NewBuffer(nil)
err := ffmpeg.Input(name).
Output("pipe:", ffmpeg.KwArgs{"format": "image2", "vcodec": "mjpeg", "q:v": "4", "vf": "scale=(-1):400"}).
WithOutput(buf).
Run()
if err != nil {
fmt.Println(err)
}
return buf.Bytes()
}
func gifPreview(name string) []byte {
buf := bytes.NewBuffer(nil)
err := ffmpeg.Input(name).
Output("pipe:", ffmpeg.KwArgs{"format": "gif", "vf": "fps=8,scale=(-1):200"}).
WithOutput(buf).
Run()
if err != nil {
fmt.Println(err)
}
return buf.Bytes()
}
func videoPreview(name string) []byte {
buf := bytes.NewBuffer(nil)
err := ffmpeg.Input(name, ffmpeg.KwArgs{"t": "15"}).
Output("pipe:", ffmpeg.KwArgs{"format": "gif", "vf": "fps=8,scale=(-1):200"}).
WithOutput(buf).
Run()
if err != nil {
fmt.Println(err)
}
return buf.Bytes()
}
func readFrameAsJpeg(inFileName string, frameNum int) []byte {
buf := bytes.NewBuffer(nil)
err := ffmpeg.Input(inFileName).
Filter("select", ffmpeg.Args{fmt.Sprintf("gte(n,%d)", frameNum)}).
Output("pipe:", ffmpeg.KwArgs{"vframes": 1, "format": "image2", "vcodec": "mjpeg"}).
WithOutput(buf).
Run()
if err != nil {
fmt.Println(err)
}
return buf.Bytes()
}