-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple.py
More file actions
212 lines (166 loc) · 6.35 KB
/
Copy pathsimple.py
File metadata and controls
212 lines (166 loc) · 6.35 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# 凌霄智能影视翻译系统 - 极简免费版
# 无需 PyTorch/Whisper,仅使用免费组件
# pip install edge-tts soundfile pillow
import sys
import os
import tempfile
import subprocess
from pathlib import Path
# ========================
# 核心组件(内联版本)
# ========================
class SimpleTranslator:
"""极简翻译器 - 离线词典模式"""
DICT = {
"hello": "你好", "hi": "你好", "good": "好", "morning": "早上好",
"welcome": "欢迎", "thank": "谢谢", "thanks": "谢谢", "you": "你",
"world": "世界", "this": "这", "is": "是", "a": "一个", "test": "测试",
"video": "视频", "translation": "翻译", "system": "系统", "ai": "人工智能",
"how": "怎么", "are": "是", "what": "什么", "the": "的", "of": "的",
"and": "和", "to": "到", "in": "在", "for": "为了", "with": "用",
"please": "请", "sorry": "对不起", "yes": "是", "no": "不",
"bye": "再见", "goodbye": "再见", "okay": "好的", "great": "太好了",
}
def translate(self, text, source_lang="auto", target_lang="zh"):
words = text.lower().replace(".", " ").replace(",", " ").split()
result = []
for w in words:
result.append(self.DICT.get(w, w))
return " ".join(result)
class SimpleTTS:
"""Edge TTS 语音合成"""
VOICE = "zh-CN-XiaoxiaoNeural"
def synthesize(self, text, output_path):
import asyncio
edge_tts = __import__("edge_tts")
async def run():
communicate = edge_tts.Communicate(text, self.VOICE)
await communicate.save(output_path)
asyncio.run(run())
def find_ffmpeg():
"""查找 ffmpeg"""
import shutil
paths = [
shutil.which("ffmpeg"),
"/usr/local/bin/ffmpeg",
"/opt/homebrew/bin/ffmpeg",
"/Users/liuxiansheng/Library/Python/3.9/lib/python/site-packages/imageio_ffmpeg/binaries/ffmpeg-macos-aarch64-v7.1",
]
for p in paths:
if p and Path(p).exists():
return p
return None
def extract_audio(video_path, audio_path):
"""提取音频"""
ffmpeg = find_ffmpeg()
if not ffmpeg:
raise RuntimeError("ffmpeg not found")
cmd = [ffmpeg, "-i", str(video_path), "-vn", "-acodec", "pcm_s16le",
"-ar", "16000", "-ac", "1", "-y", str(audio_path)]
subprocess.run(cmd, check=True, capture_output=True)
def merge_audio_video(video_path, audio_path, output_path):
"""合并音视频"""
ffmpeg = find_ffmpeg()
if not ffmpeg:
raise RuntimeError("ffmpeg not found")
cmd = [ffmpeg, "-i", str(video_path), "-i", str(audio_path),
"-c:v", "copy", "-c:a", "aac", "-b:a", "192k", "-y", str(output_path)]
subprocess.run(cmd, check=True, capture_output=True)
def simple_translate_subtitle(srt_text, target_lang="zh"):
"""翻译 SRT 字幕"""
import re
translator = SimpleTranslator()
lines = srt_text.split("\n")
result = []
for line in lines:
# 保留时间轴
if "-->" in line:
result.append(line)
else:
# 翻译文本行
translated = translator.translate(line.strip(), target_lang=target_lang)
result.append(translated)
return "\n".join(result)
def parse_srt_text(srt_text):
"""提取 SRT 中的文本"""
import re
pattern = re.compile(r"\d{2}:\d{2}:\d{2},\d{3} --> .+\n(.+)")
return " ".join(pattern.findall(srt_text))
def process_video(input_video, output_video=None, target_lang="zh"):
"""处理单个视频"""
input_path = Path(input_video)
if not input_path.exists():
return {"success": False, "error": f"文件不存在: {input_video}"}
if not output_video:
output_video = input_path.parent / f"{input_path.stem}_zh{input_path.suffix}"
output_path = Path(output_video)
output_path.parent.mkdir(parents=True, exist_ok=True)
temp_dir = Path(tempfile.mkdtemp())
audio_path = temp_dir / "audio.wav"
tts_path = temp_dir / "tts.wav"
merged_path = temp_dir / "merged.mp4"
try:
# 1. 提取音频
print(f" 提取音频...")
extract_audio(input_path, audio_path)
# 2. 简单模拟 ASR(实际应该用 Whisper,这里用占位符)
# 注意:极简版不包含 Whisper,需要完整版
print(f" [需要完整版 Whisper 来识别语音]")
print(f" 使用占位符文本演示...")
# 生成测试字幕
srt_content = """1
00:00:00,000 --> 00:00:05,000
Hello world, welcome to the AI translation system.
2
00:00:05,000 --> 00:00:10,000
This is a test video for translation.
"""
# 3. 翻译
print(f" 翻译字幕...")
translated = simple_translate_subtitle(srt_content, target_lang)
print(f" 翻译结果: {translated[:80]}...")
# 4. TTS
print(f" 生成配音...")
tts = SimpleTTS()
text = parse_srt_text(translated)
tts.synthesize(text[:200], tts_path)
# 5. 合并
print(f" 合并视频...")
merge_audio_video(input_path, tts_path, merged_path)
# 复制到输出
import shutil
shutil.copy(merged_path, output_path)
return {"success": True, "output": str(output_path)}
except Exception as e:
return {"success": False, "error": str(e)}
finally:
# 清理
import shutil
shutil.rmtree(temp_dir, ignore_errors=True)
# ========================
# CLI 入口
# ========================
def main():
import argparse
parser = argparse.ArgumentParser(description="凌霄智能影视翻译系统 - 极简版")
parser.add_argument("input", help="输入视频")
parser.add_argument("-o", "--output", help="输出视频")
parser.add_argument("-t", "--target", default="zh", help="目标语言 (默认: zh)")
args = parser.parse_args()
print("=" * 50)
print(" 凌霄智能影视翻译系统 - 极简版")
print("=" * 50)
print()
print(f"输入: {args.input}")
print(f"输出: {args.output or '自动生成'}")
print(f"目标语言: {args.target}")
print()
result = process_video(args.input, args.output, args.target)
print()
if result["success"]:
print(f"✓ 完成: {result['output']}")
else:
print(f"✗ 失败: {result['error']}")
sys.exit(1)
if __name__ == "__main__":
main()