/
process.go
269 lines (245 loc) · 5.17 KB
/
process.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package seed
import (
"context"
"crypto/md5"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"unicode"
"github.com/glvd/seed/model"
cmd "github.com/godcong/go-ffmpeg-cmd"
)
func dummy(process *Process) (e error) {
log.Info("dummy called")
return
}
// Process ...
type Process struct {
*Thread
//taskMutex *sync.RWMutex
//tasks map[string]*Task
cb chan ProcessCaller
//workspace string
//path string
//moves map[string]string
//scale int64
//skipConvert bool
//skipExist bool
//noSlice bool
//preAdd bool
//skipType []interface {}
}
// Push ...
func (p *Process) Push(v interface{}) error {
return p.push(v)
}
func (p *Process) push(cb interface{}) error {
if v, b := cb.(ProcessCaller); b {
p.cb <- v
return nil
}
return errors.New("not process callback")
}
// NewProcess ...
func NewProcess() *Process {
process := &Process{}
process.Thread = NewThread()
process.cb = make(chan ProcessCaller)
return process
}
// Option ...
func (p *Process) Option(seeder Seeder) {
processOption(p)(seeder)
}
func (p *Process) sliceAdd(unfin *model.Unfinished, format *cmd.StreamFormat, file string) (err error) {
//var sa *cmd.SplitArgs
//s := int64(0) // p.scale
//if s != 0 {
// res := format.ResolutionInt()
// if int64(res) < s {
// s = int64(res)
// }
// sa, err = cmd.FFMpegSplitToM3U8(nil, file, cmd.StreamFormatOption(format), cmd.ScaleOption(s), cmd.OutputOption(p.workspace))
//
//} else {
// sa, err = cmd.FFMpegSplitToM3U8(nil, file, cmd.StreamFormatOption(format), cmd.OutputOption(p.workspace))
//}
//
//if err != nil {
// return err
//}
//log.Infof("%+v", sa)
//
//dirs, err := p.shell.AddDir(sa.Output)
//if err != nil {
// return err
//}
//
//last := unfin.Object.ParseLinks(dirs)
//if last != nil {
// unfin.Hash = last.Hash
//}
return model.AddOrUpdateUnfinished(nil, unfin)
}
func (p *Process) fileAdd(unfin *model.Unfinished, file string) (err error) {
//object, err := p.shell.AddFile(file)
//if err != nil {
// log.Error(err)
// return
//}
//unfin.Hash = object.Hash
//unfin.Object.Link = model.ObjectToVideoLink(object)
return model.AddOrUpdateUnfinished(nil, unfin)
}
// OnlyName ...
func OnlyName(name string) string {
_, name = filepath.Split(name)
for i := len(name) - 1; i >= 0 && !os.IsPathSeparator(name[i]); i-- {
if name[i] == '.' {
return name[:i]
}
}
return ""
}
// OnlyNo ...
func OnlyNo(name string) string {
s := []rune(OnlyName(name))
last := len(s) - 1
if last > 0 && unicode.IsLetter(s[last]) {
if s[last-1] == rune('-') {
return string(s[:last-1])
}
//return string(s[:last])
}
return string(s)
}
// RelateList ...
const relateList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// IndexNumber ...
func IndexNumber(index int) byte {
if index > len(relateList) {
return relateList[0]
}
return relateList[index]
}
// NumberIndex ...
func NumberIndex(name string) int {
size := len(name)
if size > 0 {
return strings.Index(relateList, LastSlice(name, "@"))
}
return -1
}
// LastSlice ...
func LastSlice(s, sep string) string {
ss := strings.Split(s, sep)
for i := len(ss) - 1; i >= 0; i-- {
if ss[i] == "" {
continue
}
return ss[i]
}
return ""
}
// Run ...
func (p *Process) Run(ctx context.Context) {
log.Info("process running")
ProcessEnd:
for {
select {
case <-ctx.Done():
break ProcessEnd
case v := <-p.cb:
if v == nil {
break ProcessEnd
}
p.SetState(StateRunning)
log.Info("process call")
e := v.Call(p)
if e != nil {
log.Error(e)
}
case <-time.After(TimeOutLimit):
log.Info("process time out")
p.SetState(StateWaiting)
}
}
close(p.cb)
p.Finished()
}
// PathMD5 ...
func PathMD5(s ...string) string {
str := filepath.Join(s...)
return fmt.Sprintf("%x", md5.Sum([]byte(str)))
}
func (p *Process) getFiles(ws string) (files []string) {
info, e := os.Stat(ws)
if e != nil {
return nil
}
if info.IsDir() {
file, e := os.Open(ws)
if e != nil {
return nil
}
defer file.Close()
names, e := file.Readdirnames(-1)
if e != nil {
return nil
}
var fullPath string
for _, name := range names {
fullPath = filepath.Join(ws, name)
//if p.CheckIgnore(fullPath) {
// continue
//}
tmp := p.getFiles(fullPath)
if tmp != nil {
files = append(files, tmp...)
}
}
return files
}
return append(files, ws)
}
func (p *Process) skip(format *cmd.StreamFormat) bool {
//if !p.skipConvert {
// log.Info("noskip")
// return p.skipConvert
//}
video := format.Video()
audio := format.Audio()
if audio == nil || video == nil {
log.Info("isMedia")
return true
}
if video.CodecName != "h264" || audio.CodecName != "aac" {
log.Info("isMedia")
return true
}
log.Info("noskip")
return false
}
func moveSuccess(file string) (e error) {
dir, name := filepath.Split(file)
newPath := filepath.Join(dir, "success")
_ = os.MkdirAll(newPath, os.ModePerm)
newPathFile := filepath.Join(newPath, name)
return os.Rename(file, newPathFile)
}
// MustString must string
func MustString(val, src string) string {
if val != "" {
return val
}
return src
}
// processOption ...
func processOption(process *Process) Options {
return func(seed Seeder) {
seed.SetBaseThread(StepperProcess, process)
}
}