-
Notifications
You must be signed in to change notification settings - Fork 19
/
ffmpegw.go
72 lines (69 loc) · 1.78 KB
/
ffmpegw.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
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"os/exec"
"io/ioutil"
"html/template"
"os"
"errors"
)
func MakeGif(tplKey string, subs Subs) (string, error) {
return GenerateResource(tplKey, subs, "gif")
}
func MakeMp4(tplKey string, subs Subs) (string, error) {
return GenerateResource(tplKey, subs, "mp4")
}
// GenerateResource Generate resources(gif/mp4)
// ffmpeg CLI wrapper
func GenerateResource(tplKey string, subs Subs, resType string) (hash string, err error) {
tplPath := fmt.Sprintf("./resources/template/%s", tplKey)
videoTplFile := tplPath + "/template.mp4"
subTplFile := tplPath + "/template.ass"
hash = subs.Hash(tplKey)
subOutputFile := fmt.Sprintf("./dist/%s", hash)
outputResource := "dist/" + hash + "." + resType
if _, err = os.Stat(outputResource); os.IsNotExist(err) {
tplText := ""
if tmpBuf, err := ioutil.ReadFile(subTplFile); err != nil {
return hash, err
} else {
tplText = string(tmpBuf)
}
tpl := template.New("subTitle")
if tpl, err = tpl.Parse(tplText); err != nil {
return
} else {
if f, err := os.Create(subOutputFile); err != nil {
return hash, err
} else {
data := map[string][]string{
"sentences": subs.EntrySet(),
}
if err = tpl.Execute(f, data); err != nil {
return hash, err
}
}
}
var cmd = &exec.Cmd{}
switch resType {
case "gif":
cmd = exec.Command("ffmpeg", "-i", videoTplFile,
"-vf", fmt.Sprintf("ass=%s,scale=300:-1", subOutputFile),
"-r", "8",
"-y", outputResource)
case "mp4":
cmd = exec.Command("ffmpeg", "-i", videoTplFile,
"-vf", fmt.Sprintf("ass=%s", subOutputFile),
"-an",
"-y", outputResource)
default:
return "", errors.New("Unknown resType: " + resType)
}
if _, err := cmd.CombinedOutput(); err != nil {
return hash, err
}
} else {
return
}
return
}