Skip to content

Commit 89d74c1

Browse files
examples: Add token-level-timestamps examples
1 parent ac9d1c1 commit 89d74c1

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

examples/high-level.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use Codewithkyrian\Whisper\ModelLoader;
65
use Codewithkyrian\Whisper\SegmentData;
76
use Codewithkyrian\Whisper\Whisper;
8-
use Codewithkyrian\Whisper\WhisperContext;
9-
use Codewithkyrian\Whisper\WhisperContextParameters;
107
use Codewithkyrian\Whisper\WhisperException;
118
use Codewithkyrian\Whisper\WhisperFullParams;
129

@@ -18,16 +15,19 @@
1815

1916
try {
2017
$fullParams = WhisperFullParams::default()
18+
->withSegmentCallback(function (SegmentData $data) {
19+
$start = toTimestamp($data->startTimestamp);
20+
$end = toTimestamp($data->endTimestamp);
21+
printf("[%s - %s]: %s\n", $start, $end, $data->text);
22+
})
2123
->withNThreads(4);
2224

23-
$whisper = Whisper::fromPretrained('tiny.en', baseDir: __DIR__.'/models');
25+
$whisper = Whisper::fromPretrained('tiny.en', __DIR__.'/models', $fullParams);
2426

2527
$audio = readAudio(__DIR__.'/sounds/jfk.wav');
2628

2729
$segments = $whisper->transcribe($audio, 4);
2830

29-
printf('Generated Segments: %d', count($segments));
30-
3131
// Create output files
3232
$transcriptionPath = __DIR__.'/outputs/transcription.srt';
3333
outputSrt($segments, $transcriptionPath);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Codewithkyrian\Whisper\SegmentData;
6+
use Codewithkyrian\Whisper\Whisper;
7+
use Codewithkyrian\Whisper\WhisperException;
8+
use Codewithkyrian\Whisper\WhisperFullParams;
9+
10+
use function Codewithkyrian\Whisper\readAudio;
11+
use function Codewithkyrian\Whisper\toTimestamp;
12+
13+
require_once __DIR__.'/../vendor/autoload.php';
14+
15+
try {
16+
$fullParams = WhisperFullParams::default()
17+
->withSegmentCallback(function (SegmentData $data) {
18+
$start = toTimestamp($data->startTimestamp);
19+
$end = toTimestamp($data->endTimestamp);
20+
printf("[%s - %s]: %s\n", $start, $end, $data->text);
21+
})
22+
->withTokenTimestamps()
23+
->withSplitOnWord(true)
24+
->withMaxLen(1)
25+
->withNThreads(4);
26+
27+
$whisper = Whisper::fromPretrained('tiny.en', __DIR__.'/models', $fullParams);
28+
29+
$audio = readAudio(__DIR__.'/sounds/jfk.wav');
30+
31+
$segments = $whisper->transcribe($audio, 4);
32+
33+
foreach ($segments as $segment) {
34+
printf(
35+
"[%s - %s]: %s\n",
36+
toTimestamp($segment->startTimestamp),
37+
toTimestamp($segment->endTimestamp),
38+
$segment->text
39+
);
40+
}
41+
} catch (WhisperException $e) {
42+
fprintf(STDERR, "Whisper error: %s\n", $e->getMessage());
43+
exit(1);
44+
} catch (Exception $e) {
45+
fprintf(STDERR, "Error: %s\n", $e->getMessage());
46+
exit(1);
47+
}

0 commit comments

Comments
 (0)