-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
352 lines (293 loc) · 8.36 KB
/
main.go
File metadata and controls
352 lines (293 loc) · 8.36 KB
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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// DownloadTask 下载任务状态
type DownloadTask struct {
ID string `json:"download_id"`
Status string `json:"status"`
Percentage int `json:"percentage"`
Speed *string `json:"speed"`
ElapsedTime int `json:"elapsed_time"`
FilePath *string `json:"file_path"`
FileName *string `json:"file_name"`
Error *string `json:"error"`
StartTime time.Time `json:"-"`
}
// TranscribeTask 转录任务状态
type TranscribeTask struct {
ID string `json:"task_id"`
Status string `json:"status"`
Percentage int `json:"percentage"`
Stage *string `json:"stage"`
ElapsedTime int `json:"elapsed_time"`
VideoPath string `json:"-"`
MP3Path *string `json:"mp3_path"`
TxtPath *string `json:"txt_path"`
Error *string `json:"error"`
StartTime time.Time `json:"-"`
}
var (
tasks = make(map[string]*DownloadTask)
transcribes = make(map[string]*TranscribeTask)
mu = &sync.RWMutex{}
)
func main() {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
// 跨域支持
router.Use(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
})
// API 路由
router.GET("/api/health", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": "ok",
"authenticated": true,
})
})
router.POST("/api/download", func(c *gin.Context) {
var req struct {
URL string `json:"url" binding:"required"`
Quality string `json:"quality"`
OutputPath string `json:"output_path"`
}
if err := c.BindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if req.Quality == "" {
req.Quality = "hd"
}
taskID := uuid.New().String()
task := &DownloadTask{
ID: taskID,
Status: "Starting",
StartTime: time.Now(),
}
mu.Lock()
tasks[taskID] = task
mu.Unlock()
// 在 goroutine 中执行下载
go downloadVideo(taskID, req.URL, req.Quality, req.OutputPath)
c.JSON(200, gin.H{"download_id": taskID})
})
router.GET("/api/progress/:download_id", func(c *gin.Context) {
downloadID := c.Param("download_id")
mu.RLock()
task, exists := tasks[downloadID]
mu.RUnlock()
if !exists {
c.JSON(404, gin.H{"error": "任务不存在"})
return
}
c.JSON(200, task)
})
router.POST("/api/download/:download_id/cancel", func(c *gin.Context) {
downloadID := c.Param("download_id")
mu.Lock()
if task, exists := tasks[downloadID]; exists {
if task.Status == "Downloading" {
task.Status = "Cancelled"
errMsg := "用户取消"
task.Error = &errMsg
}
}
mu.Unlock()
c.JSON(200, gin.H{"status": "cancelled"})
})
// 转录相关路由
router.POST("/api/transcribe", func(c *gin.Context) {
var req struct {
VideoPath string `json:"video_path" binding:"required"`
Language string `json:"language"`
}
if err := c.BindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if req.Language == "" {
req.Language = "zh"
}
taskID := uuid.New().String()
task := &TranscribeTask{
ID: taskID,
Status: "pending",
VideoPath: req.VideoPath,
StartTime: time.Now(),
}
mu.Lock()
transcribes[taskID] = task
mu.Unlock()
// 在 goroutine 中执行转录
go transcribeVideo(taskID, req.VideoPath, req.Language)
c.JSON(200, gin.H{"task_id": taskID})
})
router.GET("/api/transcribe/:task_id", func(c *gin.Context) {
taskID := c.Param("task_id")
mu.RLock()
task, exists := transcribes[taskID]
mu.RUnlock()
if !exists {
c.JSON(404, gin.H{"error": "任务不存在"})
return
}
c.JSON(200, task)
})
fmt.Println("✓ 服务启动在 http://127.0.0.1:5124 (Go 网关 + ffmpeg + Whisper)")
router.Run("127.0.0.1:5124")
}
// downloadVideo 下载视频(调用 ffmpeg)
func downloadVideo(taskID, url, quality, outputPath string) {
mu.Lock()
task := tasks[taskID]
task.Status = "Downloading"
mu.Unlock()
if outputPath == "" {
outputPath = filepath.Join(os.Getenv("HOME"), "Downloads")
}
os.MkdirAll(outputPath, 0755)
outputFile := filepath.Join(outputPath, fmt.Sprintf("video_%s.mp4", taskID[:8]))
// 启动 ffmpeg 下载
cmd := exec.Command("ffmpeg", "-y", "-i", url, "-c", "copy", "-progress", "pipe:1", outputFile)
stdout, _ := cmd.StdoutPipe()
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "progress=") {
mu.Lock()
if task.Status == "Downloading" {
task.Percentage = min(99, task.Percentage+1)
task.ElapsedTime = int(time.Since(task.StartTime).Seconds())
if task.ElapsedTime > 0 && task.Percentage > 0 {
speedKb := float64(task.Percentage) / float64(task.ElapsedTime) / 100
var speedStr string
if speedKb > 1024 {
speedStr = fmt.Sprintf("%.1f MB/s", speedKb/1024)
} else {
speedStr = fmt.Sprintf("%.0f KB/s", speedKb)
}
task.Speed = &speedStr
}
}
mu.Unlock()
}
}
}()
err := cmd.Run()
mu.Lock()
defer mu.Unlock()
if err != nil {
task.Status = "Failed"
errMsg := fmt.Sprintf("下载失败: %v", err)
task.Error = &errMsg
} else {
if info, err := os.Stat(outputFile); err == nil && info.Size() > 0 {
task.Status = "Completed"
task.Percentage = 100
task.FilePath = &outputFile
fileName := filepath.Base(outputFile)
task.FileName = &fileName
fmt.Printf("[%s] 下载完成: %s (%.1f MB)\n", taskID, outputFile, float64(info.Size())/1024/1024)
} else {
task.Status = "Failed"
errMsg := "文件为空或不存在"
task.Error = &errMsg
}
}
}
// transcribeVideo 转录视频(使用 ffmpeg + whisper)
func transcribeVideo(taskID, videoPath, language string) {
mu.Lock()
task := transcribes[taskID]
mu.Unlock()
// 步骤1: 提取音频为 MP3
mu.Lock()
task.Status = "extracting_audio"
stage := "正在提取音频..."
task.Stage = &stage
task.Percentage = 10
mu.Unlock()
mp3Path := strings.TrimSuffix(videoPath, filepath.Ext(videoPath)) + ".mp3"
// 用 ffmpeg 从视频提取音频
cmd := exec.Command("ffmpeg", "-y", "-i", videoPath, "-q:a", "9", mp3Path)
output, err := cmd.CombinedOutput()
if err != nil {
mu.Lock()
task.Status = "failed"
errMsg := fmt.Sprintf("提取音频失败: %v\n输出: %s", err, string(output))
task.Error = &errMsg
mu.Unlock()
fmt.Printf("[%s] 错误: %s\n", taskID, errMsg)
return
}
// 检查 MP3 文件是否真的存在
if _, err := os.Stat(mp3Path); err != nil {
mu.Lock()
task.Status = "failed"
errMsg := fmt.Sprintf("MP3 文件未创建: %v", err)
task.Error = &errMsg
mu.Unlock()
fmt.Printf("[%s] 错误: %s\n", taskID, errMsg)
return
}
fmt.Printf("[%s] 音频提取完成: %s\n", taskID, mp3Path)
// 步骤2: 用 whisper 转录
mu.Lock()
task.Status = "transcribing"
stage = "正在转录(Whisper)..."
task.Stage = &stage
task.Percentage = 50
mu.Unlock()
// 输出目录
outputDir := filepath.Dir(videoPath)
// 调用 whisper CLI(使用完整环境)
whisperCmd := exec.Command("bash", "-c",
fmt.Sprintf("export PATH=/opt/homebrew/bin:$PATH && /opt/homebrew/bin/whisper %q --output_format txt --output_dir %q --language %s --model base 2>&1",
mp3Path, outputDir, language))
output, err = whisperCmd.CombinedOutput()
if err != nil {
mu.Lock()
task.Status = "failed"
errMsg := fmt.Sprintf("Whisper 转录失败: %v\n输出: %s", err, string(output))
task.Error = &errMsg
mu.Unlock()
fmt.Printf("[%s] 错误详情: %s\n", taskID, errMsg)
fmt.Printf("[%s] stderr/stdout: %s\n", taskID, string(output))
return
}
// 查找生成的 txt 文件
txtPath := strings.TrimSuffix(mp3Path, filepath.Ext(mp3Path)) + ".txt"
// 步骤3: 完成
mu.Lock()
task.Status = "completed"
task.Percentage = 100
task.MP3Path = &mp3Path
task.TxtPath = &txtPath
task.ElapsedTime = int(time.Since(task.StartTime).Seconds())
mu.Unlock()
fmt.Printf("[%s] 转录完成!\n MP3: %s\n TXT: %s\n 耗时: %ds\n", taskID, mp3Path, txtPath, task.ElapsedTime)
}
func min(a, b int) int {
if a < b {
return a
}
return b
}