Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:完善windows版本 #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions add_text_to_image.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from PIL import Image, ImageDraw, ImageFont
import jieba

def textsize(text, font):
im = Image.new(mode="P", size=(0, 0))
draw = ImageDraw.Draw(im)
_, _, width, height = draw.textbbox((0, 0), text=text, font=font)
return width, height

def add_text_to_image(text, image_path, text_color, background, padding=10, target_size=(640, 480), font_path="fonts/Hiragino Sans GB.ttc", font_size=16):
# Open the image
image = Image.open(image_path)
Expand All @@ -27,27 +33,27 @@ def add_text_to_image(text, image_path, text_color, background, padding=10, targ
words = [char for char in jieba.cut(text)]
current_line = words[0]
for word in words[1:]:
if draw.textsize(current_line + word, font=font)[0] <= target_size[0] - 2 * padding:
if textsize(current_line + word, font=font)[0] <= target_size[0] - 2 * padding:
current_line += word
else:
lines.append(current_line)
current_line = word
lines.append(current_line)

# Calculate the height of the background rectangle based on the number of lines
text_height = draw.textsize(lines[0], font=font)[1]
text_height = textsize(lines[0], font=font)[1]

if len(lines) == 1:
box_height = text_height + padding * 2
else:
box_height = (text_height + padding) * len(lines) + padding

# Calculate the position of the background rectangle
box_position = ((target_size[0] - draw.textsize(lines[0], font=font)
box_position = ((target_size[0] - textsize(lines[0], font=font)
[0]) // 2 - padding, target_size[1] - box_height - padding)

# Calculate the width of the background rectangle
box_width = draw.textsize(lines[0], font=font)[0] + 2 * padding
box_width = textsize(lines[0], font=font)[0] + 2 * padding

# Draw a rectangle with the specified background color and alpha
draw.rectangle(
Expand All @@ -58,7 +64,7 @@ def add_text_to_image(text, image_path, text_color, background, padding=10, targ

# Draw the text on the overlay image
for line in lines:
text_width, text_height = draw.textsize(line, font=font)
text_width, text_height = textsize(line, font=font)
text_position = ((target_size[0] - text_width) // 2, start_y)
print(line, text_position)
draw.text(text_position, line, font=font, fill=text_color)
Expand Down
Binary file removed images/1686711431-pollinations-ai.png
Binary file not shown.
Binary file removed images/1686711437-pollinations-ai.png
Binary file not shown.
Binary file removed images/1686711446-pollinations-ai.png
Binary file not shown.
11 changes: 11 additions & 0 deletions requirements_windows.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
requests
opencv-python
pydub
pyttsx3
jieba
langdetect
flask
Pillow
python-dotenv
pycryptodome
edge-tts
12 changes: 11 additions & 1 deletion text_to_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,21 @@ def merge_audio_to_video(audio_directory, video_file, output_file):
result = subprocess.run(command,cwd=current_directory, timeout=300)
print(result)

import chardet

def check_charset(file_path):
with open(file_path, "rb") as f:
data = f.read(4)
charset = chardet.detect(data)['encoding']
return charset

def get_duration_from_vtt(vtt_file):
print(vtt_file)
if not os.path.exists(vtt_file):
return 0.1
with open(vtt_file, 'r') as file:
# charset = check_charset(vtt_file)
# print(charset)
with open(vtt_file, 'r', encoding="utf-8") as file:
lines = file.readlines()

total_duration = 0.1
Expand Down
13 changes: 11 additions & 2 deletions translate.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import requests
from langdetect import detect

def translate_to_english(text):
def translate_to_english_old(text):
if not is_chinese(text):
return text
url = "http://fanyi.youdao.com/translate?doctype=json"
url = "https://dict.youdao.com/keyword/key"
params = {
"i": text,
"from": "auto",
Expand All @@ -18,6 +18,15 @@ def translate_to_english(text):
else:
return text

from youdaoapi import Trans

def translate_to_english(text):
if not is_chinese(text):
return text
str=text
result=Trans.fnayi(str)
print(result)
return result

# if __name__ == "__main__":
# print(translate_to_english("我是中国人"))
Expand Down
Binary file removed voices/1686711431-pollinations-ai.mp3
Binary file not shown.
5 changes: 0 additions & 5 deletions voices/1686711431-pollinations-ai.mp3.vtt

This file was deleted.

Binary file removed voices/1686711437-pollinations-ai.mp3
Binary file not shown.
5 changes: 0 additions & 5 deletions voices/1686711437-pollinations-ai.mp3.vtt

This file was deleted.

Binary file removed voices/1686711446-pollinations-ai.mp3
Binary file not shown.
5 changes: 0 additions & 5 deletions voices/1686711446-pollinations-ai.mp3.vtt

This file was deleted.

97 changes: 97 additions & 0 deletions youdaoapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import json
from Crypto.Cipher import AES
import hashlib
import base64
from Crypto.Util.Padding import unpad
import time
import requests
import json
class Trans:
def decrypt(decrypt_str):
key = "ydsecret://query/key/B*RGygVywfNBwpmBaZg*WT7SIOUP2T0C9WHMZN39j^DAdaZhAnxvGcCY6VYFwnHl"
iv = "ydsecret://query/iv/C@lZe2YzHtZ2CYgaXKSVfsb7Y4QWHjITPPZ0nQp87fBeJ!Iv6v^6fvi2WN@bYpJ4"

key_md5 = hashlib.md5((key).encode('utf-8')).digest()
iv_md5 = hashlib.md5((iv).encode('utf-8')).digest()
# print('key_md5:', key_md5)
# print('iv_md5:', iv_md5)
# print()
aes = AES.new(key=key_md5, mode=AES.MODE_CBC, iv=iv_md5)

code = aes.decrypt(base64.urlsafe_b64decode(decrypt_str))
return unpad(code, AES.block_size).decode('utf8')


def get_data(translation_words):
url = 'https://dict.youdao.com/webtranslate'
headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'OUTFOX_SEARCH_USER_ID_NCOO=379056539.64209586; [email protected]',
'Host': 'dict.youdao.com',
'Origin': 'https://fanyi.youdao.com',
'Referer': 'https://fanyi.youdao.com/',
'sec-ch-ua': '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
}

# 'ts': '1680404913064', ==> 13位 当前时间戳
# fsdsogkndfokasodnaso 固定值

ts = str(int(time.time() * 1000))
str_sign = f"client=fanyideskweb&mysticTime={ts}&product=webfanyi&key=fsdsogkndfokasodnaso"
sign = hashlib.md5((str_sign).encode('utf-8')).hexdigest()

# print('------------------------sign------------------------')
# print('sign为:', sign, end='\n\n')
form_data = {
'i': translation_words,
'from': 'auto',
'to': '',
'sign': sign,
'dictResult': 'true',
'keyid': 'webfanyi',
'client': 'fanyideskweb',
'product': 'webfanyi',
'appVersion': '1.0.0',
'vendor': 'web',
'pointParam': 'client,mysticTime,product',
'mysticTime': ts,
'keyfrom': 'fanyi.web',
}

res = requests.post(url=url, headers=headers, data=form_data).text

# print('------------------------返回的数据密文------------------------')
# print(res, end='\n\n')
return res

def fnayi(pre_words):
translation_words = pre_words
# translation_words = input("请输入要翻译的:")
decrypt_str = Trans.get_data(translation_words)
end_code = Trans.decrypt(decrypt_str)
# print('------------------------解密后的数据密文------------------------')
# print(end_code, end='\n\n')
json_data = json.loads(end_code)

# print('-------------------------有道翻译-------------------------')
# print('翻译前:', translation_words)
# print('翻译后:', json_data['translateResult'][0][0]['tgt'])
return json_data['translateResult'][0][0]['tgt']


if __name__ == "__main__":
# print(translate_to_english("我是中国人"))

test_str="我是中国人"
result=Trans.fnayi(test_str)
print(result)