-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
275 lines (217 loc) · 9.9 KB
/
test.py
File metadata and controls
275 lines (217 loc) · 9.9 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
#!/usr/bin/env python3
"""
Example usage of ElevenLabs Force Alignment SRT Generator
This file demonstrates how to use the force alignment tool in production.
Users can copy this file and modify the parameters for their specific use case.
"""
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
# Add parent directory to path to import main module
sys.path.insert(0, str(Path(__file__).parent))
from script_force_alignment import SRTGenerator, elevenlabs_force_alignment_to_srt
# Load environment variables
load_dotenv()
# ============================================================================
# CONFIGURATION PARAMETERS - MODIFY THESE FOR YOUR USE CASE
# ============================================================================
# Audio file configuration
AUDIO_FILE_PATH = "/Users/lgg/coding/sumatman/Temps/web_1755001064534_tlv280s7z/audio/scene_033_chinese.mp3" # Path to your audio file
# Supported formats: MP3, WAV, M4A, OGG, FLAC, AAC, OPUS, MP4
# Text content that corresponds to the audio
# This should be the exact transcript of what's spoken in the audio
TEXT_CONTENT = """
亚里士多德和他的学生们,在这里进行着一项前无古人、也可能后无来者的庞大工程:那就是系统性地收集、整理、研究当时人类已知的一切知识。想象一下吕克昂的日常:清晨,亚里士多德可能在和学生讨论悲剧的本质,深入剖析索福克勒斯的《俄狄浦斯王》,并从中总结出“净化”和“突转”等经典的戏剧理论。他的研究范围无所不包,充满了对万事万物的好奇心。
"""
# Output configuration
OUTPUT_FILE_PATH = "temps/subtitles_33.srt" # Where to save the SRT file
# Subtitle formatting configuration
MAX_CHARS_PER_LINE = 36 # Maximum characters per subtitle line
# Note: For bilingual subtitles, this will be automatically adjusted
# Language configuration
LANGUAGE = 'chinese' # Options: 'chinese', 'english', 'spanish', 'french', etc.
# This helps the tool optimize for specific language characteristics
# Semantic segmentation configuration
USE_SEMANTIC_SEGMENTATION = True # True: Use AI for smart segmentation
# - True: Uses Gemini AI to create natural, meaningful subtitle segments
# - False: Uses simple character-based segmentation
# Advanced configuration (optional)
API_KEY_OVERRIDE = None # Set to override the .env file API key
# Example: API_KEY_OVERRIDE = "xi-abc123..."
# Gemini model configuration (optional)
GEMINI_MODEL = 'gemini-2.0-flash' # Use default model (gemini-2.0-flash)
# Example: GEMINI_MODEL = "gemini-2.0-flash-exp" # For experimental features
# Example: GEMINI_MODEL = "gemini-1.5-pro" # For higher quality
# Example: GEMINI_MODEL = "gemini-2.0-flash-thinking" # For complex reasoning
# ============================================================================
# MAIN FUNCTION - Usually you don't need to modify this
# ============================================================================
def generate_subtitles():
"""
Generate SRT subtitles for the configured audio file using SRTGenerator class.
Returns:
bool: True if successful, False otherwise
"""
print("🎬 ElevenLabs Force Alignment SRT Generator (with SRTGenerator Class)")
print("=" * 60)
# Get API keys from environment or override
elevenlabs_key = API_KEY_OVERRIDE or os.getenv("ELEVENLABS_API_KEY")
gemini_key = os.getenv("GEMINI_API_KEY") if USE_SEMANTIC_SEGMENTATION else None
# Validate API keys
if not elevenlabs_key:
print("❌ ERROR: ELEVENLABS_API_KEY not found")
print("Please set up your .env file or provide API_KEY_OVERRIDE")
print("See .env.example for reference")
return False
if USE_SEMANTIC_SEGMENTATION and not gemini_key:
print("❌ ERROR: GEMINI_API_KEY not found (required for semantic segmentation)")
print("Please set up your .env file or set USE_SEMANTIC_SEGMENTATION = False")
return False
# Validate input file
if not os.path.exists(AUDIO_FILE_PATH):
print(f"❌ ERROR: Audio file not found: {AUDIO_FILE_PATH}")
print("Please check the file path and try again")
return False
# Create output directory if needed
output_dir = os.path.dirname(OUTPUT_FILE_PATH)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
# Display configuration
print("📋 Configuration:")
print(f" Audio: {AUDIO_FILE_PATH}")
print(f" Output: {OUTPUT_FILE_PATH}")
print(f" Language: {LANGUAGE}")
print(f" Max chars/line: {MAX_CHARS_PER_LINE}")
print(f" Semantic AI: {'Enabled' if USE_SEMANTIC_SEGMENTATION else 'Disabled'}")
if GEMINI_MODEL:
print(f" Gemini Model: {GEMINI_MODEL}")
print()
# Initialize SRTGenerator with API keys
print("🔧 Initializing SRTGenerator...")
try:
generator = SRTGenerator(
elevenlabs_api_key=elevenlabs_key,
gemini_api_key=gemini_key
)
print("✅ SRTGenerator initialized successfully")
except Exception as e:
print(f"❌ Failed to initialize SRTGenerator: {e}")
return False
# Generate subtitles using the generator
print("🚀 Starting force alignment with SRTGenerator...")
success, result = generator.generate(
audio_file=AUDIO_FILE_PATH,
text=TEXT_CONTENT.strip(),
output_file=OUTPUT_FILE_PATH,
max_chars_per_line=MAX_CHARS_PER_LINE,
language=LANGUAGE,
use_semantic_segmentation=USE_SEMANTIC_SEGMENTATION,
model=GEMINI_MODEL
)
# Report results
print()
if success:
print("✅ SUCCESS! Subtitles generated successfully")
print(f"📄 Output file: {result}")
# Display file info
if os.path.exists(result):
file_size = os.path.getsize(result)
with open(result, 'r', encoding='utf-8') as f:
lines = f.readlines()
subtitle_count = len([l for l in lines if l.strip() and '-->' in l])
print(f"📊 Statistics:")
print(f" File size: {file_size:,} bytes")
print(f" Subtitles: {subtitle_count} segments")
print(f" Total lines: {len(lines)}")
return True
else:
print(f"❌ FAILED: {result}")
print("\n🔍 Troubleshooting tips:")
print("1. Check your API keys are valid")
print("2. Ensure the audio file is accessible")
print("3. Verify the text matches the audio content")
print("4. Check your internet connection")
return False
# ============================================================================
# ADDITIONAL HELPER FUNCTIONS (Optional)
# ============================================================================
def batch_process_files(file_list):
"""
Process multiple audio files in batch using SRTGenerator.
Args:
file_list: List of tuples (audio_path, text_content, output_path)
Example:
files = [
("audio1.mp3", "Text 1", "output1.srt"),
("audio2.mp3", "Text 2", "output2.srt"),
]
batch_process_files(files)
"""
print(f"🔄 Batch processing {len(file_list)} files with SRTGenerator...")
# Initialize generator once for batch processing
elevenlabs_key = API_KEY_OVERRIDE or os.getenv("ELEVENLABS_API_KEY")
gemini_key = os.getenv("GEMINI_API_KEY") if USE_SEMANTIC_SEGMENTATION else None
try:
generator = SRTGenerator(
elevenlabs_api_key=elevenlabs_key,
gemini_api_key=gemini_key
)
except Exception as e:
print(f"❌ Failed to initialize SRTGenerator: {e}")
return []
results = []
for i, (audio_path, text, output_path) in enumerate(file_list, 1):
print(f"\n[{i}/{len(file_list)}] Processing: {audio_path}")
success, result = generator.generate(
audio_file=audio_path,
text=text,
output_file=output_path,
max_chars_per_line=MAX_CHARS_PER_LINE,
language=LANGUAGE,
use_semantic_segmentation=USE_SEMANTIC_SEGMENTATION,
model=GEMINI_MODEL
)
results.append({
'audio': audio_path,
'success': success,
'result': result
})
# Summary
successful = sum(1 for r in results if r['success'])
print(f"\n📊 Batch Results: {successful}/{len(file_list)} successful")
return results
def validate_alignment(srt_file, original_text):
"""
Validate that the SRT file contains all the original text.
Args:
srt_file: Path to the SRT file
original_text: Original text content
Returns:
bool: True if validation passes
"""
if not os.path.exists(srt_file):
return False
with open(srt_file, 'r', encoding='utf-8') as f:
srt_content = f.read()
# Extract only subtitle text (skip timecodes and numbers)
lines = srt_content.split('\n')
subtitle_text = []
for line in lines:
line = line.strip()
if line and not line.isdigit() and '-->' not in line:
subtitle_text.append(line)
combined_text = ' '.join(subtitle_text)
# Basic validation: check if most words are present
original_words = set(original_text.lower().split())
subtitle_words = set(combined_text.lower().split())
coverage = len(original_words & subtitle_words) / len(original_words) if original_words else 0
return coverage > 0.8 # 80% word coverage threshold
# ============================================================================
# ENTRY POINT
# ============================================================================
if __name__ == "__main__":
# Run the subtitle generation
success = generate_subtitles()
# Exit with appropriate code
sys.exit(0 if success else 1)