Google मेघ पाठ PHP में भाषण के लिए

Sep 30 2020

मैं अपनी php वेबसाइट में लाइव Cpanel सर्वर पर होस्ट करने के लिए Google के पाठ को भाषण में उपयोग करने का प्रयास कर रहा हूं

मैंने टेक्स्ट को स्पीच API, क्रेडेंशियल सेक्शन में API KEY क्रिएट किया है, क्रिएट सर्विस अकाउंट की पेज से क्रेडेंशियल्स की json फाइल डाउनलोड की है।

फिर मैंने गितुब से नमूना फ़ाइलों को डाउनलोड किया और लाइब्रेरी बनाने के लिए संगीतकार का भी उपयोग किया

अब मुझे समझ में नहीं आ रहा है कि मैं अपनी चाबी कहां रखूं। हर स्थान पर, यह शेल में कुंजी को एक्सपोज करने के लिए डींग करता है, लेकिन यह 1 ओपन कमांड प्रॉम्प्ट सत्र के लिए काम करेगा और इसे हर बार निर्यात करना होगा।

के रूप में मैं एक जीवित cpanel आधारित होस्टिंग पर इस कोड को चलाना चाहते हैं, तो मुझे लगता है कि यह निर्यात करने के लिए संभव नहीं होगा।

क्या कोड के भीतर कोई स्थान है जहां मैं कुंजी पारित कर सकता हूं?

पर इस यूआरएल पहले उत्तर निर्यात करने के लिए कर्ल की प्रतिक्रिया: stackoverflow पर लेख synthesize-text.txt लेकिन हम एमपी 3 उत्पादन की आवश्यकता होती है

एक अन्य सवाल का जवाब है कि हम का उपयोग करना चाहिए JQ लेकिन जब से अपने एक सर्वर hsoting साझा, मुझे यकीन है कि अगर हम व्यवस्था कर सकते हैं नहीं कर रहा हूँ JQ ।

क्या इस समस्या का कोई तरीका है?


अपडेट करें

@ V.Tur द्वारा उत्तर का उल्लेख करने के बाद निम्नलिखित कोड की कोशिश की

$params = [ "audioConfig"=>[ "audioEncoding"=>"MP3", "pitch"=> "1", "speakingRate"=> "1", "effectsProfileId"=> [ "medium-bluetooth-speaker-class-device" ] ], "input"=>[ "ssml"=>'<speak>The <say-as interpret-as=\"characters\">SSML</say-as> standard <break time=\"1s\"/>is defined by the <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>' ], "voice"=>[ "languageCode"=> "hi-IN", "name" =>"hi-IN-Wavenet-B", 'ssmlGender'=>'MALE' ] ]; $data_string = json_encode($params); $speech_api_key = "My_Key_Here";
$url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
$handle = curl_init($url);

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)
    ]                                                                       
);
$response = curl_exec($handle);              
$responseDecoded = json_decode($response, true);  
curl_close($handle); if($responseDecoded['audioContent']){
    return $responseDecoded['audioContent'];                
} 

मुझे ऑडियो डाउनलोड किया जाता है, लेकिन ssml में मैंने जो पोज़ / ब्रेक का उल्लेख किया है वह काम नहीं करता। मैंने नीचे दिए अनुसार $ params को डेटा पास करने की कोशिश की

$params = "{
    'input':{
     'ssml':'<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
          standard <break time=\"1s\"/>is defined by the
          <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>'
    },
    'voice':{
      'languageCode':'en-us',
      'name':'en-US-Standard-B',
      'ssmlGender':'MALE'
    },
    'audioConfig':{
      'audioEncoding':'MP3'
    }
}";

लेकिन मुझे निम्नलिखित त्रुटि मिलती है:

सरणी ([त्रुटि] => सरणी ([कोड] => ४०० [संदेश] => अमान्य JSON पेलोड प्राप्त हुआ। अज्ञात नाम "": रूट तत्व एक संदेश होना चाहिए। [स्थिति] => INVALID_ARGUMENT [विवरण] => सरणी ( [०] => एरियर ([@type] => type.googleapis.com/google.rpc.BadRequest [fieldViolations] => एरियर ([०] => एरियर ([विवरण] =) - अज्ञात JSON पेलोड प्राप्त हुआ। अज्ञात नाम) "": मूल तत्व एक संदेश होना चाहिए।))))))

इसे कैसे हल करें?

जवाब

2 V.Tur Sep 30 2020 at 00:22

मेरे काम करने के उदाहरण से लेकर भाषण तक, आप अपनी आवश्यकताओं के लिए फिर से कर सकते हैं:

public static function getSound($text) { $text = trim($text); if($text == '') return false;
            
            $params = [ "audioConfig"=>[ "audioEncoding"=>"LINEAR16", "pitch"=> "1", "speakingRate"=> "1", "effectsProfileId"=> [ "medium-bluetooth-speaker-class-device" ] ], "input"=>[ "text"=>$text
                ],
                "voice"=>[
                    "languageCode"=> "en-US",
                    "name" =>"en-US-Wavenet-F"
                ]
            ];

            $data_string = json_encode($params);

            $url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
            $handle = curl_init($url);
            
            curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($handle, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)
                ]                                                                       
            );
            $response = curl_exec($handle);              
            $responseDecoded = json_decode($response, true);  
            curl_close($handle); if($responseDecoded['audioContent']){
                return $responseDecoded['audioContent']; } return false; } using: public static function saveSound($text)
   {
      $speech_data = SpeechAPI::getSound($text);//see method upper

      if($speech_data) { $file_name = strtolower(md5(uniqid($text)) . '.mp3'); $path = FileUpload::getFolder();//just return directory path
         if(file_put_contents($path.$file_name, base64_decode($speech_data))){ return $file_name;
             }
         }

        return null;
   }

SSML के लिए इनपुट पैरा बदलने की जरूरत है:

$text = "<speak>The <say-as interpret-as=\"characters\">SSML</say-as> standard <break time=\"1s\"/>is defined by the <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>"; $params = [
    "audioConfig"=>[
    "audioEncoding"=>"LINEAR16",
    "pitch"=> "1",
    "speakingRate"=> "1",
    "effectsProfileId"=> [
        "medium-bluetooth-speaker-class-device"
       ]
     ],
     "input"=>[
         //"text"=>$text "ssml" => $text
          ],
          "voice"=>[
              "languageCode"=> "en-US",
              "name" =>"en-US-Wavenet-F"
            ]
         ];

के बारे में चुनें https://cloud.google.com/speech-to-text/docs/encoding