Spaces:
Running
Running
File size: 20,581 Bytes
26ca5eb | 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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | import gradio as gr
import base64
import json
import re
from datetime import datetime
from io import BytesIO
# JavaScript code for video processing and OCR
VIDEO_PROCESSING_JS = """
function() {
const state = {
videoLoaded: false,
videoFile: null,
subtitles: [],
currentTime: 0,
duration: 0,
isPlaying: false,
isMuted: false,
subtitleBox: { x: 10, y: 70, width: 80, height: 20 },
processing: false,
ocrWorker: null,
currentSource: 'file',
cameraActive: false,
liveProcessing: false,
liveStartTime: null,
liveProcessingInterval: null
};
// Language codes for Tesseract
const LANGUAGES = {
'English': 'eng',
'Japanese': 'jpn',
'Korean': 'kor',
'Chinese (Simplified)': 'chi_sim',
'Chinese (Traditional)': 'chi_tra',
'Thai': 'tha',
'Vietnamese': 'vie',
'Hindi': 'hin',
'Arabic': 'ara',
'Russian': 'rus',
'Spanish': 'spa',
'French': 'fra',
'German': 'deu',
'Portuguese': 'por',
'Italian': 'ita',
'Dutch': 'dut',
'Polish': 'pol',
'Turkish': 'tur'
};
return { state: state, languages: LANGUAGES };
}
"""
def create_video_ocr_app():
"""Create a comprehensive Video Subtitle OCR Extractor application"""
with gr.Blocks() as demo:
# Header with branding
gr.Markdown("""
<div style="text-align: center; padding: 20px 0; border-bottom: 1px solid #2d2d3a; margin-bottom: 30px;">
<h1 style="font-size: 2.5rem; margin-bottom: 10px; background: linear-gradient(135deg, #6366f1, #ec4899); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
๐ฌ Video Subtitle OCR Extractor
</h1>
<p style="color: #94a3b8; font-size: 1.1rem;">
Extract hardcoded subtitles from any video using OCR technology
</p>
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #6366f1; text-decoration: none; font-size: 0.9rem;">
Built with anycoder
</a>
</div>
""")
with gr.Row():
# Left Column - Video Upload and Preview
with gr.Column(scale=2):
# Video Upload Section
gr.Markdown("### ๐ Video Source")
with gr.Tabs():
with gr.Tab("๐ Upload File"):
video_input = gr.Video(
label="Upload Video File",
sources=["upload"],
height=400,
elem_id="main-video"
)
gr.Markdown("*Supports MP4, WebM, MOV, AVI formats*")
with gr.Tab("๐ท Live Camera"):
gr.Markdown("""
<div style="background: #1a1a25; padding: 20px; border-radius: 12px; text-align: center;">
<p style="color: #94a3b8; margin-bottom: 10px;">Use live camera for real-time subtitle extraction</p>
<p style="color: #64748b; font-size: 0.85rem;">๐ท Click "Start Camera" to begin recording</p>
</div>
""")
live_camera_btn = gr.Button("๐ท Start Live Camera", variant="primary")
live_camera_status = gr.Textbox(label="Camera Status", value="Not active", interactive=False)
# Video Preview
gr.Markdown("### โถ๏ธ Video Preview")
with gr.Row():
with gr.Column(scale=3):
video_preview = gr.Video(
label="Video Preview",
height=350,
interactive=False,
elem_id="video-preview"
)
with gr.Column(scale=1):
gr.Markdown("**Playback Controls**")
play_btn = gr.Button("โถ Play", variant="primary")
pause_btn = gr.Button("โธ Pause")
restart_btn = gr.Button("โฎ Restart")
current_time_display = gr.Textbox(
label="Current Time",
value="00:00:00",
interactive=False
)
# Subtitle Region Selection
gr.Markdown("### ๐ Subtitle Region Selection")
with gr.Row():
with gr.Column(scale=1):
x_pos = gr.Slider(
minimum=0, maximum=90, value=10, step=1,
label="Horizontal Position (X)",
info="X-axis position of subtitle box"
)
with gr.Column(scale=1):
y_pos = gr.Slider(
minimum=0, maximum=90, value=70, step=1,
label="Vertical Position (Y)",
info="Y-axis position of subtitle box"
)
with gr.Row():
with gr.Column(scale=1):
box_width = gr.Slider(
minimum=10, maximum=100, value=80, step=1,
label="Width (%)",
info="Width of subtitle detection area"
)
with gr.Column(scale=1):
box_height = gr.Slider(
minimum=5, maximum=40, value=20, step=1,
label="Height (%)",
info="Height of subtitle detection area"
)
with gr.Row():
reset_region_btn = gr.Button("โบ Reset Region", variant="secondary")
lock_aspect_btn = gr.Button("๐ Lock Aspect Ratio", variant="secondary")
# Time Range Selection
gr.Markdown("### โฑ๏ธ Processing Time Range")
with gr.Row():
with gr.Column(scale=1):
start_time_input = gr.Textbox(
label="Start Time",
placeholder="Leave empty for start (e.g., 00:01:30)",
value=""
)
set_start_btn = gr.Button("Set to Current Time")
with gr.Column(scale=1):
end_time_input = gr.Textbox(
label="End Time",
placeholder="Leave empty for end (e.g., 00:05:00)",
value=""
)
set_end_btn = gr.Button("Set to Current Time")
# Language Selection
gr.Markdown("### ๐ OCR Language")
with gr.Row():
language_select = gr.Dropdown(
choices=[
"English", "Japanese", "Korean", "Chinese (Simplified)",
"Chinese (Traditional)", "Thai", "Vietnamese", "Hindi",
"Arabic", "Russian", "Spanish", "French", "German",
"Portuguese", "Italian", "Dutch", "Polish", "Turkish"
],
value="English",
label="Select OCR Language",
info="Prioritizes selected language for better accuracy"
)
language_priority = gr.Checkbox(
label="Enable Language Priority",
value=True,
info="Use language-specific OCR optimizations"
)
# Process Button
with gr.Row():
process_btn = gr.Button(
"โก Start OCR Processing",
variant="primary",
size="lg",
scale=2
)
stop_process_btn = gr.Button("โน Stop", variant="stop", size="lg")
progress_bar = gr.Slider(
minimum=0, maximum=100, value=0,
label="Processing Progress",
interactive=False,
visible=False
)
status_text = gr.Textbox(
label="Status",
value="Ready",
interactive=False
)
# Right Column - Results
with gr.Column(scale=1):
# Options
gr.Markdown("### โ๏ธ Options")
with gr.Row():
show_overlay = gr.Checkbox(
label="Show Selection Box",
value=True
)
auto_play = gr.Checkbox(
label="Auto Play Preview",
value=True
)
with gr.Row():
highlight_active = gr.Checkbox(
label="Highlight Active Subtitles",
value=True
)
preserve_timing = gr.Checkbox(
label="Frame-accurate Timing",
value=True
)
# Results
gr.Markdown("### ๐ Extracted Subtitles")
subtitle_count = gr.Textbox(
value="0 lines extracted",
label="Results",
interactive=False
)
subtitles_output = gr.Dataframe(
headers=["#", "Start Time", "End Time", "Subtitle Text"],
datatype=["number", "str", "str", "str"],
label="Subtitles",
interactive=False,
height=400
)
# Active subtitle display
active_subtitle = gr.Textbox(
label="Currently Active Subtitle",
value="",
interactive=False,
lines=3
)
# Export Buttons
gr.Markdown("### ๐พ Export")
with gr.Row():
export_srt = gr.Button("๐ Export SRT", variant="primary")
export_txt = gr.Button("๐ Export TXT")
export_json = gr.Button("๐ Export JSON")
clear_btn = gr.Button("๐๏ธ Clear All", variant="stop")
# Statistics
gr.Markdown("### ๐ Statistics")
with gr.Row():
total_lines = gr.Number(value=0, label="Total Lines", interactive=False)
processing_time = gr.Textbox(label="Processing Time", value="-", interactive=False)
detected_language = gr.Textbox(label="Detected Language", value="-", interactive=False)
# Event Handlers
def update_preview(video_path):
"""Update video preview when file is uploaded"""
if video_path:
return video_path
return None
def set_start_time(current_time):
"""Set start time from current playback position"""
return format_time(current_time)
def set_end_time(current_time):
"""Set end time from current playback position"""
return format_time(current_time)
def reset_region():
"""Reset subtitle region to defaults"""
return 10, 70, 80, 20
def format_time(seconds):
"""Format seconds to HH:MM:SS"""
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
def parse_time(time_str):
"""Parse time string to seconds"""
if not time_str:
return None
try:
parts = time_str.split(':')
if len(parts) == 3:
return int(parts[0]) * 3600 + int(parts[1]) * 60 + int(parts[2])
elif len(parts) == 2:
return int(parts[0]) * 60 + int(parts[1])
return float(time_str)
except:
return None
def generate_srt(subtitles):
"""Generate SRT format subtitles"""
if not subtitles:
return ""
srt_content = ""
for i, sub in enumerate(subtitles, 1):
srt_content += f"{i}\\n"
srt_content += f"{sub['start']} --> {sub['end']}\\n"
srt_content += f"{sub['text']}\\n\\n"
return srt_content
def generate_txt(subtitles):
"""Generate plain text subtitles"""
if not subtitles:
return ""
return "\\n".join([sub['text'] for sub in subtitles])
def generate_json(subtitles):
"""Generate JSON format subtitles"""
return json.dumps(subtitles, indent=2, ensure_ascii=False)
# Connect video upload to preview
video_input.change(
fn=update_preview,
inputs=[video_input],
outputs=[video_preview]
)
# Set time buttons
set_start_btn.click(
fn=set_start_time,
inputs=[current_time_display],
outputs=[start_time_input]
)
set_end_btn.click(
fn=set_end_time,
inputs=[current_time_display],
outputs=[end_time_input]
)
# Reset region
reset_region_btn.click(
fn=reset_region,
inputs=[],
outputs=[x_pos, y_pos, box_width, box_height]
)
# Process button - Main OCR processing
def process_video(
video_path,
x, y, width, height,
start_time_str, end_time_str,
language, use_priority,
highlight, preserve_time
):
"""Process video and extract subtitles using OCR"""
if not video_path:
return (
"No video loaded. Please upload a video first.",
[],
"0 lines extracted",
"", 0, "-", "-"
)
start_time = parse_time(start_time_str) if start_time_str else 0
end_time = parse_time(end_time_str)
# Return processing info - actual OCR happens client-side
yield (
"Video loaded. Starting OCR extraction...",
[],
"0 lines extracted",
"", 0, "-", language
)
# Note: Actual OCR processing requires client-side JavaScript
# This is a placeholder that returns processing parameters
processing_params = {
'video_path': video_path,
'region': {'x': x, 'y': y, 'width': width, 'height': height},
'start_time': start_time,
'end_time': end_time,
'language': language,
'use_priority': use_priority,
'highlight': highlight,
'preserve_time': preserve_time
}
return (
f"Processing with params: {json.dumps(processing_params)}",
[],
"Processing...",
"", 0, "-", language
)
# Export handlers
def export_subtitles_srt(subtitles_json):
"""Export subtitles as SRT"""
if isinstance(subtitles_json, str):
subtitles = json.loads(subtitles_json) if subtitles_json else []
else:
subtitles = subtitles_json
return generate_srt(subtitles)
def export_subtitles_txt(subtitles_json):
"""Export subtitles as TXT"""
if isinstance(subtitles_json, str):
subtitles = json.loads(subtitles_json) if subtitles_json else []
else:
subtitles = subtitles_json
return generate_txt(subtitles)
def export_subtitles_json(subtitles_json):
"""Export subtitles as JSON"""
if isinstance(subtitles_json, str):
subtitles = json.loads(subtitles_json) if subtitles_json else []
else:
subtitles = subtitles_json
return generate_json(subtitles)
# Connect export buttons
export_srt.click(
fn=export_subtitles_srt,
inputs=[subtitles_output],
outputs=[subtitles_output]
)
# Playback controls
play_btn.click(
fn=lambda: "Playing...",
inputs=[],
outputs=[status_text]
)
pause_btn.click(
fn=lambda: "Paused",
inputs=[],
outputs=[status_text]
)
# Clear all
def clear_all():
return None, "", "", [], "0 lines extracted", "", 0, "-", "-", "-"
clear_btn.click(
fn=clear_all,
inputs=[],
outputs=[
video_input, start_time_input, end_time_input,
subtitles_output, subtitle_count, active_subtitle,
total_lines, processing_time, detected_language, video_preview
]
)
return demo
def format_time(seconds):
"""Format seconds to HH:MM:SS,mmm"""
if seconds is None:
return "00:00:00,000"
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
millis = int((seconds % 1) * 1000)
return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"
def format_time_simple(seconds):
"""Format seconds to HH:MM:SS"""
if seconds is None:
return "00:00:00"
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
def parse_time(time_str):
"""Parse time string (HH:MM:SS or HH:MM:SS,mmm) to seconds"""
if not time_str:
return None
try:
# Handle both comma and dot milliseconds
time_str = time_str.replace(',', '.')
parts = time_str.split(':')
if len(parts) == 3:
hours = int(parts[0])
minutes = int(parts[1])
seconds = float(parts[2])
return hours * 3600 + minutes * 60 + seconds
elif len(parts) == 2:
minutes = int(parts[0])
seconds = float(parts[1])
return minutes * 60 + seconds
return float(time_str)
except (ValueError, AttributeError):
return None
# Create and launch the demo
if __name__ == "__main__":
demo = create_video_ocr_app()
demo.launch(
title="Video Subtitle OCR Extractor",
theme=gr.themes.Soft(
primary_hue="indigo",
secondary_hue="pink",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="lg",
radius_size="md"
),
css="""
.gradio-container {max-width: 1600px !important;}
#main-video video {max-height: 400px;}
#video-preview video {max-height: 350px;}
""",
footer_links=[
{"label": "Video Subtitle OCR Extractor", "url": "#"},
{"label": "Built with Gradio", "url": "https://gradio.app"},
{"label": "Powered by Tesseract.js", "url": "https://tesseract.projectnaptha.com/"}
]
) |