Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,7 +48,9 @@ def process_video(video_file, language="hindi", progress=gr.Progress()):
|
|
| 48 |
return None, None, "Please upload a video"
|
| 49 |
|
| 50 |
temp_dir = tempfile.mkdtemp()
|
|
|
|
| 51 |
print(f"Processing in temp directory: {temp_dir}")
|
|
|
|
| 52 |
|
| 53 |
try:
|
| 54 |
# Extract audio
|
|
@@ -58,6 +60,7 @@ def process_video(video_file, language="hindi", progress=gr.Progress()):
|
|
| 58 |
print(f"Extracting audio from: {video_file}")
|
| 59 |
video = VideoFileClip(video_file)
|
| 60 |
if not video.audio:
|
|
|
|
| 61 |
return None, None, "Video has no audio track"
|
| 62 |
|
| 63 |
video.audio.write_audiofile(
|
|
@@ -90,17 +93,18 @@ def process_video(video_file, language="hindi", progress=gr.Progress()):
|
|
| 90 |
if not result.get('chunks') or len(result['chunks']) == 0:
|
| 91 |
error_msg = f"Transcription returned no segments. Full result: {result}"
|
| 92 |
print(error_msg)
|
|
|
|
| 93 |
return None, None, error_msg
|
| 94 |
|
| 95 |
print(f"Transcription complete: {len(result['chunks'])} segments")
|
| 96 |
|
| 97 |
-
# Create SRT
|
| 98 |
progress(0.6, desc="Creating SRT...")
|
| 99 |
-
srt_path = os.path.join(
|
| 100 |
|
| 101 |
print(f"Creating SRT at: {srt_path}")
|
| 102 |
-
print(f"
|
| 103 |
-
print(f"
|
| 104 |
|
| 105 |
try:
|
| 106 |
with open(srt_path, 'w', encoding='utf-8-sig') as f:
|
|
@@ -113,6 +117,179 @@ def process_video(video_file, language="hindi", progress=gr.Progress()):
|
|
| 113 |
f.write(f"{format_srt_time(start)} --> {format_srt_time(end)}\n")
|
| 114 |
f.write(f"{text}\n\n")
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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50 |
temp_dir = tempfile.mkdtemp()
|
| 51 |
+
output_dir = tempfile.mkdtemp() # Separate directory for outputs
|
| 52 |
print(f"Processing in temp directory: {temp_dir}")
|
| 53 |
+
print(f"Output directory: {output_dir}")
|
| 54 |
|
| 55 |
try:
|
| 56 |
# Extract audio
|
|
|
|
| 60 |
print(f"Extracting audio from: {video_file}")
|
| 61 |
video = VideoFileClip(video_file)
|
| 62 |
if not video.audio:
|
| 63 |
+
shutil.rmtree(output_dir)
|
| 64 |
return None, None, "Video has no audio track"
|
| 65 |
|
| 66 |
video.audio.write_audiofile(
|
|
|
|
| 93 |
if not result.get('chunks') or len(result['chunks']) == 0:
|
| 94 |
error_msg = f"Transcription returned no segments. Full result: {result}"
|
| 95 |
print(error_msg)
|
| 96 |
+
shutil.rmtree(output_dir)
|
| 97 |
return None, None, error_msg
|
| 98 |
|
| 99 |
print(f"Transcription complete: {len(result['chunks'])} segments")
|
| 100 |
|
| 101 |
+
# Create SRT in output directory (persistent)
|
| 102 |
progress(0.6, desc="Creating SRT...")
|
| 103 |
+
srt_path = os.path.join(output_dir, "subtitles.srt")
|
| 104 |
|
| 105 |
print(f"Creating SRT at: {srt_path}")
|
| 106 |
+
print(f"Output directory exists: {os.path.exists(output_dir)}")
|
| 107 |
+
print(f"Output directory contents before SRT: {os.listdir(output_dir)}")
|
| 108 |
|
| 109 |
try:
|
| 110 |
with open(srt_path, 'w', encoding='utf-8-sig') as f:
|
|
|
|
| 117 |
f.write(f"{format_srt_time(start)} --> {format_srt_time(end)}\n")
|
| 118 |
f.write(f"{text}\n\n")
|
| 119 |
|
| 120 |
+
print(f"SRT file write completed")
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print(f"Error writing SRT file: {e}")
|
| 123 |
+
import traceback
|
| 124 |
+
print(traceback.format_exc())
|
| 125 |
+
shutil.rmtree(output_dir)
|
| 126 |
+
raise
|
| 127 |
+
|
| 128 |
+
# Verify SRT was created
|
| 129 |
+
print(f"Checking if SRT exists: {os.path.exists(srt_path)}")
|
| 130 |
+
print(f"Output directory contents after SRT: {os.listdir(output_dir)}")
|
| 131 |
+
|
| 132 |
+
if not os.path.exists(srt_path):
|
| 133 |
+
shutil.rmtree(output_dir)
|
| 134 |
+
return None, None, f"SRT file was not created at: {srt_path}\nDirectory contents: {os.listdir(output_dir)}"
|
| 135 |
+
|
| 136 |
+
srt_size = os.path.getsize(srt_path)
|
| 137 |
+
if srt_size == 0:
|
| 138 |
+
shutil.rmtree(output_dir)
|
| 139 |
+
return None, None, f"SRT file is empty at: {srt_path}"
|
| 140 |
+
|
| 141 |
+
print(f"SRT created successfully: {srt_path} ({srt_size} bytes)")
|
| 142 |
+
|
| 143 |
+
# Read SRT content for debugging
|
| 144 |
+
try:
|
| 145 |
+
with open(srt_path, 'r', encoding='utf-8-sig') as f:
|
| 146 |
+
srt_preview = f.read()[:500]
|
| 147 |
+
print(f"SRT preview:\n{srt_preview}")
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"Error reading SRT file: {e}")
|
| 150 |
+
|
| 151 |
+
# Burn subtitles
|
| 152 |
+
progress(0.85, desc="Burning subtitles...")
|
| 153 |
+
output_path = os.path.join(output_dir, "final.mp4")
|
| 154 |
+
|
| 155 |
+
print("Burning subtitles with FFmpeg...")
|
| 156 |
+
|
| 157 |
+
# Convert SRT to ASS first (more reliable with FFmpeg)
|
| 158 |
+
ass_path = os.path.join(output_dir, "subtitles.ass")
|
| 159 |
+
|
| 160 |
+
print(f"Converting SRT to ASS for better compatibility...")
|
| 161 |
+
try:
|
| 162 |
+
subprocess.run([
|
| 163 |
+
'ffmpeg', '-y',
|
| 164 |
+
'-i', srt_path,
|
| 165 |
+
ass_path
|
| 166 |
+
], check=True, capture_output=True, text=True)
|
| 167 |
+
|
| 168 |
+
if not os.path.exists(ass_path):
|
| 169 |
+
raise Exception(f"Failed to create ASS file at {ass_path}")
|
| 170 |
+
|
| 171 |
+
print(f"ASS file created: {ass_path} ({os.path.getsize(ass_path)} bytes)")
|
| 172 |
+
except subprocess.CalledProcessError as e:
|
| 173 |
+
print(f"SRT to ASS conversion failed: {e.stderr}")
|
| 174 |
+
shutil.rmtree(output_dir)
|
| 175 |
+
raise
|
| 176 |
+
|
| 177 |
+
# Use ASS filter which is more reliable than subtitles filter
|
| 178 |
+
# For ass filter, we need to escape special characters differently
|
| 179 |
+
ass_path_escaped = ass_path.replace('\\', '\\\\').replace(':', '\\:')
|
| 180 |
+
|
| 181 |
+
ffmpeg_cmd = [
|
| 182 |
+
'ffmpeg', '-y',
|
| 183 |
+
'-i', video_file,
|
| 184 |
+
'-vf', f"ass={ass_path_escaped}",
|
| 185 |
+
'-c:a', 'copy',
|
| 186 |
+
'-c:v', 'libx264',
|
| 187 |
+
'-preset', 'medium',
|
| 188 |
+
'-crf', '23',
|
| 189 |
+
output_path
|
| 190 |
+
]
|
| 191 |
+
|
| 192 |
+
print(f"FFmpeg command: {' '.join(ffmpeg_cmd)}")
|
| 193 |
+
|
| 194 |
+
try:
|
| 195 |
+
ffmpeg_result = subprocess.run(
|
| 196 |
+
ffmpeg_cmd,
|
| 197 |
+
check=True,
|
| 198 |
+
capture_output=True,
|
| 199 |
+
text=True,
|
| 200 |
+
timeout=600
|
| 201 |
+
)
|
| 202 |
+
print(f"FFmpeg stdout: {ffmpeg_result.stdout}")
|
| 203 |
+
print(f"FFmpeg stderr: {ffmpeg_result.stderr}")
|
| 204 |
+
except subprocess.CalledProcessError as e:
|
| 205 |
+
print(f"FFmpeg failed!")
|
| 206 |
+
print(f"Return code: {e.returncode}")
|
| 207 |
+
print(f"Stdout: {e.stdout}")
|
| 208 |
+
print(f"Stderr: {e.stderr}")
|
| 209 |
+
shutil.rmtree(output_dir)
|
| 210 |
+
raise
|
| 211 |
+
|
| 212 |
+
if not os.path.exists(output_path):
|
| 213 |
+
shutil.rmtree(output_dir)
|
| 214 |
+
return None, None, f"Output video was not created at: {output_path}"
|
| 215 |
+
|
| 216 |
+
print(f"Video created: {output_path} ({os.path.getsize(output_path)} bytes)")
|
| 217 |
+
|
| 218 |
+
progress(1.0, desc="Complete!")
|
| 219 |
+
|
| 220 |
+
# Create status with preview
|
| 221 |
+
preview_lines = []
|
| 222 |
+
for i, chunk in enumerate(result['chunks'][:10], 1):
|
| 223 |
+
text = chunk['text'].strip()
|
| 224 |
+
start_time = chunk['timestamp'][0]
|
| 225 |
+
preview_lines.append(f"{i}. [{start_time:.1f}s] {text}")
|
| 226 |
+
|
| 227 |
+
preview_text = "\n".join(preview_lines)
|
| 228 |
+
full_text = " ".join([c['text'].strip() for c in result['chunks']])
|
| 229 |
+
|
| 230 |
+
status = f"""Processing Complete!
|
| 231 |
+
Statistics:
|
| 232 |
+
- Model: Whisper Large V3 Turbo
|
| 233 |
+
- Device: {device}
|
| 234 |
+
- Segments: {len(result['chunks'])}
|
| 235 |
+
- Duration: {result['chunks'][-1]['timestamp'][1]:.1f}s
|
| 236 |
+
- Language: {language}
|
| 237 |
+
|
| 238 |
+
Preview (first 10 segments):
|
| 239 |
+
{preview_text}
|
| 240 |
+
|
| 241 |
+
Full Transcription:
|
| 242 |
+
{full_text[:500]}{"..." if len(full_text) > 500 else ""}
|
| 243 |
+
|
| 244 |
+
Files created successfully!
|
| 245 |
+
"""
|
| 246 |
+
|
| 247 |
+
# Don't cleanup output_dir - Gradio needs these files
|
| 248 |
+
return srt_path, output_path, status
|
| 249 |
+
|
| 250 |
+
except subprocess.TimeoutExpired:
|
| 251 |
+
try:
|
| 252 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 253 |
+
shutil.rmtree(output_dir, ignore_errors=True)
|
| 254 |
+
except:
|
| 255 |
+
pass
|
| 256 |
+
return None, None, "Processing timeout. Try a shorter video."
|
| 257 |
+
except subprocess.CalledProcessError as e:
|
| 258 |
+
try:
|
| 259 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 260 |
+
shutil.rmtree(output_dir, ignore_errors=True)
|
| 261 |
+
except:
|
| 262 |
+
pass
|
| 263 |
+
error_detail = e.stderr.decode() if hasattr(e.stderr, 'decode') else str(e.stderr) if e.stderr else str(e)
|
| 264 |
+
return None, None, f"FFmpeg error: {error_detail}"
|
| 265 |
+
except Exception as e:
|
| 266 |
+
try:
|
| 267 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 268 |
+
shutil.rmtree(output_dir, ignore_errors=True)
|
| 269 |
+
except:
|
| 270 |
+
pass
|
| 271 |
+
import traceback
|
| 272 |
+
error_trace = traceback.format_exc()
|
| 273 |
+
print(f"Error: {error_trace}")
|
| 274 |
+
return None, None, f"Error: {str(e)}\n\nDetails:\n{error_trace}"
|
| 275 |
+
finally:
|
| 276 |
+
# Only cleanup temp_dir (audio processing), keep output_dir for Gradio
|
| 277 |
+
try:
|
| 278 |
+
shutil.rmtree(temp_dir)
|
| 279 |
+
print(f"Cleaned up temp directory: {temp_dir}")
|
| 280 |
+
except Exception as e:
|
| 281 |
+
print(f"Cleanup warning: {e}")
|
| 282 |
+
# Note: output_dir is NOT cleaned up here - Gradio needs access to the files
|
| 283 |
+
# The files will be cleaned up by the OS eventually from /tmp/, 'w', encoding='utf-8-sig') as f:
|
| 284 |
+
for i, chunk in enumerate(result['chunks'], 1):
|
| 285 |
+
start, end = chunk['timestamp']
|
| 286 |
+
text = chunk['text'].strip()
|
| 287 |
+
|
| 288 |
+
if text:
|
| 289 |
+
f.write(f"{i}\n")
|
| 290 |
+
f.write(f"{format_srt_time(start)} --> {format_srt_time(end)}\n")
|
| 291 |
+
f.write(f"{text}\n\n")
|
| 292 |
+
|
| 293 |
print(f"SRT file write completed")
|
| 294 |
except Exception as e:
|
| 295 |
print(f"Error writing SRT file: {e}")
|