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);
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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.