Skip to content

Commit

Permalink
add microsoft translator
Browse files Browse the repository at this point in the history
  • Loading branch information
jianchang512 committed Feb 8, 2024
1 parent b355762 commit 88f9b28
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 94 deletions.
71 changes: 30 additions & 41 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,38 @@
# msg=f"separate vocal and background music:{str(e)}"
# #set_process(msg)
# print(msg)
import sys

from PySide6.QtGui import QGuiApplication
from PySide6.QtWidgets import QWidget, QApplication

def ceshi():
from you_get.extractors import youtube
## 获取识别文字
# import os
# from faster_whisper import WhisperModel
# model = WhisperModel("base", device="cpu",
# download_root="./models",
# local_files_only=True)
# data=[]
#
# for i in range(0,24):
# name=f'output0{str(i).zfill(2)}.wav'
# if not os.path.exists(f'c:/users/c1/videos/_video_out/{name}'):
# continue
# segments, info = model.transcribe(f'c:/users/c1/videos/_video_out/{name}',
# beam_size=1,
# best_of=1,
# condition_on_previous_text=False,language="zh")
# res=[]
# for segment in segments:
# res.append(segment.text.strip())
# data.append(f"wavs/{name}|{'.'.join(res)}|coqui")
#
# with open("./metadata_train.csv","w",encoding='utf-8') as f:
# f.write("\n".join(data))

try:
youtube.download('https://www.youtube.com/watch?v=n4mF5xo5khM',
output_dir='.',
merge=True,
extractor_proxy='http://127.0.0.1:10809'
)
except Exception as e:
print(e)

class StartWindow(QWidget):
def __init__(self):
super(StartWindow, self).__init__()
# 设置窗口无边框和标题
# 设置窗口的背景图片
# 窗口大小
self.resize(560, 350)
self.center()
self.show()
# 使用QTimer延时显示窗口B
ceshi()


def center(self):
screen=QGuiApplication.primaryScreen()
qtRect = self.frameGeometry()
qtRect.moveCenter(screen.availableGeometry().center())
reso=screen.geometry()
self.width, self.height=reso.width(), reso.height()
self.move(qtRect.topLeft())
import requests
lang=["zh-hans","zh-hant","en","fr","de","ja","ko","ru","es","th","it","pt","vi","ar","tr","hi","hu"]

if __name__ == "__main__":
app = QApplication(sys.argv)
try:
startwin = StartWindow()
except Exception as e:
print(f"error:{str(e)}")
sys.exit(app.exec())
auth=requests.get('https://edge.microsoft.com/translate/auth')

for it in lang:
url=f'https://api-edge.cognitive.microsofttranslator.com/translate?from=&to={it}&api-version=3.0&includeSentenceLength=true'
res=requests.post(url,json=[{"Text":"hello,my friend\nI am from China"}],headers={"Authorization":f"Bearer {auth.text}"})
print(res.json())
4 changes: 2 additions & 2 deletions version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "0.9997",
"version_num": 10007
"version": "0.9998",
"version_num": 10008
}
4 changes: 2 additions & 2 deletions videotrans/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-

VERSION="0.9997 v.wonyes.org"
VERSION_NUM=10007
VERSION="0.9998 v.wonyes.org"
VERSION_NUM=10008
1 change: 1 addition & 0 deletions videotrans/language/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"translate_language": {
"test google": "Test connection Google..",
"Select Out Dir": "Select Out Dir",
"downing...": "Downloading...",
"start download": "Start Download",
Expand Down
1 change: 1 addition & 0 deletions videotrans/language/es.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"translate_language" : {
"test google": "Test connection Google..",
"Select Out Dir": "Select Out Dir",
"downing...": "Downloading...",
"start download": "Start Download",
Expand Down
9 changes: 5 additions & 4 deletions videotrans/language/zh.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"translate_language": {
"test google": "正在测试能否连接到Google..",
"Select Out Dir": "下载到..",
"downing...": "下载中...",
"start download": "立即下载",
Expand Down Expand Up @@ -179,7 +180,7 @@
"Open": "打开",
"Translate channel": "翻译渠道",
"Proxy": "代理地址",
"proxy address": "Google或chatGPT官方接口,国内必填代理或设置系统代理,格式http://127.0.0.1:端口号",
"proxy address": "Google或chatGPT官方接口需填代理,代理格式 http://127.0.0.1:端口号 ",
"shuoming01": "点击试听当前配音角色的发音\n生成配音可能需要数秒,请耐心等待",
"Trial dubbing": "试听配音",
"Source lang": "原始语言",
Expand Down Expand Up @@ -224,17 +225,17 @@
"Donating developers": "捐助开发者",
"Standard Function Mode": "标准功能模式",
"Display all options for video translation and dubbing": "显示全部选项,进行视频翻译和配音",
"Export Srt From Videos": "视频提取字幕",
"Export Srt From Videos": "视频识别字幕",
"Extracting SRT subtitles in the original language from local videos": "从本地视频中提取出原始语言的srt字幕",
"Merging Subtitle Video": "字幕视频合并",
"Embed locally existing SRT subtitles into the video": "将本地已有的srt字幕嵌入视频中",
"Subtitle Create Dubbing": "字幕创建配音",
"Local existing SRT subtitle generation dubbing WAV files": "本地已有的srt字幕生成配音wav文件",
"Speech Recognition Text": "语音识别文字",
"Speech Recognition Text": "音视频转文字",
"Recognize the sound in audio or video and output SRT text": "将音频或视频中的声音识别后输出srt文字",
"From Text Into Speech": "文字合成语音",
"Generate audio WAV from text or SRT subtitle files": "将文字或srt字幕文件生成音频wav",
"Extract Srt And Translate": "提取字幕翻译",
"Extract Srt And Translate": "识别字幕翻译",
"Extract SRT subtitles from local videos in the original language and translate them into SRT subtitle files in the target language": "从本地视频中提取出原始语言的srt字幕,并翻译为目标语言的srt字幕文件",
"Separate Video to audio": "视频音频分离",
"Separate audio and silent videos from videos": "从视频中分离出音频和无声视频",
Expand Down
16 changes: 10 additions & 6 deletions videotrans/task/trans_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def __init__(self, obj):
if os.path.exists(self.targetdir_source_regcon) and os.path.getsize(self.targetdir_source_regcon)==0:
os.unlink(self.targetdir_source_regcon)
else:
self.targetdir_source_vocal=self.targetdir_source_wav
self.source_separate = self.source_back = self.source_vocal = None

# 如果存在字幕,则视为目标字幕,直接生成,不再识别和翻译
Expand Down Expand Up @@ -315,11 +316,14 @@ def trans(self):
if not os.path.exists(self.targetdir_source_sub) or os.path.exists(self.targetdir_target_sub):
return True
# 测试翻译
switch_trans=""
if config.params['translate_type'].lower()==GOOGLE_NAME.lower():
set_process("Test Google connecting...")
set_process(config.transobj['test google'])
if not self.testgoogle():
raise Exception(f'无法连接Google,请正确设置代理,例如v2ray默认是http://127.0.0.1:10809,clash默认http://127.0.0.1:7890或开启全局代理')
set_process(transobj['starttrans'])
switch_trans='无法连接Google,已自动切换为Microsoft翻译'
config.params['translate_type']='Microsoft'

set_process(transobj['starttrans']+switch_trans)
# 开始翻译,从目标文件夹读取原始字幕
rawsrt = get_subtitle_from_srt(self.targetdir_source_sub, is_file=True)
if not rawsrt or len(rawsrt)<1:
Expand Down Expand Up @@ -496,11 +500,11 @@ def before_tts(self):
filename = self.cache_folder + "/" + md5_hash.hexdigest() + ".mp3"
# 如果是clone-voice类型, 需要截取对应片段
if config.params['tts_type']=='clone-voice':
if not os.path.exists(self.targetdir_source_vocal):
if config.params['is_separate'] and not os.path.exists(self.targetdir_source_vocal):
raise Exception(f'not exits {self.targetdir_source_vocal}')
# clone 方式文件为wav格式
# clone 方式文件为wav格式
filename+=".wav"
cut_from_audio(audio_file=self.targetdir_source_vocal,ss=it['startraw'],to=it['endraw'],out_file=filename)
cut_from_audio(audio_file=self.targetdir_source_vocal if config.params['is_separate'] else self.targetdir_source_wav,ss=it['startraw'],to=it['endraw'],out_file=filename)

queue_tts.append({
"text": it['text'],
Expand Down
36 changes: 30 additions & 6 deletions videotrans/translator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from videotrans.configure import config

GOOGLE_NAME = "Google"
MICROSOFT_NAME = "Microsoft"
BAIDU_NAME = "Baidu"
DEEPL_NAME = "DeepL"
DEEPLX_NAME = "DeepLx"
Expand All @@ -14,6 +15,7 @@
SRT_NAME = "srt"
# 翻译通道
TRANSNAMES = [
MICROSOFT_NAME,
GOOGLE_NAME,
BAIDU_NAME,
DEEPL_NAME,
Expand All @@ -32,22 +34,25 @@
"zh", # 百度通道
"ZH", # deepl deeplx通道
"zh", # 腾讯通道
"zh" # OTT通道
"zh", # OTT通道
"zh-Hans"
],
"zh-tw": [
"zh-tw",
"chi",
"cht",
"ZH",
"zh-TW",
"zt"
"zt",
"zh-Hant"
],
"en": [
"en",
"eng",
"en",
"EN",
"en",
"en",
"en"
],
"fr": [
Expand All @@ -56,6 +61,7 @@
"fra",
"FR",
"fr",
"fr",
"fr"
],
"de": [
Expand All @@ -64,6 +70,7 @@
"de",
"DE",
"de",
"de",
"de"
],
"ja": [
Expand All @@ -72,6 +79,7 @@
"jp",
"JA",
"ja",
"ja",
"ja"
],
"ko": [
Expand All @@ -80,6 +88,7 @@
"kor",
"KO",
"ko",
"ko",
"ko"
],
"ru": [
Expand All @@ -88,6 +97,7 @@
"ru",
"RU",
"ru",
"ru",
"ru"
],
"es": [
Expand All @@ -96,6 +106,7 @@
"spa",
"ES",
"es",
"es",
"es"
],
"th": [
Expand All @@ -104,6 +115,7 @@
"th",
"No",
"th",
"th",
"th"
],
"it": [
Expand All @@ -112,6 +124,7 @@
"it",
"IT",
"it",
"it",
"it"
],
"pt": [
Expand All @@ -120,6 +133,7 @@
"pt",
"PT",
"pt",
"pt",
"pt"
],
"vi": [
Expand All @@ -128,14 +142,16 @@
"vie",
"No",
"vi",
"No"
"No",
"vi"
],
"ar": [
"ar",
"are",
"ara",
"No",
"ar",
"ar",
"ar"
],
"tr": [
Expand All @@ -144,6 +160,7 @@
"tr",
"tr",
"tr",
"tr",
"tr"
],
"hi": [
Expand All @@ -152,6 +169,7 @@
"hi",
"No",
"hi",
"hi",
"hi"
],
"hu": [
Expand All @@ -160,7 +178,8 @@
"hu",
"HU",
"No",
"No"
"No",
"hu"
]
}

Expand All @@ -183,7 +202,6 @@ def get_source_target_code(*, show_source=None, show_target=None, translate_type
if not translate_type:
return None, None
lower_translate_type = translate_type.lower()
print(f'{lower_translate_type == GOOGLE_NAME.lower()}')
if show_source:
source_list = LANG_CODE[show_source] if show_source in LANG_CODE else LANG_CODE[
config.rev_langlist[show_source]]
Expand All @@ -202,6 +220,8 @@ def get_source_target_code(*, show_source=None, show_target=None, translate_type
return (show_source, show_target)
elif lower_translate_type == OTT_NAME.lower():
return (source_list[5] if source_list else "-", target_list[5] if target_list else "-")
elif lower_translate_type==MICROSOFT_NAME.lower():
return (source_list[6] if source_list else "-", target_list[6] if target_list else "-")
else:
raise Exception(f"[error]get_source_target_code:{translate_type=},{show_source=},{show_target=}")

Expand Down Expand Up @@ -237,14 +257,16 @@ def is_allow_translate(*, translate_type=None, show_target=None, only_key=False)

if only_key:
return True

#再判断是否为No,即不支持
index = 0
if lower_translate_type == BAIDU_NAME.lower():
index = 2
elif lower_translate_type in [DEEPLX_NAME.lower(), DEEPL_NAME.lower()]:
index = 3
elif lower_translate_type == TENCENT_NAME.lower():
index = 4
elif lower_translate_type==MICROSOFT_NAME.lower():
index=6

if show_target:
target_list = LANG_CODE[show_target] if show_target in LANG_CODE else LANG_CODE[
Expand Down Expand Up @@ -290,6 +312,8 @@ def run(*, translate_type=None, text_list=None, target_language_name=None, set_p
from videotrans.translator.gemini import trans
elif lower_translate_type == AZUREGPT_NAME.lower():
from videotrans.translator.azure import trans
elif lower_translate_type==MICROSOFT_NAME.lower():
from videotrans.translator.microsoft import trans
else:
print(lower_translate_type == OTT_NAME.lower())
raise Exception(f"[error]run {translate_type=},{target_language_name=}")
Expand Down
Loading

0 comments on commit 88f9b28

Please sign in to comment.