My Collab Lab

mycollablab.org

Google Speech to Text API

April 16, 2017 Cloud PHP

I was working on a POC around voicemail transcription from unity and I spent longer than needed trying to get this to work the way goods docs say to with all the libraries etc. Then I spent another good chunk of time on github with the same success, but finally got it figured out over the normal way that I have been doing regulr API calls and it works great.

Transcibe Function


<?php

function sendToGoogle($file_name) {

    $google['auth_token']  = "<Google API Key>";
    $file_name = fread(fopen($file_name, "r"), filesize($file_name));

    $api_data = array("config"=>array("sampleRate"=>8000
                     ,"encoding"=>"MULAW"
                    ),
              "audio"=>array(
                            "content"=>base64_encode($file_name)
                    )
              );

    $options = array(
        'http' => array(
            'header'  => "Content-type: application/json\r\n",
            'method'  => 'POST',
            'content' => json_encode($api_data),
            )
    );


    $context  = stream_context_create($options);
    $result = json_decode(file_get_contents('https://speech.googleapis.com/v1beta1/speech:syncrecognize?key='
                            .$google['auth_token'], false, $context));

    return $result;
}

$audio_file = "./file.wav";

//Get google transcription
if ( $transcript = sendToGoogle($audio_file) ) {
//var_dump($transcription);
    $transcript = $transcript->results[0]->alternatives[0];
    $transcript = "\n\n**Transcript:** (".ceil( $transcript->confidence * 100 )."%)\n".$transcript->transcript;
} else {
    $transcript = "\n\n**Transcript Unavailble**\n";
}

?>

XML Phone Directory

Display Ascii Update


Jon Snipes
LinkedIn

My career progressed from head butcher to Collaboration CCIE. There isn’t much technically that carries over between professions, but 8 years of direct customer service experience and squeezing margin out of processes provided a solid base and direction for the rest of my career. My focus today is deep into collaboration messaging, voice and video with the expertise to develop processes and programmatic solutions to complex business problems.

Principal Architect at Cloverhound
CCIE Collaboration 51786
Cisco Webex Expert
2018-2021 Cisco Champion

Categories
  • Cloud (4)
  • Contact Center (1)
  • Development (1)
  • PHP (10)
  • Python (3)
  • Uncategorized (1)
  • Video (2)
  • Voice (12)
  • Voicemail (5)
  • Webex (7)
Recent Posts
  • Azure OAuth2.0 User Authentication December 4, 2021
  • Pass-through Gateway Routing and Dial Peer Groups December 4, 2021
  • Get Started Coding with Python: System Setup and Running Scripts December 4, 2021
  • Exporting CUCM to an Offline Local DB with Python December 4, 2021
  • Connecting to UCCX INFORMIX DB with Python on Linux December 4, 2021
Proudly powered by WordPress | Theme: Doo by ThemeVS.