-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdx-voice-v3.py
More file actions
47 lines (39 loc) · 1.58 KB
/
Copy pathmdx-voice-v3.py
File metadata and controls
47 lines (39 loc) · 1.58 KB
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
import os
import re
import asyncio
import edge_tts
SOURCE_DIR = "content/docs"
OUTPUT_DIR = "public/audio/markdown"
VOICE = "en-US-GuyNeural" # Giọng nam tiếng Anh
def clean_text(text):
text = re.sub(r"```[\s\S]*?```", "", text)
text = re.sub(r"^#+\s*", "", text, flags=re.MULTILINE)
text = re.sub(r"\[(.*?)\]\(.*?\)", r"\1", text)
text = re.sub(r"\*\*(.*?)\*\*", r"\1", text)
text = re.sub(r"\*(.*?)\*", r"\1", text)
text = re.sub(r"\s+", " ", text).strip()
return text
async def mdx_to_audio(input_path, output_path):
with open(input_path, "r", encoding="utf-8") as f:
content = f.read()
cleaned = clean_text(content)
if not cleaned:
print(f"⚠️ Bỏ qua {input_path} vì rỗng sau khi làm sạch")
return
communicate = edge_tts.Communicate(cleaned, VOICE)
await communicate.save(output_path)
print(f"✅ Đã tạo: {output_path}")
async def main():
for root, _, files in os.walk(SOURCE_DIR):
for file in files:
if file.endswith(".mdx"):
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, SOURCE_DIR)
rel_dir = os.path.dirname(rel_path)
file_name = os.path.splitext(file)[0].lower().replace(" ", "-")
output_dir = os.path.join(OUTPUT_DIR, rel_dir)
os.makedirs(output_dir, exist_ok=True)
output_file = os.path.join(output_dir, f"{file_name}.mp3")
await mdx_to_audio(file_path, output_file)
if __name__ == "__main__":
asyncio.run(main())