-
Notifications
You must be signed in to change notification settings - Fork 1
/
video.go
59 lines (52 loc) · 1.24 KB
/
video.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
package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
ffmpeg "github.com/u2takey/ffmpeg-go"
"io/ioutil"
"log"
"os"
"strings"
)
func init() {
}
func getSanitizedBaseDir(path string) string {
path = strings.ReplaceAll(path, *baseDir, "")
path = strings.ReplaceAll(path, "/", "_")
return path
}
func LoadThumbnail(path string) ([]byte, error) {
h := sha1.New()
h.Write([]byte(path))
hash := hex.EncodeToString(h.Sum(nil))
os.Mkdir(*thumbnails, 0777)
thumbPath := *thumbnails + "/" + getSanitizedBaseDir(path) + "_" + hash
if _, err := os.Stat(thumbPath); errors.Is(err, os.ErrNotExist) {
e := bytes.NewBufferString("")
for frame := 100; frame >= 0; frame -= 25 {
e = Thumbnail(path, frame)
if len(e.Bytes()) > 0 {
break
}
}
err := ioutil.WriteFile(thumbPath, e.Bytes(), 0644)
if err != nil {
log.Panicln(err)
}
return e.Bytes(), nil
}
return ioutil.ReadFile(thumbPath)
}
func Thumbnail(path string, frameNum int) *bytes.Buffer {
buf := bytes.NewBuffer(nil)
ffmpeg.
Input(path).
Filter("select", ffmpeg.Args{fmt.Sprintf("gte(n,%d)", frameNum)}).
Output("pipe:", ffmpeg.KwArgs{"vframes": 1, "format": "image2", "vcodec": "mjpeg"}).
WithOutput(buf, os.Stdout).
Run()
return buf
}