> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Google speech-to-text audio transcripts 集成

> 使用 LangChain Python 集成 Google Speech-to-Text 音频转录文档加载器。

The `SpeechToTextLoader` 允许通过 [Google Cloud Speech-to-Text API](https://cloud.google.com/speech-to-text) 转录音频文件并将转录文本加载到文档中。

使用该功能前，您需要安装 `google-cloud-speech` Python 包，并拥有已启用 [Speech-to-Text API](https://cloud.google.com/speech-to-text/v2/docs/transcribe-client-libraries#before_you_begin) 的 Google Cloud 项目。

* [将大型模型的力量引入Google Cloud的语音API](https://cloud.google.com/blog/products/ai-machine-learning/bringing-power-large-models-google-clouds-speech-api)

## 安装与配置

首先需要安装 `google-cloud-speech` Python 包。

更多详情请参考 [Speech-to-Text 客户端库](https://cloud.google.com/speech-to-text/v2/docs/libraries) 页面。

按照 Google Cloud 文档中的 [快速入门指南](https://cloud.google.com/speech-to-text/v2/docs/sync-recognize) 创建项目并启用 API。

```python theme={null}
pip install -qU langchain-google-community[speech]
```

## 示例

`SpeechToTextLoader` 必须包含 `project_id` 和 `file_path` 参数。音频文件可通过 Google Cloud Storage URI（`gs://...`）或本地文件路径指定。

该加载器仅支持同步请求，对单个音频文件有 [60秒或10MB](https://cloud.google.com/speech-to-text/v2/docs/sync-recognize#:~:text=60%20seconds%20and/or%2010%20MB) 的限制。

```python theme={null}
from langchain_google_community import SpeechToTextLoader

project_id = "<PROJECT_ID>"
file_path = "gs://cloud-samples-data/speech/audio.flac"
# or a local file path: file_path = "./audio.wav"

loader = SpeechToTextLoader(project_id=project_id, file_path=file_path)

docs = loader.load()
```

注意：调用 `loader.load()` 会阻塞直到转录完成。

转录文本将保存在 `page_content` 中：

```python theme={null}
docs[0].page_content
```

```
"How old is the Brooklyn Bridge?"
```

`metadata` 包含完整的JSON响应及更多元数据：

```python theme={null}
docs[0].metadata
```

```json theme={null}
{
  'language_code': 'en-US',
  'result_end_offset': datetime.timedelta(seconds=1)
}
```

## 识别配置

可通过 `config` 参数指定不同语音识别模型并启用特定功能。

参考 [Speech-to-Text 识别器文档](https://cloud.google.com/speech-to-text/v2/docs/recognizers) 和 [`RecognizeRequest`](https://cloud.google.com/python/docs/reference/speech/latest/google.cloud.speech_v2.types.RecognizeRequest) API 参考文档以了解如何设置自定义配置。

未指定 `config` 时，系统将自动选择以下默认配置：

* 模型：[Chirp 通用语音模型](https://cloud.google.com/speech-to-text/v2/docs/chirp-model)
* 语言：`en-US`
* 音频编码：自动检测
* 自动标点：已启用

```python theme={null}
from google.cloud.speech_v2 import (
    AutoDetectDecodingConfig,
    RecognitionConfig,
    RecognitionFeatures,
)
from langchain_google_community import SpeechToTextLoader

project_id = "<PROJECT_ID>"
location = "global"
recognizer_id = "<RECOGNIZER_ID>"
file_path = "./audio.wav"

config = RecognitionConfig(
    auto_decoding_config=AutoDetectDecodingConfig(),
    language_codes=["en-US"],
    model="long",
    features=RecognitionFeatures(
        enable_automatic_punctuation=False,
        profanity_filter=True,
        enable_spoken_punctuation=True,
        enable_spoken_emojis=True,
    ),
)

loader = SpeechToTextLoader(
    project_id=project_id,
    location=location,
    recognizer_id=recognizer_id,
    file_path=file_path,
    config=config,
)
```

***

> 官方原文：[src/oss/python/integrations/document\_loaders/google\_speech\_to\_text.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/document_loaders/google_speech_to_text.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.