ナビゲーションにスキップ

サーバーサイドストリーミング

このガイドでは、ElevenLabsを使ってサーバーサイドでオーディオをリアルタイムに文字起こしする方法を説明します。

ハウツーガイド · スピーチtoテキスト クイックスタートを完了していることを前提としています。

概要

ElevenLabsのリアルタイム・スピーチtoテキストAPIでは、Scribe Realtime v2モデルを使用し、超低遅延でオーディオストリームをリアルタイムに文字起こしできます。音声アシスタント、文字起こしサービス、ライブ音声認識を必要とするあらゆるアプリケーションで、WebSocketベースのAPIが発話中は部分文字起こしを、音声セグメントの完了時には確定済み文字起こしを提供します。

Scribe v3 Liveはサーバーサイドに実装でき、URL、ファイル、または独自のオーディオストリームからオーディオをリアルタイムに文字起こしできます。

サーバーサイドの実装は、クライアントサイドと次の点で異なります。

  • 使い捨てトークンではなく、ElevenLabsのAPIキーを使用します。
  • オーディオを手動でチャンクに分割せず、URLから直接ストリーミングできます。

マイクから直接オーディオをストリーミングする方法については、クライアントサイドストリーミングガイドを参照してください。

クイックスタート

このガイドは、APIキーとSDKの設定が完了していることを前提としています。まだの場合は、 先にクイックスタートを完了してください。

1

SDKを設定する

SDKでは、オーディオをリアルタイムに文字起こしする方法を2つ提供しています。URLからストリーミングする方法と、ファイルまたは独自のオーディオストリームを手動でチャンクに分割する方法です。

APIが対応するパラメーターとオプションの全一覧については、APIリファレンスを参照してください。

この例では、公式SDKを使ってURLからオーディオファイルをストリーミングする方法を示します。

URLからストリーミングする場合は、ffmpeg ツールが必要です。インストール手順については、公式サイトを参照してください。

使用する言語に応じて、example.py または example.mts という名前の新しいファイルを作成し、次のコードを追加します。

from dotenv import load_dotenv
import os
import asyncio
from elevenlabs import ElevenLabs, RealtimeEvents, RealtimeUrlOptions
load_dotenv()
async def main():
elevenlabs = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
# Create an event to signal when to stop
stop_event = asyncio.Event()
# Connect to a streaming audio URL
connection = await elevenlabs.speech_to_text.realtime.connect(RealtimeUrlOptions(
model_id="scribe_v3_live",
url="https://npr-ice.streamguys1.com/live.mp3",
include_timestamps=True,
))
# Set up event handlers
def on_session_started(data):
print(f"Session started: {data}")
def on_partial_transcript(data):
print(f"Partial: {data.get('text', '')}")
def on_committed_transcript(data):
print(f"Committed: {data.get('text', '')}")
# Committed transcripts with word-level timestamps. Only received when include_timestamps is set to True.
def on_committed_transcript_with_timestamps(data):
print(f"Committed with timestamps: {data.get('words', '')}")
# Errors - will catch all errors, both server and websocket specific errors
def on_error(error):
print(f"Error: {error}")
# Signal to stop on error
stop_event.set()
def on_close():
print("Connection closed")
# Register event handlers
connection.on(RealtimeEvents.SESSION_STARTED, on_session_started)
connection.on(RealtimeEvents.PARTIAL_TRANSCRIPT, on_partial_transcript)
connection.on(RealtimeEvents.COMMITTED_TRANSCRIPT, on_committed_transcript)
connection.on(RealtimeEvents.COMMITTED_TRANSCRIPT_WITH_TIMESTAMPS, on_committed_transcript_with_timestamps)
connection.on(RealtimeEvents.ERROR, on_error)
connection.on(RealtimeEvents.CLOSE, on_close)
print("Transcribing audio stream... (Press Ctrl+C to stop)")
try:
# Wait until error occurs or connection closes
await stop_event.wait()
except KeyboardInterrupt:
print("\nStopping transcription...")
finally:
await connection.close()
if __name__ == "__main__":
asyncio.run(main())
2

コードを実行する

python example.py

オーディオファイルの文字起こしが、部分文字起こしと確定済み文字起こしとしてコンソールに表示されます。

次のステップ