diff --git a/add_text_to_image.py b/add_text_to_image.py index c2064be..a9ba0cd 100644 --- a/add_text_to_image.py +++ b/add_text_to_image.py @@ -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) @@ -27,7 +33,7 @@ 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) @@ -35,7 +41,7 @@ def add_text_to_image(text, image_path, text_color, background, padding=10, targ 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 @@ -43,11 +49,11 @@ def add_text_to_image(text, image_path, text_color, background, padding=10, targ 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( @@ -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) diff --git a/images/1686711431-pollinations-ai.png b/images/1686711431-pollinations-ai.png deleted file mode 100644 index 4bb72cc..0000000 Binary files a/images/1686711431-pollinations-ai.png and /dev/null differ diff --git a/images/1686711437-pollinations-ai.png b/images/1686711437-pollinations-ai.png deleted file mode 100644 index 1120e97..0000000 Binary files a/images/1686711437-pollinations-ai.png and /dev/null differ diff --git a/images/1686711446-pollinations-ai.png b/images/1686711446-pollinations-ai.png deleted file mode 100644 index eaba106..0000000 Binary files a/images/1686711446-pollinations-ai.png and /dev/null differ diff --git a/requirements_windows.txt b/requirements_windows.txt new file mode 100644 index 0000000..9140db4 --- /dev/null +++ b/requirements_windows.txt @@ -0,0 +1,11 @@ +requests +opencv-python +pydub +pyttsx3 +jieba +langdetect +flask +Pillow +python-dotenv +pycryptodome +edge-tts \ No newline at end of file diff --git a/text_to_video.py b/text_to_video.py index 7d551a5..d92e2e7 100644 --- a/text_to_video.py +++ b/text_to_video.py @@ -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 diff --git a/translate.py b/translate.py index c91d3af..2ee49eb 100644 --- a/translate.py +++ b/translate.py @@ -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", @@ -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("我是中国人")) diff --git a/voices/1686711431-pollinations-ai.mp3 b/voices/1686711431-pollinations-ai.mp3 deleted file mode 100644 index da7c8e1..0000000 Binary files a/voices/1686711431-pollinations-ai.mp3 and /dev/null differ diff --git a/voices/1686711431-pollinations-ai.mp3.vtt b/voices/1686711431-pollinations-ai.mp3.vtt deleted file mode 100644 index 4b14095..0000000 --- a/voices/1686711431-pollinations-ai.mp3.vtt +++ /dev/null @@ -1,5 +0,0 @@ -WEBVTT - -00:00:00.100 --> 00:00:02.062 -一个 天气 晴朗 的 早上 - diff --git a/voices/1686711437-pollinations-ai.mp3 b/voices/1686711437-pollinations-ai.mp3 deleted file mode 100644 index 960fd3c..0000000 Binary files a/voices/1686711437-pollinations-ai.mp3 and /dev/null differ diff --git a/voices/1686711437-pollinations-ai.mp3.vtt b/voices/1686711437-pollinations-ai.mp3.vtt deleted file mode 100644 index 5130f7a..0000000 --- a/voices/1686711437-pollinations-ai.mp3.vtt +++ /dev/null @@ -1,5 +0,0 @@ -WEBVTT - -00:00:00.100 --> 00:00:02.413 -张三 骑 着 自行车 在 马路 上 - diff --git a/voices/1686711446-pollinations-ai.mp3 b/voices/1686711446-pollinations-ai.mp3 deleted file mode 100644 index f4dc4c3..0000000 Binary files a/voices/1686711446-pollinations-ai.mp3 and /dev/null differ diff --git a/voices/1686711446-pollinations-ai.mp3.vtt b/voices/1686711446-pollinations-ai.mp3.vtt deleted file mode 100644 index 20dba62..0000000 --- a/voices/1686711446-pollinations-ai.mp3.vtt +++ /dev/null @@ -1,5 +0,0 @@ -WEBVTT - -00:00:00.100 --> 00:00:02.200 -看到 了 天上 有 飞机 经过 - diff --git a/youdaoapi.py b/youdaoapi.py new file mode 100644 index 0000000..a787835 --- /dev/null +++ b/youdaoapi.py @@ -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; OUTFOX_SEARCH_USER_ID=-380628258@222.182.116.19', + '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) \ No newline at end of file