同步辨識字詞
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
偵測音訊檔案中的語音。
程式碼範例
Ruby
如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。
如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["Detects speech in the audio file.\n\nCode sample \n\nNode.js\n\n\nTo learn how to install and use the client library for Speech-to-Text, see\n[Speech-to-Text client libraries](/speech-to-text/docs/client-libraries).\n\n\nFor more information, see the\n[Speech-to-Text Node.js API\nreference documentation](/nodejs/docs/reference/speech/latest).\n\n\nTo authenticate to Speech-to-Text, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Imports the Google Cloud client library\n const fs = require('fs');\n const speech = require('https://cloud.google.com/nodejs/docs/reference/speech/latest/overview.html');\n\n // Creates a client\n const client = new speech.https://cloud.google.com/nodejs/docs/reference/speech/latest/overview.html();\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample.\n */\n // const filename = 'Local path to audio file, e.g. /path/to/audio.raw';\n // const encoding = 'Encoding of the audio file, e.g. LINEAR16';\n // const sampleRateHertz = 16000;\n // const languageCode = 'BCP-47 language code, e.g. en-US';\n\n const config = {\n enableWordTimeOffsets: true,\n encoding: encoding,\n sampleRateHertz: sampleRateHertz,\n languageCode: languageCode,\n };\n const audio = {\n content: fs.readFileSync(filename).toString('base64'),\n };\n\n const request = {\n config: config,\n audio: audio,\n };\n\n // Detects speech in the audio file\n const [response] = await client.recognize(request);\n response.results.forEach(result =\u003e {\n console.log('Transcription: ', result.alternatives[0].transcript);\n result.alternatives[0].words.forEach(wordInfo =\u003e {\n // NOTE: If you have a time offset exceeding 2^32 seconds, use the\n // wordInfo.{x}Time.seconds.high to calculate seconds.\n const startSecs =\n `${wordInfo.startTime.seconds}` +\n '.' +\n wordInfo.startTime.nanos / 100000000;\n const endSecs =\n `${wordInfo.endTime.seconds}` +\n '.' +\n wordInfo.endTime.nanos / 100000000;\n console.log(`Word: ${wordInfo.word}`);\n console.log(`\\t ${startSecs} secs - ${endSecs} secs`);\n });\n });\n\nRuby\n\n\nTo learn how to install and use the client library for Speech-to-Text, see\n[Speech-to-Text client libraries](/speech-to-text/docs/client-libraries).\n\n\nTo authenticate to Speech-to-Text, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n # audio_file_path = \"Path to file on which to perform speech recognition\"\n\n require \"google/cloud/speech\"\n\n speech = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-speech-v1/latest/Google-Cloud-Speech.html.https://cloud.google.com/ruby/docs/reference/google-cloud-speech/latest/Google-Cloud-Speech.html version: :v1\n\n audio_file = File.binread audio_file_path\n\n config = { encoding: :LINEAR16,\n sample_rate_hertz: 16_000,\n language_code: \"en-US\",\n enable_word_time_offsets: true }\n audio = { content: audio_file }\n\n response = speech.recognize config: config, audio: audio\n\n results = response.results\n\n alternatives = results.first.alternatives\n alternatives.each do |alternative|\n puts \"Transcription: #{alternative.transcript}\"\n\n alternative.words.each do |word|\n start_time = word.start_time.seconds + (word.start_time.nanos / 1_000_000_000.0)\n end_time = word.end_time.seconds + (word.end_time.nanos / 1_000_000_000.0)\n\n puts \"Word: #{word.word} #{start_time} #{end_time}\"\n end\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=speech)."]]