PHP code example of errogaht / transcribe-bundle

1. Go to this page and download the library: Download errogaht/transcribe-bundle 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/ */

    

errogaht / transcribe-bundle example snippets




return [
    // ...
    Errogaht\TranscribeBundle\TranscribeBundle::class => ['all' => true],
];

use Errogaht\TranscribeBundle\Contract\TranscriberInterface;

final class VoiceMessageHandler
{
    public function __construct(private TranscriberInterface $transcriber)
    {
    }

    public function __invoke(string $audioPath): string
    {
        return $this->transcriber->transcribe($audioPath)->text;
    }
}

use Errogaht\TranscribeBundle\Contract\TranscriberInterface;

final class MeetingImporter
{
    public function __construct(private TranscriberInterface $meeting)
    {
    }

    public function import(string $videoPath): void
    {
        $result = $this->meeting->transcribe($videoPath);

        foreach ($result->segments as $segment) {
            // $segment->speaker, ->start, ->end and ->text are provider-neutral.
        }
    }
}

use Errogaht\TranscribeBundle\Value\MediaInput;

$result = $transcriber->transcribe(
    MediaInput::fromBytes($upload, 'voice.ogg', 'audio/ogg'),
);

use Errogaht\TranscribeBundle\Value\TranscriptionOptions;

$result = $transcriber->transcribe($path, TranscriptionOptions::diarized('ru', 2));

use Errogaht\TranscribeBundle\Contract\StreamingTranscriberInterface;
use Errogaht\TranscribeBundle\Enum\StreamEventType;

final class LiveCaptionService
{
    public function __construct(private StreamingTranscriberInterface $microphone)
    {
    }

    public function run(iterable $pcmChunks): string
    {
        $session = $this->microphone->open();

        try {
            foreach ($pcmChunks as $chunk) {
                $session->send($chunk);
                $event = $session->receive();

                if ($event?->type === StreamEventType::Partial) {
                    // Publish $event->chunkText or $event->fullText to the UI.
                }
            }

            $session->finish();
            while (($event = $session->receive(10.0)) !== null) {
                if ($event->type === StreamEventType::Final) {
                    return $event->fullText ?? '';
                }
            }

            return '';
        } finally {
            $session->close();
        }
    }
}

$srt = $formatters->get('srt')->render($result);

use Errogaht\TranscribeBundle\Contract\TaskTranscriberInterface;

public function __construct(
    private TaskTranscriberInterface $podcastTaskTranscriber,
) {
}

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final class TranscribeRecordingHandler
{
    public function __construct(private TranscriberInterface $meeting)
    {
    }

    public function __invoke(TranscribeRecording $message): void
    {
        $result = $this->meeting->transcribe($message->path);
        // Persist the completed result and let Messenger own delivery retries.
    }
}

final class VoiceAgent
{
    public function __construct(
        private TranscriberInterface $fast,
        private SupportAgent $supportAgent,
    ) {
    }

    public function answer(string $voicePath): mixed
    {
        return $this->supportAgent->chat($this->fast->transcribe($voicePath)->text);
    }
}