forked from wxbool/video-srt-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
379 lines (333 loc) · 9.46 KB
/
main.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"log"
"runtime"
"strings"
"time"
. "videosrt/app"
"videosrt/app/ffmpeg"
"videosrt/app/tool"
)
//应用版本号
const APP_VERSION = "0.1.2"
var AppRootDir string
var mw *MyMainWindow
func init() {
//设置可同时执行的最大CPU数
runtime.GOMAXPROCS(runtime.NumCPU())
//MY Window
mw = new(MyMainWindow)
AppRootDir = GetAppRootDir()
if AppRootDir == "" {
panic("应用根目录获取失败")
}
//校验ffmpeg环境
if e := ffmpeg.VailFfmpegLibrary(); e != nil {
//尝试自动引入 ffmpeg 环境
ffmpeg.VailTempFfmpegLibrary(AppRootDir)
}
}
func main() {
var taskFiles = new(TaskHandleFile)
var logText *walk.TextEdit
var operateDb *walk.DataBinder
var operateFrom = new(OperateFrom)
var startBtn *walk.PushButton
var engineOtionsBox *walk.ComboBox
var dropFilesEdit *walk.TextEdit
var appSetings = GetCacheAppSetingsData()
if appSetings.CurrentEngineId != 0 {
operateFrom.EngineId = appSetings.CurrentEngineId
}
var videosrt = NewApp(AppRootDir)
var tasklog = NewTasklog()
//注册日志事件
videosrt.SetLogHandler(func(s string, video string) {
baseName := tool.GetFileBaseName(video)
//fmt.Println("日志:" , s , baseName)
//fmt.Println("\r\n")
//fmt.Println("\r\n")
//fmt.Println("\r\n")
strs := strings.Join([]string{"【" , baseName , "】" , s} , "")
//追加日志
tasklog.AppendLogText(strs)
})
//字幕输出目录
videosrt.SetSrtDir(appSetings.SrtFileDir)
//注册多任务
var multitask = NewVideoMultitask(appSetings.MaxConcurrency)
if err := (MainWindow{
AssignTo: &mw.MainWindow,
Icon:"./data/img/index.png",
Title: "VideoSrt - 识别视频语音生成字幕文件的小工具" + " - " + APP_VERSION,
ToolBar: ToolBar{
ButtonStyle: ToolBarButtonImageBeforeText,
Items: []MenuItem{
Menu{
Text: "新建",
Image: "./data/img/new.png",
Items: []MenuItem{
Action{
Image: "./data/img/voice.png",
Text: "语音引擎",
OnTriggered: func() {
mw.RunSpeechEngineSetingDialog(mw , func() {
thisData := GetEngineOtionsSelects()
if appSetings.CurrentEngineId == 0 {
appSetings.CurrentEngineId = thisData[0].Id
//更新缓存
SetCacheAppSetingsData(appSetings)
}
//重新加载选项
_ = engineOtionsBox.SetModel(thisData)
//重置index
engIndex := GetCurrentIndex(thisData , appSetings.CurrentEngineId)
if engIndex != -1 {
_ = engineOtionsBox.SetCurrentIndex(engIndex)
}
operateFrom.EngineId = appSetings.CurrentEngineId
})
},
},
},
},
Menu{
Text: "设置",
Image: "./data/img/setings.png",
Items: []MenuItem{
Action{
Text: "Oss对象存储设置",
Image: "./data/img/oss.png",
OnTriggered: func() {
mw.RunObjectStorageSetingDialog(mw)
},
},
Action{
Text: "软件设置",
Image: "./data/img/app-setings.png",
OnTriggered: func() {
mw.RunAppSetingDialog(mw , func(setings *AppSetings) {
//更新配置
appSetings.MaxConcurrency = setings.MaxConcurrency
appSetings.SrtFileDir = setings.SrtFileDir
videosrt.SetSrtDir( setings.SrtFileDir )
multitask.SetMaxConcurrencyNumber( setings.MaxConcurrency )
})
},
},
},
},
Menu{
Text: "关于/支持",
Image: "./data/img/about.png",
Items: []MenuItem{
Action{
Text: "github",
Image: "./data/img/github.png",
OnTriggered: mw.OpenAboutGithub,
},
Action{
Text: "gitee",
Image: "./data/img/gitee.png",
OnTriggered: mw.OpenAboutGitee,
},
},
},
},
},
Size: Size{800, 450},
MinSize: Size{300, 280},
Layout: VBox{},
Children: []Widget{
HSplitter{
Children: []Widget{
Composite{
DataBinder: DataBinder{
AssignTo: &operateDb,
DataSource: operateFrom,
},
Layout: Grid{Columns: 6},
Children: []Widget{
Label{
Text: "语音引擎:",
},
ComboBox{
AssignTo:&engineOtionsBox,
Value: Bind("EngineId", SelRequired{}),
BindingMember: "Id",
DisplayMember: "Name",
Model: GetEngineOtionsSelects(),
OnCurrentIndexChanged: func() {
operateDb.Submit()
//fmt.Println( "OnCurrentIndexChanged:" , operateFrom.EngineId ,appSetings.CurrentEngineId )
if operateFrom.EngineId == 0 {
return
}
appSetings.CurrentEngineId = operateFrom.EngineId
//更新缓存
SetCacheAppSetingsData(appSetings)
},
},
PushButton{
Text: "删除引擎",
MaxSize:Size{80 , 55},
OnClicked: func() {
var thisEngineOptions = make([]*EngineSelects , 0)
thisEngineOptions = GetEngineOtionsSelects()
//删除校验
if appSetings.CurrentEngineId == 0 {
mw.NewErrormationTips("错误" , "请先选择要操作的语音引擎")
return
}
if len(thisEngineOptions) <= 1 {
mw.NewErrormationTips("错误" , "不允许删除最后一个语音引擎")
return
}
//删除引擎
if ok := RemoveCacheAliyunEngineData(appSetings.CurrentEngineId);ok == false {
//删除失败
mw.NewErrormationTips("错误" , "语音引擎删除失败")
return
}
thisEngineOptions = GetEngineOtionsSelects()
//fmt.Println( "thisEngineOptions" , thisEngineOptions[0] )
//重新加载列表
_ = engineOtionsBox.SetModel(thisEngineOptions)
appSetings.CurrentEngineId = thisEngineOptions[0].Id
operateFrom.EngineId = appSetings.CurrentEngineId
//更新缓存
SetCacheAppSetingsData(appSetings)
//更新下标
_ = engineOtionsBox.SetCurrentIndex(0)
},
},
},
},
},
},
HSplitter{
Children: []Widget{
TextEdit{
AssignTo: &dropFilesEdit,
ReadOnly: true,
Text: "将需要生成字幕的视频文件,拖入放到这里",
},
TextEdit{
AssignTo: &logText,
ReadOnly: true,
Text:"",
//HScroll:true,
VScroll:true,
},
},
},
PushButton{
AssignTo: &startBtn,
Text: "生成字幕文件",
MinSize:Size{500 , 50},
OnClicked: func() {
//设置随机种子
tool.SetRandomSeed()
tlens := len(taskFiles.Files)
if tlens == 0 {
mw.NewErrormationTips("错误" , "请先拖入要处理的视频文件")
return
}
ossData := GetCacheAliyunOssData()
if ossData.Endpoint == "" {
mw.NewErrormationTips("错误" , "请先设置Oss对象配置")
return
}
engineIndex := GetCacheAppSetingsData()
if engineIndex.CurrentEngineId == 0 {
mw.NewErrormationTips("错误" , "请先新建/选择语音引擎")
return
}
currentEngine , ok := GetEngineById(engineIndex.CurrentEngineId)
if !ok {
mw.NewErrormationTips("错误" , "你选择的语音引擎不存在")
return
}
//加载配置
videosrt.InitConfig(ossData , currentEngine)
multitask.SetVideoSrt(videosrt)
//设置队列
multitask.SetQueueFile(taskFiles.Files)
var finish = false
startBtn.SetEnabled(false)
startBtn.SetText("任务运行中,请勿关闭软件窗口...")
//清除log
tasklog.ClearLogText()
tasklog.AppendLogText("任务开始... \r\n")
//运行
multitask.Run()
//回调链式执行
videosrt.SetFailHandler(func(video string) {
//运行下一任务
multitask.RunOver()
//任务完成
if ok := multitask.FinishTask(); ok && finish == false {
//延迟结束
go func() {
time.Sleep(time.Second)
finish = true
startBtn.SetEnabled(true)
startBtn.SetText("生成字幕文件")
logText.AppendText("\r\n\r\n任务完成!")
//清空临时目录
videosrt.ClearTempDir()
}()
}
})
videosrt.SetSuccessHandler(func(video string) {
//运行下一任务
multitask.RunOver()
//任务完成
if ok := multitask.FinishTask(); ok && finish == false {
//延迟结束
go func() {
time.Sleep(time.Second)
finish = true
startBtn.SetEnabled(true)
startBtn.SetText("生成字幕文件")
logText.AppendText("\r\n\r\n任务完成!")
//清空临时目录
videosrt.ClearTempDir()
}()
}
})
//日志输出
go func() {
for finish == false {
logText.SetText("")
logText.AppendText(tasklog.GetString())
time.Sleep(time.Millisecond * 150)
}
}()
},
},
},
OnDropFiles: func(files []string) {
//检测文件列表
result , err := VaildateHandleFiles(files)
if err != nil {
mw.NewErrormationTips("错误" , err.Error())
return
}
taskFiles.Files = result
dropFilesEdit.SetText(strings.Join(result, "\r\n"))
},
}.Create()); err != nil {
log.Fatal(err)
time.Sleep(1 * time.Second)
}
//校验依赖库
if e := ffmpeg.VailFfmpegLibrary(); e != nil {
mw.NewErrormationTips("错误" , "请先下载并安装 ffmpeg 软件,才可以正常使用软件哦")
tool.OpenUrl("https://gitee.com/641453620/video-srt")
return
}
mw.Run()
}