简体   繁体   English

Python:如何使用 pyaudio 为 Google Cloud Speech API 获取原始音频文件

[英]Python: How to get raw audio file using pyaudio for Google Cloud Speech API

I am using the program given in below link on linux.我在 linux 上使用下面链接中给出的程序。

https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/quickstart.py https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/quickstart.py

The problem I am facing is how to get my own raw audio file recorded by microphone using pyaudio to use the above program to get text of what I have recorded.我面临的问题是如何通过麦克风使用 pyaudio 获取我自己的原始音频文件,以使用上述程序获取我录制内容的文本。

I have the below program of pyaudio but it gives me wav file.我有下面的 pyaudio 程序,但它给了我 wav 文件。 But I want to save raw audio file for google cloud speech api.但我想为谷歌云语音 api 保存原始音频文件。 I don't want to convert wav to raw audio file.我不想将 wav 转换为原始音频文件。 I directly want to save raw audio file using pyaudio.我直接想使用pyaudio保存原始音频文件。

import pyaudio
import wave

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "file.wav"

audio = pyaudio.PyAudio()

# start Recording
 stream = audio.open(format=FORMAT, channels=CHANNELS,
            rate=RATE, input=True,
            frames_per_buffer=CHUNK)
 print "recording..."
 frames = []

 for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
 data = stream.read(CHUNK)
 frames.append(data) 
 print "finished recording"


 # stop Recording
 stream.stop_stream()
 stream.close()
 audio.terminate()

 waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
 waveFile.setnchannels(CHANNELS)
 waveFile.setsampwidth(audio.get_sample_size(FORMAT))
 waveFile.setframerate(RATE)
 waveFile.writeframes(b''.join(frames))
 waveFile.close()

I have found the answer.我找到了答案。 Sorry for posting the question.抱歉发布问题。 I am new in programming..我是编程新手..

import pyaudio
import wave

FORMAT = pyaudio.paInt16

CHANNELS = 1
RATE = 16000
CHUNK = int(RATE / 10)
RECORD_SECONDS = 5

audio = pyaudio.PyAudio()

# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
            rate=RATE, input=True,
            frames_per_buffer=CHUNK)
print "recording..."
frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)
print "finished recording"


# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()



file = open("newfile.raw", "w")
file.write(b''.join(frames))
file.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Google Cloud API Text-to-Speech 获取音素 - How to get phonemes from Google Cloud API Text-to-Speech 使用Python将云语音API的结果导出到JSON文件 - Export the result of cloud speech API to JSON file using Python 在谷歌云语音 api 中使用增强模型 - using enhanced model in google cloud speech api 如何从谷歌语音 api 获得每个话语的结果并将每个音频话语块分别保存为 wav 文件? - How can I get results for each utterances from google speech api and save each audio utterance chunk seperately as wav file? 使用 Python 和 PyAudio 的语音到文本无法在操作系统上运行 - Speech to Text using Python & PyAudio not working on OS 使用Google Cloud Speech转录音频文件时如何解决“请求包含无效的参数错误” - How to fix “request contains an invalid argument error” when transcribing audio file with Google Cloud Speech 使用 Python Speech Client 从 Google Speech 向文本 API 请求“获取操作” - Request a “get operation” from Google Speech to text API using Python Speech Client Google Cloud Speech API python代码示例可能存在错误 - Google Cloud Speech API python code sample has possible bug 谷歌云文本到语音音频到浏览器 - Google Cloud Text-to-Speech Audio to Browser Python 语音识别与谷歌云语音 API - Python SpeechRecognition vs. Google Cloud Speech API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM