File size: 8,942 Bytes
4f81a3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Segment full meeting audio into sentence-level clips and write Parquet shards.

For each meeting in metadata.csv, this script:
  1. Parses the SRT file to get (start, end, text) segments.
  2. Uses ffmpeg to extract each segment from the opus file (stream copy, no re-encoding).
  3. Batches segments and writes Parquet shards with embedded audio bytes.

Usage:
    python -m scripts.segment_audio [--workers N] [--shard-size N] [--out-dir DIR]
"""

import argparse
import collections
import csv
import itertools
import re
import subprocess
from concurrent.futures import FIRST_COMPLETED, ProcessPoolExecutor, wait
from dataclasses import dataclass
from pathlib import Path

from rich.console import Console
from rich.progress import (
    BarColumn,
    MofNCompleteColumn,
    Progress,
    SpinnerColumn,
    TextColumn,
    TimeElapsedColumn,
    TimeRemainingColumn,
)

REPO_ROOT = Path(__file__).resolve().parent.parent
DEFAULT_SHARD_SIZE = 5000
DEFAULT_WORKERS = 4

console = Console()


@dataclass
class SrtSegment:
    index: int
    start_seconds: float
    end_seconds: float
    text: str


def _ts_to_seconds(ts: str) -> float:
    """Convert SRT timestamp (HH:MM:SS,mmm) to seconds."""
    h, m, rest = ts.split(":")
    s, ms = rest.split(",")
    return int(h) * 3600 + int(m) * 60 + int(s) + int(ms) / 1000


def parse_srt(srt_path: Path) -> list[SrtSegment]:
    """Parse an SRT file into a list of timed segments."""
    try:
        content = srt_path.read_text(encoding="utf-8")
    except FileNotFoundError:
        return []

    segments: list[SrtSegment] = []
    blocks = re.split(r"\n\s*\n", content.strip())

    for block in blocks:
        lines = block.strip().split("\n")
        if len(lines) < 3:
            continue

        try:
            idx = int(lines[0].strip())
        except ValueError:
            continue

        ts_match = re.match(
            r"(\d{2}:\d{2}:\d{2},\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2},\d{3})",
            lines[1].strip(),
        )
        if not ts_match:
            continue

        start = _ts_to_seconds(ts_match.group(1))
        end = _ts_to_seconds(ts_match.group(2))
        text = " ".join(l.strip() for l in lines[2:] if l.strip())

        if not text or end <= start:
            continue

        segments.append(SrtSegment(index=idx, start_seconds=start, end_seconds=end, text=text))

    return segments


def extract_segment_audio(opus_path: Path, start: float, duration: float) -> bytes | None:
    """Extract a segment from an opus file using ffmpeg stream copy.

    Returns the raw OGG/Opus bytes, or None on failure.
    """
    cmd = [
        "ffmpeg",
        "-v", "error",
        "-ss", f"{start:.3f}",
        "-i", str(opus_path),
        "-t", f"{duration:.3f}",
        "-c", "copy",
        "-f", "ogg",
        "pipe:1",
    ]
    try:
        result = subprocess.run(cmd, capture_output=True, timeout=30)
        if result.returncode != 0:
            return None
        if len(result.stdout) < 100:
            return None
        return result.stdout
    except (subprocess.TimeoutExpired, OSError):
        return None


def process_meeting(row: dict) -> list[dict]:
    """Process a single meeting: parse SRT, extract all audio segments."""
    video_id = row["id"]
    opus_path = REPO_ROOT / row["audio"]
    srt_path = REPO_ROOT / row["subtitles"]

    if not opus_path.exists():
        return []

    segments = parse_srt(srt_path)
    if not segments:
        return []

    results = []
    for seg in segments:
        duration = seg.end_seconds - seg.start_seconds
        audio_bytes = extract_segment_audio(opus_path, seg.start_seconds, duration)
        if audio_bytes is None:
            continue

        results.append({
            "video_id": video_id,
            "segment_id": seg.index,
            "audio": {"bytes": audio_bytes, "path": f"{video_id}_{seg.index:05d}.opus"},
            "text": seg.text,
            "start_time": round(seg.start_seconds, 3),
            "end_time": round(seg.end_seconds, 3),
            "duration": round(duration, 3),
        })

    return results


def write_shard(segments: list[dict], shard_idx: int, out_dir: Path) -> Path:
    """Write a list of segment dicts as a Parquet shard with Audio feature."""
    from datasets import Audio, Dataset, Features, Value

    features = Features({
        "video_id": Value("string"),
        "segment_id": Value("int32"),
        "audio": Audio(),
        "text": Value("string"),
        "start_time": Value("float64"),
        "end_time": Value("float64"),
        "duration": Value("float64"),
    })

    ds = Dataset.from_dict(
        {k: [s[k] for s in segments] for k in segments[0]},
        features=features,
    )

    path = out_dir / f"train-{shard_idx:05d}.parquet"
    ds.to_parquet(path)
    return path


def _flush_buffer(buffer: collections.deque, shard_size: int, shard_idx: int,
                   out_dir: Path, *, force: bool = False) -> int:
    """Write complete shards from buffer. Returns updated shard_idx."""
    while len(buffer) >= shard_size or (force and buffer):
        n = min(shard_size, len(buffer))
        batch = [buffer.popleft() for _ in range(n)]
        shard_path = write_shard(batch, shard_idx, out_dir)
        console.print(
            f"  Wrote shard {shard_idx} ({n} segments) -> {shard_path.name}"
        )
        del batch
        shard_idx += 1
    return shard_idx


def main() -> None:
    parser = argparse.ArgumentParser(description="Segment audio and build Parquet shards")
    parser.add_argument("--workers", type=int, default=DEFAULT_WORKERS,
                        help="Number of parallel workers (default: %(default)s)")
    parser.add_argument("--shard-size", type=int, default=DEFAULT_SHARD_SIZE,
                        help="Segments per Parquet shard (default: %(default)s)")
    parser.add_argument("--out-dir", type=Path, default=REPO_ROOT / "segmented",
                        help="Output directory for Parquet shards")
    args = parser.parse_args()

    args.out_dir.mkdir(parents=True, exist_ok=True)

    src = REPO_ROOT / "metadata.csv"
    with open(src, encoding="utf-8", newline="") as f:
        reader = csv.DictReader(f)
        rows = list(reader)

    console.print(f"Processing {len(rows)} meetings with {args.workers} workers")
    console.print(f"Shard size: {args.shard_size} segments")
    console.print(f"Output: {args.out_dir}")

    buffer: collections.deque[dict] = collections.deque()
    shard_idx = 0
    total_segments = 0
    errors = 0
    meetings_done = 0
    max_in_flight = args.workers * 2

    progress = Progress(
        SpinnerColumn(),
        TextColumn("[progress.description]{task.description}"),
        BarColumn(),
        MofNCompleteColumn(),
        TimeElapsedColumn(),
        TimeRemainingColumn(),
        console=console,
    )

    with progress:
        task = progress.add_task("Meetings processed", total=len(rows))
        rows_iter = iter(rows)

        with ProcessPoolExecutor(max_workers=args.workers) as pool:
            active: dict = {}

            # Seed the pool with an initial batch of work
            for row in itertools.islice(rows_iter, max_in_flight):
                f = pool.submit(process_meeting, row)
                active[f] = row["id"]

            while active:
                done, _ = wait(active, return_when=FIRST_COMPLETED)

                for future in done:
                    video_id = active.pop(future)
                    try:
                        segments = future.result()
                        buffer.extend(segments)
                        total_segments += len(segments)
                        del segments
                    except Exception as e:
                        errors += 1
                        console.print(f"[red]Error processing {video_id}: {e}[/red]")

                    # Release the Future's internal result reference
                    future._result = None

                    meetings_done += 1
                    progress.advance(task)

                    # Submit next meeting to keep the pool fed
                    row = next(rows_iter, None)
                    if row is not None:
                        f = pool.submit(process_meeting, row)
                        active[f] = row["id"]

                # Flush complete shards after processing each batch of done futures
                shard_idx = _flush_buffer(
                    buffer, args.shard_size, shard_idx, args.out_dir
                )

    # Flush remaining segments
    shard_idx = _flush_buffer(
        buffer, args.shard_size, shard_idx, args.out_dir, force=True
    )

    console.print(f"\n[bold green]Done![/bold green]")
    console.print(f"  Total segments: {total_segments}")
    console.print(f"  Total shards:   {shard_idx}")
    console.print(f"  Errors:         {errors}")


if __name__ == "__main__":
    main()