-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.go
211 lines (194 loc) · 5.34 KB
/
audio.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
package main
import (
"bytes"
"context"
"fmt"
"github.com/sashabaranov/go-openai"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
func TTS(ctx context.Context, voice string, content string) (audioUrl string, err error) {
// TODO 先直接生成tts 后续对长句子切分生成
client := GetOpenaiClient()
speech, err := client.CreateSpeech(ctx, openai.CreateSpeechRequest{
Model: openai.TTSModel1,
Input: content,
Voice: openai.VoiceAlloy,
//ResponseFormat: openai.SpeechResponseFormatMp3, // default mp3
})
if err != nil {
fmt.Println(err)
return
}
defer speech.Close()
buf, err := io.ReadAll(speech)
if err != nil {
return
}
audioUrl = fmt.Sprintf("./tmp/audio/%d.mp3", time.Now().UnixNano())
err = os.MkdirAll(filepath.Dir(audioUrl), os.ModePerm)
if err != nil {
return
}
// save buf to file as mp3
err = os.WriteFile(audioUrl, buf, 777)
if err != nil {
return
}
return audioUrl, nil
}
type MergeAudioResponse struct {
Data struct {
FileRequestId string `json:"FileRequestId"`
} `json:"Data"`
Code int `json:"Code"`
Message interface{} `json:"Message"`
Action interface{} `json:"Action"`
SessionId interface{} `json:"SessionId"`
}
type HandleStatusResponse struct {
Data struct {
Status int `json:"Status"`
Message interface{} `json:"Message"`
FileName string `json:"FileName"`
FolderName string `json:"FolderName"`
DownloadLink string `json:"DownloadLink"`
} `json:"Data"`
Code int `json:"Code"`
Message interface{} `json:"Message"`
Action interface{} `json:"Action"`
SessionId interface{} `json:"SessionId"`
}
// MergeAudio https://products.aspose.app/audio/zh-cn/merger/api
func MergeAudio(ctx context.Context, audioUrls []string) (audioUrl string, err error) {
var (
header = make(http.Header)
param = url.Values{}
bodyIdx = 1
)
header.Set("authority", "api.products.aspose.app")
param.Set("audioFormat", "mp3")
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for _, audio := range audioUrls {
// 打开要上传的文件
file, err := os.Open(audio)
if err != nil {
fmt.Println(err)
return "", err
}
// 创建一个文件表单字段并将文件内容写入其中
part, err := writer.CreateFormFile(fmt.Sprint(bodyIdx), file.Name())
if err != nil {
fmt.Println(err)
return "", err
}
if _, err = io.Copy(part, file); err != nil {
fmt.Println(err)
return "", err
}
file.Close()
bodyIdx++
}
// 关闭multipart writer
if err = writer.Close(); err != nil {
return
}
request, err := BuildRequest(ctx, http.MethodGet, "https://api.products.aspose.app/audio/merger/api/merger", body, header, param)
if err != nil {
return "", err
}
var resp MergeAudioResponse
err = SendRequest(request, &http.Client{}, &resp)
if err != nil {
fmt.Println(err)
fmt.Println(111)
return "", err
}
requestId := resp.Data.FileRequestId
param = url.Values{}
param.Set("fileRequestId", requestId)
for {
req, err := BuildRequest(ctx, http.MethodGet, "https://api.products.aspose.app/audio/merger/api/merger/HandleStatus", nil, header, param)
if err != nil {
fmt.Println(err)
return "", err
}
var resp0 HandleStatusResponse
err = SendRequest(req, &http.Client{}, &resp0)
if err != nil {
return "", err
}
if resp0.Data.Status == 0 {
// 转换完成
link := resp0.Data.DownloadLink
// 下载本地
localAudio, err := SaveFileLocal(link, "./tmp/audio/", fmt.Sprintf("%d.mp3", time.Now().UnixNano()))
if err != nil {
return "", err
}
return localAudio, nil
}
}
}
func CombinedAudioByFfmpeg(audioUrls []string) (mergeAudioUrl string, err error) {
var (
mergeAudioDir = "./tmp/mergeAudio/"
)
// 给url加上绝对路径
pwd, err := os.Getwd()
if err != nil {
return
}
for i, audioUrl := range audioUrls {
audioUrl = audioUrl[1:] // 去掉相对路径的点
audioUrls[i] = fmt.Sprintf("%s%s", pwd, audioUrl)
}
// 合并所有文件
txtFile, err := CreateTxtFileWithDynamicContent(audioUrls)
if err != nil {
return
}
mergeAudioUrl = fmt.Sprintf("%s%d.mp3", mergeAudioDir, time.Now().UnixNano())
err = os.MkdirAll(filepath.Dir(mergeAudioUrl), os.ModePerm)
if err != nil {
return
}
cmd := fmt.Sprintf(`ffmpeg -f concat -safe 0 -i %s -c copy %s`, txtFile, mergeAudioUrl)
command := exec.Command("/bin/bash", "-c", cmd)
_, err = command.CombinedOutput()
if err != nil {
return
}
return
}
/*
GetAudioDuration 利用ffmpeg获取音频持续时间
*/
func GetAudioDuration(audio string) time.Duration {
// 格式转换 ffmpeg -i xxxx 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//
cmd := fmt.Sprintf("ffmpeg -i %s 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//", audio)
command := exec.Command("/bin/bash", "-c", cmd)
res, err := command.CombinedOutput()
if err != nil {
fmt.Println(err)
}
body := string(res)
parts := strings.Split(body, ":")
hours, _ := strconv.Atoi(parts[0])
minutes, _ := strconv.Atoi(parts[1])
secondsAndMilliseconds := strings.Split(parts[2], ".")
seconds, _ := strconv.Atoi(secondsAndMilliseconds[0])
milliseconds, _ := strconv.ParseFloat("0."+strings.TrimSpace(secondsAndMilliseconds[1]), 64)
totalSeconds := float64(hours*3600+minutes*60+seconds) + milliseconds
duration := time.Duration(totalSeconds * float64(time.Second))
return duration
}