PHP code example of codewithkyrian / whisper.php

1. Go to this page and download the library: Download codewithkyrian/whisper.php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codewithkyrian / whisper.php example snippets


// Initialize context with a model
$contextParams = WhisperContextParameters::default();
$ctx = new WhisperContext("path/to/model.bin", $contextParams);

// Create state and set parameters
$state = $ctx->createState();
$fullParams = WhisperFullParams::default()
    ->withNThreads(4)
    ...
    ->withLanguage('en');

// Transcribe audio
$state->full($pcm, $fullParams);

// Process segments
$numSegments = $state->nSegments();
for ($i = 0; $i < $numSegments; $i++) {
    $segment = $state->getSegmentText($i);
    $startTimestamp = $state->getSegmentStartTime($i);
    $endTimestamp = $state->getSegmentEndTime($i);

    printf(
        "[%s - %s]: %s\n",
        toTimestamp($startTimestamp),
        toTimestamp($endTimestamp),
        $segment
    );
}

// Automatically download and load a model if it's already downloaded
$modelPath = ModelLoader::loadModel('tiny.en', __DIR__.'/models');

// Convenient audio reading function
$pcm = readAudio($audioPath);

// Simple transcription
$whisper = Whisper::fromPretrained('tiny.en', baseDir: __DIR__.'/models');
$audio = readAudio(__DIR__.'/sounds/sample.wav');
$segments = $whisper->transcribe($audio, 4);

// Accessing segment data
foreach ($segments as $segment) {
    echo toTimestamp($segment->startTimestamp) . ': ' . $segment->text . "\n";
}

// Advanced usage with custom parameters
$params = WhisperFullParams::default()
    ->withNThreads(4)
    ->withLanguage('en');

$whisper = Whisper::fromPretrained(
    'tiny.en',           // Model name
    baseDir: __DIR__.'/models',  // Model storage directory
    params: $params      // Custom transcription parameters
);

$fullParams = WhisperFullParams::default()
    ->withLanguage('en');  // Specify two-letter language code eg. 'en' (English), 'de' (German), 'es' (Spanish)

$fullParams = WhisperFullParams::default()
    ->withNThreads(8);  // Default is 4

$fullParams = WhisperFullParams::default()
    ->withSegmentCallback(function (SegmentData $data) {
        printf("[%s - %s]: %s\n", 
            toTimestamp($data->startTimestamp), 
            toTimestamp($data->endTimestamp), 
            $data->text
        );
    })

$fullParams = $fullParams
        ->withProgressCallback(function (int $progress) {
            printf("Transcribing: %d%%\n", $progress);
        });

outputTxt($segments, 'transcription.txt'); // Ideal for quick reading, documentation, or further text processing
outputVtt($segments, 'subtitles.vtt'); // Primarily used for web-based video subtitles, compatible with HTML5 video players
outputSrt($segments, 'subtitles.srt'); // Widely supported by media players, video editing software, and streaming platforms
outputCsv($segments, 'transcription.csv'); // Perfect for data analysis and spreadsheet applications

// Log to a file
Whisper::setLogger(new WhisperLogger('whisper.log'));

// Log to standard output
Whisper::setLogger(new WhisperLogger(STDOUT));

$monologLogger = new Logger('whisper');
$monologLogger->pushHandler(new StreamHandler('whisper.log', Logger::DEBUG));
$monologLogger->pushHandler(new FirePHPHandler());

// Set the Monolog logger
Whisper::setLogger($monologLogger);

// Using Laravel's Log facade
Whisper::setLogger(Log::getLogger());

// Or directly with Laravel's logger
Whisper::setLogger(app('log'));
bash
composer 
ini
extension = ffi