-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
135 lines (120 loc) · 3.04 KB
/
converter.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"fmt"
ffmpeg "github.com/u2takey/ffmpeg-go"
"log"
"math/rand"
"net"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
)
type converter interface {
resizeWithErr(fileName, inputDir, outputDir, videoQuality string) error
}
type ConverterImpl struct {
}
func checkDirectories(paths ...string) error {
for _, dir := range paths {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return err
}
}
return nil
}
// Try this one too
func resize(input, outputDir, videoQuality string, errorsChanel chan error) {
size := fmt.Sprintf("%dx%d", videoSizes[videoQuality].X, videoSizes[videoQuality].Y)
fmt.Print("Video size is: " + size + "\n")
outputFile := fmt.Sprintf("%sresize_%v_%v.mp4", outputDir, strings.Split(input, ".")[0], size)
fmt.Printf("Output file is: %v\\%v\n", outputDir, outputFile)
err := ffmpeg.Input(input).Output(outputFile, ffmpeg.KwArgs{"s": size}).OverWriteOutput().Run()
if err != nil {
errorsChanel <- err
}
}
func (c ConverterImpl) resizeWithErr(fileName, inputDir, outputDir, videoQuality string) error {
size := fmt.Sprintf("%dx%d", videoSizes[videoQuality].X, videoSizes[videoQuality].Y)
fmt.Print("Video size is: " + size + "\n")
inputFile := fmt.Sprintf("%s%s", inputDir, fileName)
outputFile := fmt.Sprintf("%sresize_%s_%s.mp4", outputDir, fileName, size)
fmt.Printf("Out file is: %v\n", outputFile)
//Use channel for progress streaming smh
probe, err := ffmpeg.Probe(inputFile)
if err != nil {
return err
}
total, err := probeDuration(probe)
if err != nil {
return err
}
fs := ffmpeg.Input(inputFile).
Output(outputFile, ffmpeg.KwArgs{"s": size}).
GlobalArgs("-progress", "unix://"+TempSock(total))
return err
}
func TempSock(totalDuration float64) string {
// serve
rand.New(rand.NewSource(time.Now().Unix()))
sockFileName := path.Join(os.TempDir(), fmt.Sprintf("%d_sock", rand.Int()))
l, err := net.Listen("unix", sockFileName)
if err != nil {
panic(err)
}
go func() {
re := regexp.MustCompile(`out_time_ms=(\d+)`)
fd, err := l.Accept()
if err != nil {
log.Fatal("accept error:", err)
}
buf := make([]byte, 16)
data := ""
progress := ""
for {
_, err := fd.Read(buf)
if err != nil {
return
}
data += string(buf)
a := re.FindAllStringSubmatch(data, -1)
cp := ""
if len(a) > 0 && len(a[len(a)-1]) > 0 {
c, _ := strconv.Atoi(a[len(a)-1][len(a[len(a)-1])-1])
cp = fmt.Sprintf("%.2f", float64(c)/totalDuration/1000000)
}
if strings.Contains(data, "progress=end") {
cp = "done"
}
if cp == "" {
cp = ".0"
}
if cp != progress {
progress = cp
fmt.Println("progress: ", progress)
}
}
}()
return sockFileName
}
type probeFormat struct {
Duration string `json:"duration"`
}
type probeData struct {
Format probeFormat `json:"format"`
}
func probeDuration(input string) (float64, error) {
pd := probeData{}
err := json.Unmarshal([]byte(input), &pd)
if err != nil {
return 0, err
}
f, err := strconv.ParseFloat(pd.Format.Duration, 64)
if err != nil {
return 0, err
}
return f, nil
}