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
4be78c1
 
 
c8b6818
4be78c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8b6818
 
4be78c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8b6818
4be78c1
 
c8b6818
4be78c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8b6818
4be78c1
 
c8b6818
4be78c1
 
 
 
 
 
c8b6818
4be78c1
 
 
 
 
 
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import argparse
import datetime
import os
import sys
import tempfile
import time
import wave

import ffmpeg
import torch
import whisper
import whisper_timestamped
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
from transformers import pipeline as hf_pipeline

# ─────────────────────────────────────────────
# MODEL  (shared cache β€” loaded once for entire batch)
# ─────────────────────────────────────────────

_model_cache = {}


def load_model():
    """Load and cache the Apex model. Downloads automatically on first run (~1.5 GB)."""
    if "apex" not in _model_cache:
        print("Loading Whisper-Hindi2Hinglish-Apex...")
        print(
            "(First run will download ~1.5 GB β€” this happens once, then it's cached forever)\n"
        )

        model_id = "Oriserve/Whisper-Hindi2Hinglish-Apex"
        device = "cpu"
        torch_dtype = torch.float32

        model = AutoModelForSpeechSeq2Seq.from_pretrained(
            model_id,
            torch_dtype=torch_dtype,
            low_cpu_mem_usage=True,
            use_safetensors=True,
        ).to(device)

        processor = AutoProcessor.from_pretrained(model_id)

        model.generation_config.task = "transcribe"
        model.generation_config.language = "en"
        model.generation_config.no_repeat_ngram_size = 5
        model.generation_config.condition_on_prev_tokens = False

        _model_cache["apex"] = hf_pipeline(
            "automatic-speech-recognition",
            model=model,
            tokenizer=processor.tokenizer,
            feature_extractor=processor.feature_extractor,
            device=device,
            chunk_length_s=30,
            stride_length_s=5,
            return_timestamps=True,
            ignore_warning=True,
        )
        print("Model loaded successfully!\n")

    return _model_cache["apex"]


# ─────────────────────────────────────────────
# AUDIO EXTRACTION
# ─────────────────────────────────────────────


def extract_audio(video_path: str, output_dir: str) -> str:
    """Extract mono 16kHz WAV audio from a video file using FFmpeg."""
    audio_path = os.path.join(output_dir, "audio.wav")
    (
        ffmpeg.input(video_path)
        .output(audio_path, ac=1, ar="16000", format="wav")
        .overwrite_output()
        .run(quiet=True)
    )
    return audio_path


# ─────────────────────────────────────────────
# TRANSCRIPTION
# ─────────────────────────────────────────────


def transcribe(audio_path: str) -> list[dict]:
    """Transcribe audio and return list of segments with timestamps."""
    pipe = load_model()
    result = pipe(audio_path)

    raw_chunks = result.get("chunks", [])

    # Get audio duration to estimate timestamps when model returns None
    with wave.open(audio_path, "rb") as wf:
        audio_duration = wf.getnframes() / wf.getframerate()

    n = len(raw_chunks)
    segments = []

    for i, chunk in enumerate(raw_chunks):
        ts = chunk.get("timestamp", (None, None))
        text = chunk.get("text", "").strip()

        if not text:
            continue

        # Estimate start if missing
        if ts[0] is not None:
            start = ts[0]
        else:
            start = (i / n) * audio_duration if n > 0 else 0.0

        # Estimate end if missing
        if ts[1] is not None:
            end = ts[1]
        elif i + 1 < n:
            next_ts = raw_chunks[i + 1].get("timestamp", (None, None))
            end = next_ts[0] if next_ts[0] is not None else start + (audio_duration / n)
        else:
            end = audio_duration

        segments.append(
            {
                "id": len(segments),
                "start": start,
                "end": end,
                "text": text,
            }
        )

    return segments


# ─────────────────────────────────────────────
# WORD-LEVEL TIMESTAMPS (whisper-timestamped)
# ─────────────────────────────────────────────

_whisper_model_cache = {}


def load_whisper_model(model_size: str = "base"):
    """Load and cache OpenAI Whisper model for word-level timestamps."""
    if model_size not in _whisper_model_cache:
        print(f"Loading Whisper model for word-level timestamps: {model_size} ...")
        _whisper_model_cache[model_size] = whisper.load_model(model_size)
    return _whisper_model_cache[model_size]


def transcribe_word_level(
    audio_path: str, model_size: str = "base", words_per_line: int = 2
) -> list[dict]:
    """
    Transcribe audio with word-level timestamps using whisper-timestamped.
    Groups words into lines with specified words_per_line.
    """
    model = load_whisper_model(model_size)

    # Get word-level timestamps
    result = whisper_timestamped.transcribe_timestamped(
        model, audio_path, language="en", task="transcribe", verbose=False
    )

    # Extract all words with timestamps
    words = []
    for segment in result.get("segments", []):
        for word_info in segment.get("words", []):
            word_text = word_info.get("text", "").strip()
            if word_text:
                words.append(
                    {
                        "text": word_text,
                        "start": word_info.get("start", 0),
                        "end": word_info.get("end", 0),
                    }
                )

    if not words:
        return []

    # Group words into lines (words_per_line words per caption)
    segments = []
    current_line_words = []
    line_start = words[0]["start"]
    line_end = words[0]["end"]

    for i, word in enumerate(words):
        current_line_words.append(word["text"])
        line_end = word["end"]

        # Create a new segment when we hit words_per_line
        if len(current_line_words) >= words_per_line:
            segments.append(
                {
                    "id": len(segments),
                    "start": line_start,
                    "end": line_end,
                    "text": " ".join(current_line_words),
                }
            )
            current_line_words = []
            # Start next line from next word's start time
            if i + 1 < len(words):
                line_start = words[i + 1]["start"]

    # Add remaining words as final segment
    if current_line_words:
        segments.append(
            {
                "id": len(segments),
                "start": line_start,
                "end": line_end,
                "text": " ".join(current_line_words),
            }
        )

    return segments


# ─────────────────────────────────────────────
# SRT GENERATION
# ─────────────────────────────────────────────


def seconds_to_srt_time(seconds: float) -> str:
    """Convert float seconds β†’ HH:MM:SS,mmm (SRT format)."""
    td = datetime.timedelta(seconds=seconds)
    total_seconds = int(td.total_seconds())
    hours = total_seconds // 3600
    minutes = (total_seconds % 3600) // 60
    secs = total_seconds % 60
    millis = int((seconds - int(seconds)) * 1000)
    return f"{hours:02}:{minutes:02}:{secs:02},{millis:03}"


def segments_to_srt(segments: list[dict]) -> str:
    """Convert segments list to SRT string."""
    lines = []
    for i, seg in enumerate(segments, start=1):
        start = seconds_to_srt_time(seg["start"])
        end = seconds_to_srt_time(seg["end"])
        text = seg["text"].strip()
        lines.append(f"{i}\n{start} --> {end}\n{text}\n")
    return "\n".join(lines)


# ─────────────────────────────────────────────
# PREMIERE PRO FORMAT SUPPORT
# ─────────────────────────────────────────────


def get_video_fps(video_path: str) -> float:
    """Extract video frame rate using ffprobe."""
    try:
        import json
        import subprocess

        cmd = [
            "ffprobe",
            "-v",
            "error",
            "-select_streams",
            "v:0",
            "-show_entries",
            "stream=r_frame_rate",
            "-of",
            "json",
            video_path,
        ]
        result = subprocess.run(cmd, capture_output=True, text=True)
        data = json.loads(result.stdout)
        fps_str = data["streams"][0]["r_frame_rate"]
        # Parse fraction like "30000/1001" or "25/1"
        if "/" in fps_str:
            num, den = fps_str.split("/")
            fps = float(num) / float(den)
        else:
            fps = float(fps_str)
        return fps
    except Exception as e:
        print(f"Warning: Could not detect FPS, defaulting to 25: {e}")
        return 25.0


def seconds_to_timecode(seconds: float, fps: float = 25.0) -> str:
    """Convert seconds to HH:MM:SS:FF format for Premiere Pro."""
    hours = int(seconds // 3600)
    minutes = int((seconds % 3600) // 60)
    secs = int(seconds % 60)
    frames = int((seconds - int(seconds)) * fps)
    return f"{hours:02d}:{minutes:02d}:{secs:02d}:{frames:02d}"


def segments_to_pr_text(segments: list[dict], fps: float = 25.0) -> str:
    """
    Convert segments to Premiere Pro Text format (.txt).
    Format: HH:MM:SS:FF - HH:MM:SS:FF
    """
    lines = []
    for seg in segments:
        start_tc = seconds_to_timecode(seg["start"], fps)
        end_tc = seconds_to_timecode(seg["end"], fps)
        lines.append(f"{start_tc} - {end_tc}")
        lines.append(seg["text"].strip())
        lines.append("")  # Blank line between entries
    return "\n".join(lines)


def segments_to_pr_srt(segments: list[dict]) -> str:
    """
    Convert segments to frame-accurate SRT format.
    Same as standard SRT but with precise timing.
    """
    lines = []
    for i, seg in enumerate(segments, start=1):
        start = seconds_to_srt_time(seg["start"])
        end = seconds_to_srt_time(seg["end"])
        text = seg["text"].strip()
        lines.append(f"{i}")
        lines.append(f"{start} --> {end}")
        lines.append(text)
        lines.append("")  # Blank line
    return "\n".join(lines)


# ─────────────────────────────────────────────
# SINGLE VIDEO PIPELINE
# ─────────────────────────────────────────────

# Supported video extensions
VIDEO_EXTENSIONS = {
    ".mp4",
    ".mov",
    ".avi",
    ".mkv",
    ".webm",
    ".flv",
    ".m4v",
    ".ts",
    ".wmv",
}


def process_video(
    video_path: str,
    output_dir: str,
    word_level: bool = False,
    words_per_line: int = 2,
    output_format: str = "srt",
) -> str | None:
    """
    Full pipeline for a single video:
    video β†’ audio β†’ transcription β†’ caption file

    Returns the path to the generated file, or None on failure.
    """
    video_name = os.path.splitext(os.path.basename(video_path))[0]

    # Determine output filename based on format
    if output_format == "pr-text":
        output_filename = f"{video_name}.txt"
    else:
        output_filename = f"{video_name}.srt"

    output_path = os.path.join(output_dir, output_filename)

    with tempfile.TemporaryDirectory() as tmp:
        # Step 1 β€” extract audio
        print("  Extracting audio...")
        try:
            audio_path = extract_audio(video_path, tmp)
        except Exception as e:
            print(f"  Audio extraction failed: {e}")
            return None

        # Step 2 β€” transcribe
        if word_level:
            print("  Transcribing with word-level timestamps...")
            try:
                segments = transcribe_word_level(
                    audio_path, words_per_line=words_per_line
                )
            except Exception as e:
                print(f"  Word-level transcription failed: {e}")
                return None
        else:
            print("  Transcribing... (may take a while on CPU)")
            try:
                segments = transcribe(audio_path)
            except Exception as e:
                print(f"  Transcription failed: {e}")
                return None

        if not segments:
            print("No speech detected - skipping.")
            return None

        # Step 3 β€” detect FPS for Premiere Pro formats
        fps = 25.0
        if output_format in ["pr-text", "pr-srt"]:
            print("  Detecting video FPS...")
            fps = get_video_fps(video_path)
            print(f"     FPS: {fps}")

        # Step 4 β€” generate output based on format
        print(f"  Generating caption file ({output_format})...")

        if output_format == "pr-text":
            # Premiere Pro Text format (.txt)
            content = segments_to_pr_text(segments, fps)
        elif output_format == "pr-srt":
            # Premiere Pro optimized SRT (frame-accurate)
            content = segments_to_pr_srt(segments)
        else:
            # Standard SRT
            content = segments_to_srt(segments)

        with open(output_path, "w", encoding="utf-8") as f:
            f.write(content)

        print(f"  Done! {len(segments)} segments -> {output_path}")
        return output_path


# ─────────────────────────────────────────────
# BATCH RUNNER
# ─────────────────────────────────────────────


def collect_videos(inputs: list[str]) -> list[str]:
    """
    Given a list of paths (files and/or folders), return all video files found.
    Folders are scanned non-recursively by default.
    """
    videos = []

    for path in inputs:
        path = os.path.abspath(path)

        if os.path.isfile(path):
            ext = os.path.splitext(path)[1].lower()
            if ext in VIDEO_EXTENSIONS:
                videos.append(path)
            else:
                print(f"Skipping '{path}' β€” not a supported video format.")

        elif os.path.isdir(path):
            found = [
                os.path.join(path, f)
                for f in sorted(os.listdir(path))
                if os.path.splitext(f)[1].lower() in VIDEO_EXTENSIONS
            ]
            if not found:
                print(f"No videos found in folder: {path}")
            videos.extend(found)

        else:
            print(f"Path not found: {path}")

    return videos


def run_batch(
    videos: list[str],
    output_dir: str,
    word_level: bool = False,
    words_per_line: int = 2,
    output_format: str = "srt",
):
    """Process a list of video files and write caption files to output_dir."""

    total = len(videos)
    succeeded = []
    failed = []

    # Load model once before the loop β€” not per video
    print("─" * 60)
    load_model()
    print("─" * 60)

    format_name = {
        "srt": "Standard SRT",
        "pr-srt": "Premiere Pro SRT",
        "pr-text": "Premiere Pro Text",
    }.get(output_format, "SRT")

    ext = ".txt" if output_format == "pr-text" else ".srt"
    print(f"Starting batch: {total} video(s) β†’ {format_name} ({ext})")
    print(f"Output directory: {output_dir}\n")

    batch_start = time.time()

    for i, video_path in enumerate(videos, start=1):
        print(f"[{i}/{total}] {os.path.basename(video_path)}")
        video_start = time.time()

        result = process_video(
            video_path, output_dir, word_level, words_per_line, output_format
        )

        elapsed = time.time() - video_start
        print(f"  ⏱  Took {elapsed:.1f}s\n")

        if result:
            succeeded.append(video_path)
        else:
            failed.append(video_path)

    # ── Summary ──────────────────────────────
    total_time = time.time() - batch_start
    minutes, seconds = divmod(int(total_time), 60)

    print("─" * 60)
    print(f"Batch complete in {minutes}m {seconds}s")
    print(f"  Succeeded : {len(succeeded)}/{total}")
    print(f"  Failed    : {len(failed)}/{total}")

    if failed:
        print("\nFailed videos:")
        for f in failed:
            print(f"  - {f}")

    print("─" * 60)


# ─────────────────────────────────────────────
# CLI ENTRY POINT
# ─────────────────────────────────────────────


def main():
    parser = argparse.ArgumentParser(
        prog="batch.py",
        description=(
            "HinglishCaps Batch CLI β€” generate SRT subtitle files for multiple videos at once.\n"
            "Powered by Oriserve/Whisper-Hindi2Hinglish-Apex.\n\n"
            "Examples:\n"
            "  # Single video\n"
            "  python batch.py video.mp4\n\n"
            "  # Multiple videos\n"
            "  python batch.py clip1.mp4 clip2.mov clip3.mkv\n\n"
            "  # Entire folder of videos\n"
            "  python batch.py /path/to/videos/\n\n"
            "  # Mix of files and folders\n"
            "  python batch.py intro.mp4 /path/to/more/videos/\n\n"
            "  # Custom output folder\n"
            "  python batch.py /videos/ --output /subtitles/\n"
        ),
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )

    parser.add_argument(
        "inputs",
        nargs="+",
        metavar="VIDEO_OR_FOLDER",
        help=(
            "One or more video files or folders containing videos. "
            f"Supported formats: {', '.join(sorted(VIDEO_EXTENSIONS))}"
        ),
    )

    parser.add_argument(
        "--output",
        "-o",
        metavar="OUTPUT_DIR",
        default=None,
        help=(
            "Folder where SRT files will be saved. "
            "Defaults to same folder as each video. "
            "If a single folder input is given, defaults to that same folder."
        ),
    )

    parser.add_argument(
        "--word-level",
        "-w",
        action="store_true",
        help="Enable word-level timestamps (karaoke-style captions, 2-3 words per line)",
    )

    parser.add_argument(
        "--words-per-line",
        "-wp",
        type=int,
        default=2,
        metavar="N",
        help="Number of words per caption line when using --word-level (default: 2, max: 5)",
    )

    parser.add_argument(
        "--format",
        "-f",
        choices=["srt", "pr-srt", "pr-text"],
        default="srt",
        help=(
            "Output format: srt (standard), pr-srt (Premiere Pro SRT), "
            "pr-text (Premiere Pro Text). Default: srt"
        ),
    )

    args = parser.parse_args()

    # Collect all video files
    videos = collect_videos(args.inputs)

    if not videos:
        print("No valid video files found. Nothing to do.")
        sys.exit(1)

    print(f"\nFound {len(videos)} video(s) to process:")
    for v in videos:
        print(f"   {v}")
    print()

    # Resolve output directory
    if args.output:
        output_dir = os.path.abspath(args.output)
        os.makedirs(output_dir, exist_ok=True)
    else:
        # If all videos are in the same folder, put SRTs there too
        # Otherwise use current working directory
        dirs = {os.path.dirname(v) for v in videos}
        if len(dirs) == 1:
            output_dir = dirs.pop()
        else:
            output_dir = os.getcwd()

    print(f"Output directory: {output_dir}\n")

    if args.word_level:
        print(f"Word-level mode: {args.words_per_line} words per line")

    format_name = {
        "srt": "Standard SRT",
        "pr-srt": "Premiere Pro SRT",
        "pr-text": "Premiere Pro Text",
    }.get(args.format, "SRT")
    print(f"Output format: {format_name}\n")

    run_batch(videos, output_dir, args.word_level, args.words_per_line, args.format)


if __name__ == "__main__":
    main()