-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscript.py
More file actions
296 lines (234 loc) · 8.66 KB
/
Copy pathtranscript.py
File metadata and controls
296 lines (234 loc) · 8.66 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import os
import sys
import argparse
import json
from pathlib import Path
import assemblyai as aai
PUNCTUATION_BREAKS = (".", "?", "!")
TRAILING_PUNCTUATION = ".,!?;:"
def format_timestamp(ms):
ms = max(0, int(round(ms)))
hours = ms // 3_600_000
ms -= hours * 3_600_000
minutes = ms // 60_000
ms -= minutes * 60_000
seconds = ms // 1_000
ms -= seconds * 1_000
return f"{hours:02d}:{minutes:02d}:{seconds:02d},{ms:03d}"
def clean_word(text):
text = text.strip().replace("—", "")
text = text.strip("'\"“”‘’")
return text.rstrip(TRAILING_PUNCTUATION)
def display_text(words):
return " ".join(clean_word(word["text"]) for word in words).strip()
def output_text(words):
text = display_text(words)
if not text:
return text
return text[0].upper() + text[1:]
def has_terminal_punctuation(text):
return text.strip().rstrip("'\"“”‘’").endswith(PUNCTUATION_BREAKS)
def normalized_word(word, max_word_duration_ms):
start = int(word.get("start") or 0)
end = int(word.get("end") or start)
text = word.get("text") or ""
if end < start:
end = start
duration = end - start
if duration > max_word_duration_ms:
estimated = max(240, min(max_word_duration_ms, 180 + len(clean_word(text)) * 90))
end = start + estimated
if end == start:
end = start + 80
new_word = dict(word)
new_word["start"] = start
new_word["end"] = end
return new_word
def normalize_words(words, max_word_duration_ms):
normalized = []
for word in words:
text = clean_word(word.get("text") or "")
if not text:
continue
normalized.append(normalized_word(word, max_word_duration_ms))
return normalized
def should_break(current, next_word, max_chars, max_words, max_duration_ms, gap_ms):
previous = current[-1]
current_text = display_text(current)
next_text = clean_word(next_word["text"])
projected = f"{current_text} {next_text}".strip()
projected_duration = next_word["end"] - current[0]["start"]
pause = next_word["start"] - previous["end"]
previous_text = previous.get("text", "").strip()
if pause > gap_ms:
return True
if len(projected) > max_chars:
return True
if len(current) >= max_words:
return True
if projected_duration > max_duration_ms:
return True
if has_terminal_punctuation(previous_text):
return True
if previous_text.endswith(",") and len(current) <= 2:
return True
return False
def build_segments(
words,
max_chars=40,
max_words=7,
max_duration_ms=2800,
gap_ms=500,
max_word_duration_ms=1200,
):
words = normalize_words(words, max_word_duration_ms)
segments = []
current = []
def flush():
nonlocal current
text = output_text(current)
if text:
segments.append((current[0]["start"], current[-1]["end"], text))
current = []
for word in words:
if current and should_break(
current,
word,
max_chars,
max_words,
max_duration_ms,
gap_ms,
):
flush()
current.append(word)
if current:
flush()
previous_end = 0
adjusted_segments = []
for start, end, text in segments:
start = max(start, previous_end)
if end <= start:
end = start + 80
adjusted_segments.append((start, end, text))
previous_end = end
return adjusted_segments
def write_srt(segments, output_path):
with open(output_path, "w", encoding="utf-8") as output_file:
for index, (start, end, text) in enumerate(segments, start=1):
output_file.write(f"{index}\n")
output_file.write(f"{format_timestamp(start)} --> {format_timestamp(end)}\n")
output_file.write(f"{text}\n\n")
def translate_srt(input_srt, output_srt, target_lang="zh-TW"):
from googlecloud import GoogleTranslator
translator = GoogleTranslator(source="en", target=target_lang)
if translator.client is None:
raise RuntimeError("Google Translate client is not initialized")
translated_path = translator.translate_file(input_srt, output_file=str(output_srt))
if translated_path is None or not Path(translated_path).exists():
raise RuntimeError(f"Translation output was not created: {output_srt}")
def to_srt(
input_json,
output_dir=None,
max_chars=40,
max_words=7,
max_duration_ms=2800,
gap_ms=500,
max_word_duration_ms=1200,
translate=True,
target_lang="zh-TW",
):
input_json = Path(input_json)
output_dir = Path(output_dir) if output_dir else input_json.parent
output_dir.mkdir(parents=True, exist_ok=True)
output_srt = output_dir / "result.srt"
translated_srt = output_dir / "result-zh.srt"
with open(input_json, "r", encoding="utf-8") as json_file:
data = json.load(json_file)
segments = build_segments(
data.get("words") or [],
max_chars=max_chars,
max_words=max_words,
max_duration_ms=max_duration_ms,
gap_ms=gap_ms,
max_word_duration_ms=max_word_duration_ms,
)
write_srt(segments, output_srt)
print(f"Wrote {len(segments):3d} subtitles: {output_srt}")
if translate:
translate_srt(output_srt, translated_srt, target_lang=target_lang)
print(f"Wrote translated subtitles: {translated_srt}")
else:
translated_srt = None
return output_srt, translated_srt, len(segments)
def transcribe_video(
input_file: str,
output_file: str,
speaker_labels: bool = False,
google_translate: bool = True,
target_lang: str = "zh-TW",
):
"""
Transcribe a video or audio file with AssemblyAI and write assemblyAI.json.
Args:
input_file: Path to the input video/audio file.
output_file: Path used to determine the output directory.
speaker_labels: Enable speaker diarization in AssemblyAI.
google_translate: Write translated result-zh.srt when True.
target_lang: Translation target language.
"""
api_key = os.environ.get("ASSEMBLYAI_API_KEY")
if not api_key:
print("Error: ASSEMBLYAI_API_KEY environment variable not set.")
sys.exit(1)
aai.settings.api_key = api_key
transcriber = aai.Transcriber()
config = dict(
speech_models=["universal-3-pro", "universal-2"],
format_text=True,
punctuate=False,
language_detection=True,
disfluencies=True,
)
if speaker_labels:
config["speaker_labels"] = True
transcribe_config = aai.TranscriptionConfig(**config)
print("Starting Analysis & Transcription (AssemblyAI)...")
transcript = transcriber.transcribe(input_file, config=transcribe_config)
if transcript.status == aai.TranscriptStatus.error:
print(f"AssemblyAI Error: {transcript.error}")
sys.exit(1)
output_dir = os.path.dirname(os.path.abspath(output_file or input_file))
if output_dir:
os.makedirs(output_dir, exist_ok=True)
json_output = os.path.join(output_dir, "assemblyAI.json")
print(f"Writing AssemblyAI JSON to: {json_output}")
with open(json_output, "w", encoding="utf-8") as json_file:
json.dump(transcript.json_response, json_file, ensure_ascii=False, indent=2)
to_srt(json_output, translate=google_translate, target_lang=target_lang)
print("Done!")
return json_output
def main():
parser = argparse.ArgumentParser(description="Transcribe audio/video and write AssemblyAI JSON")
parser.add_argument("input_file", help="Path to input video/audio file")
parser.add_argument("--speaker_labels", action="store_true", help="Enable speaker labels")
parser.add_argument("--no-translate", action="store_true", help="Only write result.srt; skip result-zh.srt")
parser.add_argument("--target-lang", default="zh-TW", help="Translation target language for result-zh.srt")
args = parser.parse_args()
input_file = os.path.abspath(args.input_file)
if not os.path.exists(input_file):
raise FileNotFoundError(f"Input file not found: {input_file}")
# Use transcript.srt as a legacy path hint; AssemblyAI JSON is written beside it.
original_output = os.path.join(os.path.dirname(input_file), "transcript.srt")
try:
transcribe_video(
input_file=input_file,
output_file=original_output,
speaker_labels=args.speaker_labels,
google_translate=not args.no_translate,
target_lang=args.target_lang,
)
except Exception as e:
print(f"Error during transcription: {e}")
sys.exit(1)
if __name__ == "__main__":
main()