PHP code example of aisdk / elevenlabs

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

    

aisdk / elevenlabs example snippets


use AiSdk\ElevenLabs;

ElevenLabs::create([
    'apiKey' => 'xi-...',
    'baseUrl' => 'https://api.elevenlabs.io/v1',
    'headers' => ['X-Custom-Header' => 'value'],
]);

$elevenLabs = ElevenLabs::create(['apiKey' => 'xi-...']);

$music = $elevenLabs->music()->compose('A warm acoustic intro.');
$sameResource = ElevenLabs::music();

use AiSdk\ElevenLabs;
use AiSdk\Generate;

$result = Generate::speech('Welcome to the show.')
    ->model(ElevenLabs::model('eleven_flash_v2_5'))
    ->voice('JBFqnCBsd6RMkjVDRZzb')
    ->format('mp3')
    ->providerOptions('elevenlabs', [
        'voice_settings' => [
            'stability' => 0.5,
            'similarity_boost' => 0.75,
        ],
    ])
    ->run();

$result->output->save(__DIR__.'/speech.mp3');

use AiSdk\Content;
use AiSdk\ElevenLabs;
use AiSdk\Generate;

$result = Generate::transcription(Content::audio(__DIR__.'/meeting.mp3'))
    ->model(ElevenLabs::model('scribe_v2'))
    ->providerOptions('elevenlabs', [
        'language_code' => 'en',
        'diarize' => true,
    ])
    ->run();

echo $result->output->text;

use AiSdk\ElevenLabs;
use AiSdk\Live;
use AiSdk\Live\TranscriptCompleted;
use AiSdk\Live\TranscriptUpdate;
use AiSdk\Transport;

$session = Live::transcribe()
    ->model(ElevenLabs::model('scribe_v2_realtime'))
    ->language('en')
    ->audioFormat('pcm_16000')
    ->providerOptions('elevenlabs', [
        'commit_strategy' => 'manual',
        '
}

$secret = Live::transcribe()
    ->model(ElevenLabs::model('scribe_v2_realtime'))
    ->clientSecret();

return ['token' => $secret->value, 'expires_at' => $secret->expiresAt];

$result = ElevenLabs::voiceChanger()->convert(
    voiceId: 'JBFqnCBsd6RMkjVDRZzb',
    audio: Content::audio(__DIR__.'/performance.wav'),
    options: [
        'model_id' => 'eleven_multilingual_sts_v2',
        'remove_background_noise' => true,
        'output_format' => 'wav_44100',
    ],
);

$result->output->save(__DIR__.'/changed.wav');

$result = ElevenLabs::voiceIsolator()->isolate(
    Content::audio(__DIR__.'/noisy-interview.mp3'),
);

$result->output->save(__DIR__.'/clean.mp3');

$music = ElevenLabs::music()->compose('Warm acoustic guitar intro with gentle rain.', [
    'model_id' => 'music_v1',
    'music_length_ms' => 30_000,
]);

$effect = ElevenLabs::soundEffects()->generate('A wooden door slowly creaking open.');

$plan = ElevenLabs::music()->plan('A tense cinematic cue with a quiet ending.', [
    'model_id' => 'music_v2',
    'music_length_ms' => 30_000,
]);

$track = ElevenLabs::music()->composeFromPlan($plan, [
    'model_id' => 'music_v2',
    'output_format' => 'mp3_48000_192',
]);

$detailed = ElevenLabs::music()->composeDetailed('A bright synth-pop theme.', [
    'model_id' => 'music_v2',
    'with_timestamps' => true,
]);

echo $detailed->songId;
$detailed->output->save(__DIR__.'/theme.mp3');

$soundtrack = ElevenLabs::music()->videoToMusic([
    Content::video(__DIR__.'/opening.mp4'),
    Content::video(__DIR__.'/closing.mp4'),
], [
    'description' => 'Cinematic, restrained, then uplifting.',
    'tags' => ['cinematic', 'orchestral'],
    'model_id' => 'music_v2',
]);

$stems = ElevenLabs::music()->separateStems(
    Content::audio(__DIR__.'/song.mp3'),
    ['stem_variation_id' => 'six_stems_v1'],
);

$stems->save(__DIR__.'/stems.zip');

$dialogue = ElevenLabs::dialogue()->create([
    ['text' => '[giggling] Knock knock.', 'voice_id' => 'JBFqnCBsd6RMkjVDRZzb'],
    ['text' => '[curious] Who is there?', 'voice_id' => 'Aw4FAjKCGjjNkVhN1Xmq'],
], ['model_id' => 'eleven_v3']);

$dialogue = ElevenLabs::dialogue()->withTimestamps([
    ['text' => 'Ready?', 'voice_id' => 'JBFqnCBsd6RMkjVDRZzb'],
    ['text' => 'Ready.', 'voice_id' => 'Aw4FAjKCGjjNkVhN1Xmq'],
]);

$dialogue->output->save(__DIR__.'/dialogue.mp3');
foreach ($dialogue->voiceSegments as $segment) {
    echo "{$segment->voiceId}: {$segment->start} - {$segment->end}\n";
}

$design = ElevenLabs::voiceDesign()->design(
    'A warm, expressive documentary narrator with measured pacing.',
    [
        'model_id' => 'eleven_ttv_v3',
        'auto_generate_text' => true,
    ],
);

$preview = $design->previews[0];
$preview->audio->save(__DIR__.'/voice-preview.mp3');

$voice = ElevenLabs::voiceDesign()->create(
    generatedVoiceId: $preview->generatedVoiceId,
    name: 'Documentary narrator',
    description: 'A warm, expressive documentary narrator with measured pacing.',
);

echo $voice->id;

$job = ElevenLabs::dubbing()->create(
    Content::video(__DIR__.'/interview.mp4'),
    targetLanguage: 'es',
    options: ['source_lang' => 'en'],
);

$status = ElevenLabs::dubbing()->status($job->id);

if ($status->status === 'dubbed') {
    ElevenLabs::dubbing()
        ->output($job->id, 'es')
        ->save(__DIR__.'/interview-es.mp4');

    $subtitles = ElevenLabs::dubbing()->transcript($job->id, 'es', 'srt');
    file_put_contents(__DIR__.'/interview-es.srt', $subtitles->content ?? '');
}

$alignment = ElevenLabs::forcedAlignment()->create(
    Content::audio(__DIR__.'/narration.wav'),
    'The exact transcript spoken in the recording.',
);

foreach ($alignment->words as $word) {
    echo "{$word->text}: {$word->start} - {$word->end}\n";
}