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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Video Subtitle Remover - Google Colab\n",
8
+ "\n",
9
+ "This notebook allows you to run Video Subtitle Remover in Google Colab with free GPU access.\n",
10
+ "\n",
11
+ "**New in this version:**\n",
12
+ "- \ud83d\udcdd Subtitle extraction to SRT files with OCR\n",
13
+ "- \ud83e\udd16 Automatic model downloading from Hugging Face\n",
14
+ "- \u26a1 Improved performance\n",
15
+ "\n",
16
+ "**Requirements:**\n",
17
+ "- Google account\n",
18
+ "- Video file (upload to Google Drive or use sample)\n",
19
+ "\n",
20
+ "**Colab Environment:**\n",
21
+ "- Python: 3.10\n",
22
+ "- CUDA: 12.2 (Tesla T4 GPU)\n",
23
+ "- GPU Memory: 15GB\n",
24
+ "\n",
25
+ "**Recommended Algorithm:** STTN (fastest for Colab's limited runtime)"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type": "markdown",
30
+ "metadata": {},
31
+ "source": [
32
+ "## Step 1: Check GPU and Environment"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": 1,
38
+ "metadata": {},
39
+ "outputs": [],
40
+ "source": [
41
+ "# Check GPU availability\n",
42
+ "!nvidia-smi\n",
43
+ "\n",
44
+ "import torch\n",
45
+ "print(f\"Python version: 3.10\")\n",
46
+ "print(f\"PyTorch version: {torch.__version__}\")\n",
47
+ "print(f\"CUDA available: {torch.cuda.is_available()}\")\n",
48
+ "if torch.cuda.is_available():\n",
49
+ " print(f\"CUDA version: {torch.version.cuda}\")\n",
50
+ " print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n",
51
+ " print(f\"GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB\")"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "markdown",
56
+ "metadata": {},
57
+ "source": [
58
+ "## Step 2: Clone Repository"
59
+ ]
60
+ },
61
+ {
62
+ "cell_type": "markdown",
63
+ "metadata": {},
64
+ "source": []
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": 2,
69
+ "metadata": {},
70
+ "outputs": [],
71
+ "source": [
72
+ "# Clone the repository\n",
73
+ "!git clone https://github.com/walidurrosyad/video-subtitle-remover.git\n",
74
+ "%cd video-subtitle-remover"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "markdown",
79
+ "metadata": {},
80
+ "source": [
81
+ "## Step 2.5: Verify Installation\n",
82
+ "\n",
83
+ "Quick check that the repository was cloned successfully."
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "execution_count": 3,
89
+ "metadata": {},
90
+ "outputs": [],
91
+ "source": [
92
+ "# Verify the repository structure\n",
93
+ "import os\n",
94
+ "\n",
95
+ "repo_path = '/content/video-subtitle-remover'\n",
96
+ "if os.path.exists(repo_path):\n",
97
+ " print('\u2713 Repository cloned successfully')\n",
98
+ " print(f' Path: {repo_path}')\n",
99
+ "else:\n",
100
+ " print('\u274c Repository not found')\n",
101
+ " print(' Please run Step 2 first')"
102
+ ]
103
+ },
104
+ {
105
+ "cell_type": "markdown",
106
+ "metadata": {},
107
+ "source": [
108
+ "## Step 3: Install Dependencies\n",
109
+ "\n",
110
+ "Colab already has many packages. We'll install missing ones."
111
+ ]
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": 4,
116
+ "metadata": {},
117
+ "outputs": [],
118
+ "source": [
119
+ "# Install dependencies\n",
120
+ "# Note: Colab already has torch, torchvision, opencv-python, numpy, etc.\n",
121
+ "!pip install -q filesplit==3.0.2 albumentations scikit-image imgaug pyclipper lmdb\n",
122
+ "!pip install -q PyYAML omegaconf tqdm easydict scikit-learn pandas webdataset\n",
123
+ "!pip install -q protobuf av einops paddleocr paddle2onnx onnxruntime-gpu\n",
124
+ "\n",
125
+ "# Install PaddlePaddle GPU version (compatible with Colab)\n",
126
+ "!pip install -q paddlepaddle-gpu==2.6.2\n",
127
+ "\n",
128
+ "# Advanced Inpainting Models (Optional - only install if using these modes)\n",
129
+ "# Uncomment the line below to enable Stable Diffusion, DiffuEraser, E2FGVI\n",
130
+ "# !pip install -q diffusers transformers accelerate\n",
131
+ "\n",
132
+ "print(\"\u2713 Dependencies installed!\")"
133
+ ]
134
+ },
135
+ {
136
+ "cell_type": "markdown",
137
+ "metadata": {},
138
+ "source": [
139
+ "## Step 4: Mount Google Drive (Optional)\n",
140
+ "\n",
141
+ "Mount your Google Drive to access videos stored there."
142
+ ]
143
+ },
144
+ {
145
+ "cell_type": "code",
146
+ "execution_count": null,
147
+ "metadata": {},
148
+ "outputs": [],
149
+ "source": [
150
+ "from google.colab import drive\n",
151
+ "drive.mount('/content/drive')\n",
152
+ "\n",
153
+ "# Your videos will be accessible at:\n",
154
+ "# /content/drive/MyDrive/your_video.mp4"
155
+ ]
156
+ },
157
+ {
158
+ "cell_type": "markdown",
159
+ "metadata": {},
160
+ "source": [
161
+ "## Step 5: Configure Settings\n",
162
+ "\n",
163
+ "Adjust these settings based on your needs:"
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "execution_count": null,
169
+ "metadata": {},
170
+ "outputs": [],
171
+ "source": [
172
+ "# === CONFIGURATION ===\n",
173
+ "\n",
174
+ "# Algorithm selection\n",
175
+ "# Options:\n",
176
+ "# 'STTN' - Fast, real-time video (recommended for Colab)\n",
177
+ "# 'LAMA' - High quality for images/animation\n",
178
+ "# 'PROPAINTER' - Best quality, very slow, high VRAM\n",
179
+ "# 'SD' - Stable Diffusion (NEW - requires extra install above)\n",
180
+ "# 'DIFFUERASER' - Specialized subtitle removal (Coming soon)\n",
181
+ "# 'E2FGVI' - Fast flow-guided (Coming soon)\n",
182
+ "ALGORITHM = 'STTN'\n",
183
+ "\n",
184
+ "# STTN Settings (recommended for Colab)\n",
185
+ "STTN_SKIP_DETECTION = True # Much faster, processes entire subtitle area\n",
186
+ "STTN_MAX_LOAD_NUM = 40 # Reduce if OOM (30-50 for T4 GPU)\n",
187
+ "STTN_NEIGHBOR_STRIDE = 5\n",
188
+ "STTN_REFERENCE_LENGTH = 10\n",
189
+ "\n",
190
+ "# LAMA Settings\n",
191
+ "LAMA_SUPER_FAST = False # Set True for faster but lower quality\n",
192
+ "\n",
193
+ "# ProPainter Settings (requires 16GB+ GPU, not recommended for Colab)\n",
194
+ "PROPAINTER_MAX_LOAD_NUM = 40 # Very low for T4 GPU\n",
195
+ "\n",
196
+ "# Stable Diffusion Settings (NEW)\n",
197
+ "SD_STEPS = 50 # More steps = better quality but slower\n",
198
+ "SD_GUIDANCE_SCALE = 7.5 # How much to follow the prompt\n",
199
+ "SD_PROMPT = \"natural scene, high quality\" # Text guidance\n",
200
+ "\n",
201
+ "# Video path (change this)\n",
202
+ "# Option 1: Use sample video\n",
203
+ "VIDEO_PATH = '/content/video-subtitle-remover/test/test.mp4'\n",
204
+ "\n",
205
+ "# Option 2: Use video from Google Drive (uncomment)\n",
206
+ "# VIDEO_PATH = '/content/drive/MyDrive/my_video.mp4'\n",
207
+ "\n",
208
+ "# Subtitle area (optional, in pixels: ymin, ymax, xmin, xmax)\n",
209
+ "# None = auto-detect subtitle area\n",
210
+ "SUBTITLE_AREA = None\n",
211
+ "\n",
212
+ "# Example: Bottom 20% of 1080p video\n",
213
+ "# SUBTITLE_AREA = (864, 1080, 0, 1920)\n",
214
+ "\n",
215
+ "print(f\"Configuration:\")\n",
216
+ "print(f\" Algorithm: {ALGORITHM}\")\n",
217
+ "print(f\" Video: {VIDEO_PATH}\")\n",
218
+ "print(f\" Subtitle area: {SUBTITLE_AREA or 'Auto-detect'}\")"
219
+ ]
220
+ },
221
+ {
222
+ "cell_type": "markdown",
223
+ "metadata": {},
224
+ "source": [
225
+ "## Step 6: Apply Configuration"
226
+ ]
227
+ },
228
+ {
229
+ "cell_type": "code",
230
+ "execution_count": 6,
231
+ "metadata": {},
232
+ "outputs": [],
233
+ "source": [
234
+ "# Modify config.py with our settings\n",
235
+ "import sys\n",
236
+ "sys.path.insert(0, '/content/video-subtitle-remover')\n",
237
+ "sys.path.insert(0, '/content/video-subtitle-remover/backend')\n",
238
+ "\n",
239
+ "from backend import config\n",
240
+ "from backend.config import InpaintMode\n",
241
+ "\n",
242
+ "# Apply algorithm selection\n",
243
+ "if ALGORITHM == 'STTN':\n",
244
+ " config.MODE = InpaintMode.STTN\n",
245
+ " config.STTN_SKIP_DETECTION = STTN_SKIP_DETECTION\n",
246
+ " config.STTN_MAX_LOAD_NUM = STTN_MAX_LOAD_NUM\n",
247
+ " config.STTN_NEIGHBOR_STRIDE = STTN_NEIGHBOR_STRIDE\n",
248
+ " config.STTN_REFERENCE_LENGTH = STTN_REFERENCE_LENGTH\n",
249
+ "elif ALGORITHM == 'LAMA':\n",
250
+ " config.MODE = InpaintMode.LAMA\n",
251
+ " config.LAMA_SUPER_FAST = LAMA_SUPER_FAST\n",
252
+ "elif ALGORITHM == 'PROPAINTER':\n",
253
+ " config.MODE = InpaintMode.PROPAINTER\n",
254
+ " config.PROPAINTER_MAX_LOAD_NUM = PROPAINTER_MAX_LOAD_NUM\n",
255
+ "elif ALGORITHM == 'SD':\n",
256
+ " config.MODE = InpaintMode.STABLE_DIFFUSION\n",
257
+ " config.SD_STEPS = SD_STEPS\n",
258
+ " config.SD_GUIDANCE_SCALE = SD_GUIDANCE_SCALE\n",
259
+ " config.SD_PROMPT = SD_PROMPT\n",
260
+ "elif ALGORITHM == 'DIFFUERASER':\n",
261
+ " config.MODE = InpaintMode.DIFFUERASER\n",
262
+ " print('\u26a0\ufe0f DiffuEraser not yet implemented, will fall back to LAMA')\n",
263
+ "elif ALGORITHM == 'E2FGVI':\n",
264
+ " config.MODE = InpaintMode.E2FGVI\n",
265
+ " print('\u26a0\ufe0f E2FGVI not yet implemented, will fall back to STTN')\n",
266
+ "\n",
267
+ "print(f\"\u2713 Configuration applied!\")\n",
268
+ "print(f\" Using device: {config.device}\")\n",
269
+ "print(f\" Mode: {config.MODE.value}\")"
270
+ ]
271
+ },
272
+ {
273
+ "cell_type": "markdown",
274
+ "metadata": {},
275
+ "source": [
276
+ "## Step 7: Process Video"
277
+ ]
278
+ },
279
+ {
280
+ "cell_type": "code",
281
+ "execution_count": 7,
282
+ "metadata": {},
283
+ "outputs": [],
284
+ "source": [
285
+ "import multiprocessing\n",
286
+ "from backend.main import SubtitleRemover\n",
287
+ "import os\n",
288
+ "\n",
289
+ "# Check if video exists\n",
290
+ "if not os.path.exists(VIDEO_PATH):\n",
291
+ " print(f\"\u274c Error: Video not found at {VIDEO_PATH}\")\n",
292
+ " print(\"Please upload your video or update VIDEO_PATH\")\n",
293
+ "else:\n",
294
+ " print(f\"Processing video: {VIDEO_PATH}\")\n",
295
+ " print(f\"This may take several minutes...\")\n",
296
+ " print(f\"\")\n",
297
+ " \n",
298
+ " # Set multiprocessing start method\n",
299
+ " try:\n",
300
+ " multiprocessing.set_start_method(\"spawn\")\n",
301
+ " except:\n",
302
+ " pass\n",
303
+ " \n",
304
+ " # Create SubtitleRemover instance\n",
305
+ " sr = SubtitleRemover(VIDEO_PATH, sub_area=SUBTITLE_AREA, gui_mode=False)\n",
306
+ " \n",
307
+ " # Run processing\n",
308
+ " sr.run()\n",
309
+ " \n",
310
+ " # Output location\n",
311
+ " output_path = sr.video_out_name\n",
312
+ " print(f\"\\n\u2713 Processing complete!\")\n",
313
+ " print(f\"Output saved to: {output_path}\")"
314
+ ]
315
+ },
316
+ {
317
+ "cell_type": "markdown",
318
+ "metadata": {},
319
+ "source": [
320
+ "## Step 8: Download Result\n",
321
+ "\n",
322
+ "Download the processed video to your computer."
323
+ ]
324
+ },
325
+ {
326
+ "cell_type": "code",
327
+ "execution_count": 11,
328
+ "metadata": {},
329
+ "outputs": [],
330
+ "source": [
331
+ "from google.colab import files\n",
332
+ "import os\n",
333
+ "\n",
334
+ "# Get output filename\n",
335
+ "video_name = os.path.basename(VIDEO_PATH)\n",
336
+ "video_name_no_ext = os.path.splitext(video_name)[0]\n",
337
+ "output_file = f\"{video_name_no_ext}_no_sub.mp4\"\n",
338
+ "output_path = os.path.join(os.path.dirname(VIDEO_PATH), output_file)\n",
339
+ "\n",
340
+ "if os.path.exists(output_path):\n",
341
+ " print(f\"Downloading: {output_file}\")\n",
342
+ " files.download(output_path)\n",
343
+ "else:\n",
344
+ " print(f\"\u274c Output file not found: {output_path}\")\n",
345
+ " print(\"Check if processing completed successfully.\")"
346
+ ]
347
+ },
348
+ {
349
+ "cell_type": "markdown",
350
+ "metadata": {},
351
+ "source": [
352
+ "## Step 9: Save to Google Drive (Optional)\n",
353
+ "\n",
354
+ "Save the output video to Google Drive instead of downloading."
355
+ ]
356
+ },
357
+ {
358
+ "cell_type": "code",
359
+ "execution_count": 15,
360
+ "metadata": {},
361
+ "outputs": [],
362
+ "source": [
363
+ "import shutil\n",
364
+ "import os\n",
365
+ "\n",
366
+ "# Destination in Google Drive\n",
367
+ "drive_output_path = '/content/drive/MyDrive/video_subtitle_remover_output/'\n",
368
+ "\n",
369
+ "# Create directory if it doesn't exist\n",
370
+ "os.makedirs(drive_output_path, exist_ok=True)\n",
371
+ "\n",
372
+ "# Copy output file\n",
373
+ "if os.path.exists(output_path):\n",
374
+ " dest_file = os.path.join(drive_output_path, output_file)\n",
375
+ " shutil.copy(output_path, dest_file)\n",
376
+ " print(f\"\u2713 Saved to Google Drive: {dest_file}\")\n",
377
+ "else:\n",
378
+ " print(f\"\u274c Output file not found\")"
379
+ ]
380
+ },
381
+ {
382
+ "cell_type": "markdown",
383
+ "metadata": {},
384
+ "source": [
385
+ "## Troubleshooting\n",
386
+ "\n",
387
+ "### Out of Memory (OOM)\n",
388
+ "```python\n",
389
+ "# Reduce batch size\n",
390
+ "STTN_MAX_LOAD_NUM = 30\n",
391
+ "# Or use LAMA\n",
392
+ "ALGORITHM = 'LAMA'\n",
393
+ "```\n",
394
+ "\n",
395
+ "### Processing too slow\n",
396
+ "```python\n",
397
+ "# Enable skip detection\n",
398
+ "STTN_SKIP_DETECTION = True\n",
399
+ "# Or use super fast mode\n",
400
+ "ALGORITHM = 'LAMA'\n",
401
+ "LAMA_SUPER_FAST = True\n",
402
+ "```\n",
403
+ "\n",
404
+ "### Subtitles not removed\n",
405
+ "```python\n",
406
+ "# Set manual subtitle area (bottom 20% of 1080p)\n",
407
+ "SUBTITLE_AREA = (864, 1080, 0, 1920)\n",
408
+ "```\n",
409
+ "\n",
410
+ "### GPU not detected\n",
411
+ "1. Runtime \u2192 Change runtime type \u2192 Hardware accelerator \u2192 GPU\n",
412
+ "2. Restart runtime\n",
413
+ "\n",
414
+ "### Session timeout\n",
415
+ "- Colab free tier has time limits\n",
416
+ "- Process smaller videos (<10 min)\n",
417
+ "- Or use Colab Pro for longer sessions"
418
+ ]
419
+ },
420
+ {
421
+ "cell_type": "markdown",
422
+ "metadata": {},
423
+ "source": [
424
+ "## Advanced: Batch Processing\n",
425
+ "\n",
426
+ "Process multiple videos from Google Drive:"
427
+ ]
428
+ },
429
+ {
430
+ "cell_type": "code",
431
+ "execution_count": 14,
432
+ "metadata": {},
433
+ "outputs": [],
434
+ "source": [
435
+ "import glob\n",
436
+ "import os\n",
437
+ "from backend.main import SubtitleRemover\n",
438
+ "\n",
439
+ "# Folder containing videos\n",
440
+ "video_folder = '/content/video-subtitle-remover/test/'\n",
441
+ "\n",
442
+ "# Find all MP4 files\n",
443
+ "video_files = glob.glob(os.path.join(video_folder, '*.mp4'))\n",
444
+ "\n",
445
+ "print(f\"Found {len(video_files)} videos to process\")\n",
446
+ "\n",
447
+ "for i, video_path in enumerate(video_files, 1):\n",
448
+ " print(f\"\\n[{i}/{len(video_files)}] Processing: {os.path.basename(video_path)}\")\n",
449
+ " \n",
450
+ " try:\n",
451
+ " sr = SubtitleRemover(video_path, sub_area=SUBTITLE_AREA, gui_mode=False)\n",
452
+ " sr.run()\n",
453
+ " print(f\"\u2713 Complete: {sr.video_out_name}\")\n",
454
+ " except Exception as e:\n",
455
+ " print(f\"\u274c Error: {e}\")\n",
456
+ " continue\n",
457
+ "\n",
458
+ "print(f\"\\n\u2713 Batch processing complete!\")"
459
+ ]
460
+ },
461
+ {
462
+ "cell_type": "markdown",
463
+ "metadata": {},
464
+ "source": [
465
+ "## Tips for Best Results\n",
466
+ "\n",
467
+ "1. **Use STTN with skip detection** - Fastest for Colab\n",
468
+ "2. **Keep videos under 10 minutes** - Avoid session timeout\n",
469
+ "3. **Set manual subtitle area** - For better accuracy\n",
470
+ "4. **Monitor GPU memory** - Use `!nvidia-smi` in separate cell\n",
471
+ "5. **Save to Drive frequently** - Avoid data loss on timeout\n",
472
+ "\n",
473
+ "## Performance Expectations (Colab T4 GPU)\n",
474
+ "\n",
475
+ "| Video Length | Algorithm | Time |\n",
476
+ "|--------------|-----------|------|\n",
477
+ "| 1 min 720p | STTN (skip) | ~30s |\n",
478
+ "| 5 min 720p | STTN (skip) | ~2min |\n",
479
+ "| 1 min 720p | LAMA | ~3min |\n",
480
+ "| 5 min 720p | LAMA | ~15min |\n",
481
+ "\n",
482
+ "ProPainter is not recommended for Colab due to memory limitations."
483
+ ]
484
+ }
485
+ ],
486
+ "metadata": {
487
+ "accelerator": "GPU",
488
+ "colab": {
489
+ "gpuType": "T4",
490
+ "provenance": []
491
+ },
492
+ "kernelspec": {
493
+ "display_name": "Python 3 (ipykernel)",
494
+ "language": "python",
495
+ "name": "python3"
496
+ }
497
+ },
498
+ "nbformat": 4,
499
+ "nbformat_minor": 0
500
+ }