1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Amazon TranscribeとAgora.ioでリアルタイム文字起こしを試してみた

Last updated at Posted at 2022-12-14

ビデオ通話や、ライブ配信で文字起こしをリアルタイムに行う要件が増えてきています。
この記事ではAmazon Transcribeを用いて、リアルタイムに文字起こしをする方法について解説します。

Amazon Transcribe

AWSでは2パターンの文字起こしを提供しています。S3に保存されたファイルからjobで生成するパターンと、リアルタイムにストリームを流し込んで生成するパターンがあります。
今回の記事では後者を利用します。

Amazon Transcribe ストリーミング Servic

公式のドキュメントはこちらにあります。

双方向 HTTP/2 を起動するか WebSocket ストリーミング。オーディオが Amazon Transcribe にストリーミングされると、テキスト文字起こしの結果がアプリケーションにストリーミングされます。

(公式ドキュメントから引用)

WebSocket で Amazon Transcribe ストリーミングを使用する

WebSocketを用いた実装の解説がこちらに掲載されています。
この解説の中に実装例のリンクが貼られていました。

Agora.ioと連携してみる

さきほどのサンプルコードでは、ブラウザのAPIを利用して音声をキャプチャしています。

main.js
    window.navigator.mediaDevices.getUserMedia({
            video: false,
            audio: true
        })
        // ...then we convert the mic stream to binary event stream messages when the promise resolves 
        .then(streamAudioToWebSocket) 
        .catch(function (error) {
            showError('There was an error streaming your audio to Amazon Transcribe. Please try again.');
            toggleStartStop();
        });

この部分をAgoraのAPIに置き換えてビデオ通話+文字起こしを実現します。

main.js
const AgoraRTC = require('agora-rtc-sdk-ng');
const agoraEngine = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });
//中略
    await agoraEngine.join(YOUR APP ID, YOUR CHANNEL ID, null, null);
    let localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack();
    await agoraEngine.publish(localAudioTrack);
    let mediaStream = new MediaStream();
    mediaStream.addTrack(localAudioTrack.getMediaStreamTrack());
    streamAudioToWebSocket(mediaStream);
    // window.navigator.mediaDevices.getUserMedia({
    //         video: false,
    //         audio: true
    //     })
    //     // ...then we convert the mic stream to binary event stream messages when the promise resolves 
    //     .then(streamAudioToWebSocket) 
    //     .catch(function (error) {
    //         showError('There was an error streaming your audio to Amazon Transcribe. Please try again.');
    //         toggleStartStop();
    //     });

音声キャプチャをAgoraのAPIに差し替えただけの簡単な修正になります。
AgoraのAPIで取得した音声ストリームについてはgetMediaStreamTrack()でアクセス可能です。

この修正だけでビデオ通話中のリアルタイム文字起こしが完了です。
ライブ配信の場合ですと、取得した文字をリアルタイムメッセージ等で視聴者にブロードキャストする方法等が考えられます。

最後に

agora.ioに関するお問い合わせはこちらから
Agoraの詳細はこちら

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?