|
@@ -1,97 +1,95 @@
|
|
-# -*- coding: UTF-8 -*-
|
|
|
|
-# Python 2.x引入httplib模块。
|
|
|
|
-# import httplib
|
|
|
|
-# Python 3.x引入http.client模块。
|
|
|
|
-import http.client
|
|
|
|
-# Python 2.x引入urllib模块。
|
|
|
|
-# import urllib
|
|
|
|
-# Python 3.x引入urllib.parse模块。
|
|
|
|
-import urllib.parse
|
|
|
|
|
|
+import subprocess
|
|
|
|
+
|
|
|
|
+import requests
|
|
import json
|
|
import json
|
|
-def processGETRequest(appKey, token, text, audioSaveFile, format, sampleRate) :
|
|
|
|
- host = 'nls-gateway-cn-shanghai.aliyuncs.com'
|
|
|
|
- url = 'https://' + host + '/stream/v1/tts'
|
|
|
|
- # 设置URL请求参数
|
|
|
|
- url = url + '?appkey=' + appKey
|
|
|
|
- url = url + '&token=' + token
|
|
|
|
- url = url + '&text=' + text
|
|
|
|
- url = url + '&format=' + format
|
|
|
|
- url = url + '&sample_rate=' + str(sampleRate)
|
|
|
|
- # voice 发音人,可选,默认是xiaoyun。
|
|
|
|
- # url = url + '&voice=' + 'xiaoyun'
|
|
|
|
- # volume 音量,范围是0~100,可选,默认50。
|
|
|
|
- # url = url + '&volume=' + str(50)
|
|
|
|
- # speech_rate 语速,范围是-500~500,可选,默认是0。
|
|
|
|
- # url = url + '&speech_rate=' + str(0)
|
|
|
|
- # pitch_rate 语调,范围是-500~500,可选,默认是0。
|
|
|
|
- # url = url + '&pitch_rate=' + str(0)
|
|
|
|
- print(url)
|
|
|
|
- # Python 2.x请使用httplib。
|
|
|
|
- # conn = httplib.HTTPSConnection(host)
|
|
|
|
- # Python 3.x请使用http.client。
|
|
|
|
- conn = http.client.HTTPSConnection(host)
|
|
|
|
- conn.request(method='GET', url=url)
|
|
|
|
- # 处理服务端返回的响应。
|
|
|
|
- response = conn.getresponse()
|
|
|
|
- print('Response status and response reason:')
|
|
|
|
- print(response.status ,response.reason)
|
|
|
|
- contentType = response.getheader('Content-Type')
|
|
|
|
- print(contentType)
|
|
|
|
- body = response.read()
|
|
|
|
- if 'audio/mpeg' == contentType :
|
|
|
|
- with open(audioSaveFile, mode='wb') as f:
|
|
|
|
- f.write(body)
|
|
|
|
- print('The GET request succeed!')
|
|
|
|
- else :
|
|
|
|
- print('The GET request failed: ' + str(body))
|
|
|
|
- conn.close()
|
|
|
|
-def processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate) :
|
|
|
|
- host = 'nls-gateway-cn-shanghai.aliyuncs.com'
|
|
|
|
- url = 'https://' + host + '/stream/v1/tts'
|
|
|
|
- # 设置HTTPS Headers。
|
|
|
|
- httpHeaders = {
|
|
|
|
- 'Content-Type': 'application/json'
|
|
|
|
- }
|
|
|
|
- # 设置HTTPS Body。
|
|
|
|
- body = {'appkey': appKey, 'token': token, 'text': text, 'format': format, 'sample_rate': sampleRate}
|
|
|
|
- body = json.dumps(body)
|
|
|
|
- print('The POST request body content: ' + body)
|
|
|
|
- # Python 2.x请使用httplib。
|
|
|
|
- # conn = httplib.HTTPSConnection(host)
|
|
|
|
- # Python 3.x请使用http.client。
|
|
|
|
- conn = http.client.HTTPSConnection(host)
|
|
|
|
- conn.request(method='POST', url=url, body=body, headers=httpHeaders)
|
|
|
|
- # 处理服务端返回的响应。
|
|
|
|
- response = conn.getresponse()
|
|
|
|
- print('Response status and response reason:')
|
|
|
|
- print(response.status ,response.reason)
|
|
|
|
- contentType = response.getheader('Content-Type')
|
|
|
|
- print(contentType)
|
|
|
|
- body = response.read()
|
|
|
|
- if 'audio/mpeg' == contentType :
|
|
|
|
- with open(audioSaveFile, mode='wb') as f:
|
|
|
|
- f.write(body)
|
|
|
|
- print('The POST request succeed!')
|
|
|
|
- else :
|
|
|
|
- print('The POST request failed: ' + str(body))
|
|
|
|
- conn.close()
|
|
|
|
-appKey = 'KZChElSawT5Zue3n'
|
|
|
|
-token = 'fbb04d8f368e4394ac7172a1225c0720'
|
|
|
|
-text = '今天是周五,天气挺好的。'
|
|
|
|
-# 采用RFC 3986规范进行urlencode编码。
|
|
|
|
-textUrlencode = text
|
|
|
|
-# Python 2.x请使用urllib.quote。
|
|
|
|
-# textUrlencode = urllib.quote(textUrlencode, '')
|
|
|
|
-# Python 3.x请使用urllib.parse.quote_plus。
|
|
|
|
-textUrlencode = urllib.parse.quote_plus(textUrlencode)
|
|
|
|
-textUrlencode = textUrlencode.replace("+", "%20")
|
|
|
|
-textUrlencode = textUrlencode.replace("*", "%2A")
|
|
|
|
-textUrlencode = textUrlencode.replace("%7E", "~")
|
|
|
|
-print('text: ' + textUrlencode)
|
|
|
|
-audioSaveFile = '/Users/tzld/Desktop/video_rewriting/common/syAudio.MP3'
|
|
|
|
-format = 'MP3'
|
|
|
|
-sampleRate = 16000
|
|
|
|
-# GET请求方式
|
|
|
|
-processGETRequest(appKey, token, textUrlencode, audioSaveFile, format, sampleRate)
|
|
|
|
-# POST请求方式
|
|
|
|
-# processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate)
|
|
|
|
|
|
+import random
|
|
|
|
+import re
|
|
|
|
+import time
|
|
|
|
+
|
|
|
|
+class TTS:
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def get_pw_zm(cls, text):
|
|
|
|
+ max_retries = 3
|
|
|
|
+ for attempt in range(max_retries):
|
|
|
|
+ url = "https://zh.api.guiji.cn/avatar2c/tool/sec_tts"
|
|
|
|
+
|
|
|
|
+ payload = json.dumps({
|
|
|
|
+ "text": text,
|
|
|
|
+ "speaker_id": "160"
|
|
|
|
+ })
|
|
|
|
+ headers = {
|
|
|
|
+ 'accept': 'application/json, text/plain, */*',
|
|
|
|
+ 'accept-language': 'zh-CN',
|
|
|
|
+ 'cache-control': 'no-cache',
|
|
|
|
+ 'content-type': 'application/json',
|
|
|
|
+ 'cookie': 'anylangIsLogin=true',
|
|
|
|
+ 'origin': 'https://app.guiji.cn',
|
|
|
|
+ 'pragma': 'no-cache',
|
|
|
|
+ 'priority': 'u=1, i',
|
|
|
|
+ 'referer': 'https://app.guiji.cn/',
|
|
|
|
+ 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
|
|
|
|
+ 'sec-ch-ua-mobile': '?0',
|
|
|
|
+ 'sec-ch-ua-platform': '"macOS"',
|
|
|
|
+ 'sec-fetch-dest': 'empty',
|
|
|
|
+ 'sec-fetch-mode': 'cors',
|
|
|
|
+ 'sec-fetch-site': 'same-site',
|
|
|
|
+ 'token': '72912b4ac30b412cb19d2e835a678854'
|
|
|
|
+ }
|
|
|
|
+ wait_time = random.uniform(5, 20)
|
|
|
|
+ time.sleep(wait_time)
|
|
|
|
+ response = requests.request("POST", url, headers=headers, data=payload)
|
|
|
|
+ try:
|
|
|
|
+ response = response.json()
|
|
|
|
+ code = response["code"]
|
|
|
|
+ if code == 200:
|
|
|
|
+ mp3 = response["data"]
|
|
|
|
+ return mp3
|
|
|
|
+ else:
|
|
|
|
+ if attempt == max_retries - 1:
|
|
|
|
+ return None
|
|
|
|
+ except Exception:
|
|
|
|
+ if attempt == max_retries - 1:
|
|
|
|
+ return None
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def get_srt_format(cls, pw_srt_text, pw_url_sec):
|
|
|
|
+ segments = re.split(r'(,|。|!|?)', pw_srt_text)
|
|
|
|
+ segments = [segments[i] + segments[i + 1] for i in range(0, len(segments) - 1, 2)]
|
|
|
|
+ pw_url_sec = int(pw_url_sec) + 1
|
|
|
|
+ # 确定每段显示时间
|
|
|
|
+ num_segments = len(segments)
|
|
|
|
+ duration_per_segment = pw_url_sec / num_segments
|
|
|
|
+ srt_content = ""
|
|
|
|
+ start_time = 0.0
|
|
|
|
+ for i, segment in enumerate(segments):
|
|
|
|
+ end_time = start_time + duration_per_segment
|
|
|
|
+ srt_content += f"{i + 1}\n"
|
|
|
|
+ srt_content += f"{int(start_time // 3600):02}:{int((start_time % 3600) // 60):02}:{int(start_time % 60):02},{int((start_time % 1) * 1000):03} --> "
|
|
|
|
+ srt_content += f"{int(end_time // 3600):02}:{int((end_time % 3600) // 60):02}:{int(end_time % 60):02},{int((end_time % 1) * 1000):03}\n"
|
|
|
|
+ srt_content += f"{segment.strip()}\n\n"
|
|
|
|
+ start_time = end_time
|
|
|
|
+
|
|
|
|
+ print(srt_content)
|
|
|
|
+ return srt_content
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+if __name__ == '__main__':
|
|
|
|
+ # text = "真是太实用了,分享给身边的准妈妈们吧!这些孕期禁忌一定要记住,赶紧转发给更多人,帮助更多的宝妈们。一起为宝宝的健康加油!"
|
|
|
|
+ # mp3 = TTS.get_pw_zm(text)
|
|
|
|
+ # command = [
|
|
|
|
+ # 'ffmpeg',
|
|
|
|
+ # '-i', mp3,
|
|
|
|
+ # '-q:a', '0',
|
|
|
|
+ # '-map', 'a',
|
|
|
|
+ # # '-codec:a', 'libmp3lame', # 指定 MP3 编码器
|
|
|
|
+ # "/Users/tzld/Desktop/video_rewriting/path/pw_video.mp3"
|
|
|
|
+ # ]
|
|
|
|
+ # subprocess.run(command)
|
|
|
|
+ # print("完成")
|
|
|
|
+ video_file = 'https://digital-public.obs.myhuaweicloud.com/vcm_server/20240715/KnOgRTYKvZbWD2EX_ms/FwA2SEh7DZQGlwAe.wav'
|
|
|
|
+ result = subprocess.run(
|
|
|
|
+ ["ffprobe", "-v", "error", "-show_entries", "format=duration",
|
|
|
|
+ "-of", "default=noprint_wrappers=1:nokey=1", video_file],
|
|
|
|
+ capture_output=True, text=True)
|
|
|
|
+ print(float(result.stdout))
|