-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaidu.go
69 lines (56 loc) · 1.54 KB
/
baidu.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
package speech
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/url"
"github.com/tiechui1994/tool/util"
)
/**
百度语音转换测试API, 支持中文,英文
网站: https://developer.baidu.com/vcast
**/
// https://developer.baidu.com/vcast 登录百度账号, 获取 Cookie 信息
var COOKIE = ""
func TextToSpeed(text string, filename string) (err error) {
values := make(url.Values)
values.Set("title", text)
values.Set("content", ".")
values.Set("sex", "0") // 0 非情感女声, 1 非情感男声 3 情感男声 4 情感女声
values.Set("speed", "6") // 声速: 0-10
values.Set("volumn", "8") // 声音大小
values.Set("pit", "8")
values.Set("method", "TRADIONAL")
u := "https://developer.baidu.com/vcast/getVcastInfo"
header := map[string]string{
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Cookie": COOKIE,
}
data, err := util.POST(u, util.WithBody(values.Encode()), util.WithHeader(header))
if err != nil {
return err
}
log.Printf("uploadtext: %s", data)
var res struct {
BosUrl string `json:"bosUrl"`
Status string `json:"status"`
}
err = json.Unmarshal(data, &res)
if err != nil {
return err
}
if res.Status != "success" {
log.Printf("failed: %v", string(data))
return fmt.Errorf("")
}
return download(res.BosUrl, filename)
}
func download(u string, filename string) (err error) {
data, err := util.GET(u)
if err != nil {
return err
}
return ioutil.WriteFile(filename+".mp3", data, 0666)
}