My Collab Lab

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
TwitterLinkedIn

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.

ROVE Senior Technical Consultant
CCIE Collaboration 51786
Cisco Spark Professional Ambassador
2018 Cisco Champion

Categories

  • Cloud (5)
  • DevNet (2)
  • PHP (10)
  • Python (3)
  • SQL (1)
  • UCCX (1)
  • Uncategorized (1)
  • UnityConnection (5)
  • Video (2)
  • Voice (11)
  • Webex Teams (8)

Recent Posts

  • Connecting to UCCX INFORMIX DB with Python on Linux November 13, 2020
  • Exporting CUCM to an Offline Local DB with Python October 25, 2020
  • Get Started Coding with Python: System Setup and Running Scripts September 20, 2020
  • Pass-through Gateway Routing and Dial Peer Groups January 7, 2020
  • Azure OAuth2.0 User Authentication December 16, 2019
Proudly powered by WordPress | Theme: Doo by ThemeVS.