File size: 4,634 Bytes
6dc64a1
 
 
 
 
 
 
 
c24cfde
6dc64a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c24cfde
6dc64a1
c24cfde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dc64a1
c24cfde
 
 
6dc64a1
c24cfde
6dc64a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c24cfde
6dc64a1
 
c24cfde
 
 
 
6dc64a1
c24cfde
 
6dc64a1
 
 
 
c24cfde
6dc64a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c24cfde
6dc64a1
 
 
 
c24cfde
 
 
 
 
 
 
 
 
 
 
 
 
6dc64a1
 
c24cfde
 
 
 
 
6dc64a1
 
 
 
c24cfde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json
import hmac
import hashlib
import time
import base64
import urllib
import urllib3
import gradio as gr
# 加密函数
def hmac_sha256_encrypt(message, key):
    message = message.encode('utf-8')
    key = key.encode('utf-8')
    # 计算HMAC值
    hashed = hmac.new(key, message, hashlib.sha256).digest()
    return hashed
    
# // 声明accessToken
appkey = '4e8f2e8492c947da9c3738b74b4ac2fa'
accessToken = '6b4c98203218457fbe276e13990f3550';
reqTimestamp = "";
reqSignature = '';
def  pre_request():
#   // 获取当前时间戳


  timestamp = str (round(time.time()));
  # timestamp = str (1691034072);
  
#   // 设置本次请求的签名时使用的时间戳,单位:秒,时间要求在 UTC/GMT 零时区时间的前后 5 分钟内
  global reqTimestamp
  reqTimestamp = timestamp;
#   // 获取待计算签名的内容
  signingContent = "appkey="+ appkey +"&timestamp="+timestamp;
  
#   // 计算 HMAC-SHA256 值
  hash = hmac_sha256_encrypt(signingContent, accessToken);
  
#   // 将签名内容转换16进制
  hashInBase64 = base64.b64encode(hash).decode('utf-8');
# url encode  签名串  用于拼接请求地址        
  hashInBase64.encode('utf-8')
  encodeSign = urllib.parse.quote_plus(hashInBase64)

#   // 设置本次请求的签名签名串
  global reqSignature
  reqSignature = encodeSign;
  # print(reqTimestamp, reqSignature, appkey)

def req_audioMake(text):
  pre_request()
  url = "https://gw.tvs.qq.com/v2/ivh/videomaker/broadcastservice/tts?timestamp="+reqTimestamp+"&signature="+reqSignature+"&appkey="+appkey

  payload = json.dumps({
    "Header": {},
    "Payload": {
      "TimbreKey": "",
      "VirtualmanKey": "36b88108f6de6da255534eaf4363c7d7",
      "InputSsml": text,
      "Speed": 1
    }
  })
  headers = {
    'Content-Type': 'application/json'
  }

  response = requests.request("POST", url, headers=headers, data=payload)
  
  res = json.loads(response.text)
  if res["Header"]["Code"] != 0:
    print(response.text)
    return 

  taskID = (res["Payload"]["TaskId"])
  print(taskID)
  return taskID

def req_videoMake(text = "你好"):
    pre_request()
    taskID = ""

    # 调用视颯合户接口,获取taskID
    # 调用视颯合户接口,获取taskID
    url = "https://gw.tvs.qq.com/v2/ivh/videomaker/broadcastservice/videomake?timestamp="+reqTimestamp+"&signature="+reqSignature+"&appkey="+appkey
    payload = json.dumps({
    "Header": {},
    "Payload": {
        "VirtualmanKey": "36b88108f6de6da255534eaf4363c7d7",
        "InputSsml": text,
        "SpeechParam": {
        "Speed": 1,
        "TimbreKey": "female_1"
        },
        "VideoParam": {
        "Format": "GreenScreenMp4"
        }
    }
    })
    

    headers = {
    'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    res = json.loads(response.text)
    if res["Header"]["Code"] != 0:
      print(response.text)
      return 

    taskID = (res["Payload"]["TaskId"])
    print(taskID)
    return taskID



def get_media(taskID):

  pre_request()
  url = "https://gw.tvs.qq.com/v2/ivh/videomaker/broadcastservice/getprogress?timestamp="+reqTimestamp+"&signature="+reqSignature+"&appkey="+appkey

  payload = json.dumps({
    "Header": {},
    "Payload": {
      "TaskId": taskID
    }
  })
  headers = {
    'Content-Type': 'application/json'
  }

  response = requests.request("POST", url, headers=headers, data=payload)

  # print(response.text)
  return response.text


def generate_video(text = "你好"):
    taskID = req_videoMake(text)
    # print(taskID)

    sleepSecond = 5
    time.sleep(sleepSecond)
    videoRep = json.loads(get_media(taskID))
 
    status = videoRep["Payload"]["Status"]

    while status == "COMMIT" or status == "MAKING":
        time.sleep(1)
        videoRep = json.loads(get_media(taskID))
        status = videoRep["Payload"]["Status"]
        print(status)

    if status == "FAIL":
        print(videoRep)
        return
 
    mediaUrl = videoRep["Payload"]["MediaUrl"]

    return mediaUrl


def generate_audio(text):
    taskID = req_audioMake(text)
    # print(taskID)

    sleepSecond = 5
    time.sleep(sleepSecond)
    videoRep = json.loads(get_media(taskID))
 
    status = videoRep["Payload"]["Status"]

    while status == "COMMIT" or status == "MAKING":
        time.sleep(1)
        videoRep = json.loads(get_media(taskID))
        status = videoRep["Payload"]["Status"]
        print(status)

    if status == "FAIL":
        print(videoRep)
        return
 
    mediaUrl = videoRep["Payload"]["MediaUrl"]

    return mediaUrl