

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 创建视频字幕
<a name="subtitles"></a>

Amazon Transcribe 支持 WebVTT (\*.vtt) SubRip 和 (\*.srt) 输出用作视频字幕。在设置批量视频转录作业时，您可以选择一种或两种文件类型。使用字幕特征时，会生成您选择的字幕文件和常规的转录文件（包含其它信息）。字幕和转录文件输出到同一个目标位置。

在读出文本的同时显示字幕，字幕保持可见，直到出现自然停顿或发言者说完为止。请注意，如果您在转录请求中启用字幕，并且您的音频不包含语音，则不会创建字幕文件。

**重要**  
Amazon Transcribe 字幕输出使用默认`0`的起始索引，这与更广泛使用的值不同。`1`如果您需要的起始索引为`1`，则可以在 AWS 管理控制台 或的 API 请求中使用[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Subtitles.html#transcribe-Type-Subtitles-OutputStartIndex](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Subtitles.html#transcribe-Type-Subtitles-OutputStartIndex)参数指定该索引。

使用错误的起始索引可能会导致与其它服务不兼容，因此在创建字幕之前，请务必确认您要使用哪个起始索引。如果您不确定要使用哪个值，我们建议您选择 `1`。有关更多信息，请参阅 [https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Subtitles.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Subtitles.html)。

字幕支持的特征：
+ **内容编辑** — 任何经过编辑的内容都会在字幕和常规转录输出文件中反映为“`PII`”。音频没有改变。
+ **词汇表过滤器** — 字幕文件由转录文件生成，因此您在标准转录输出中过滤的任何单词也会在字幕中进行过滤。过滤后的内容以空格形式或 `***` 形式显示在您的转录和字幕文件中。音频没有改变。
+ **发言者分类** — 如果给定的字幕片段中有多个发言者，则使用短划线来区分每个发言者。这适用于WebVTT SubRip 和格式；例如：
  + --人物 1 说出的文本
  + --人物 2 说出的文本

字幕文件存储在与转录输出相同 Amazon S3 的位置。

有关创建字幕的视频演练，请参阅：

[![AWS Videos](http://img.youtube.com/vi/PZdfXGggcH4/0.jpg)](http://www.youtube.com/watch?v=PZdfXGggcH4)


## 生成字幕文件
<a name="subtitles-how-to"></a>

您可以使用 **AWS 管理控制台**、**AWS CLI** 或 **AWS SDK** 创建字幕文件；请参阅以下示例：

### AWS 管理控制台
<a name="subtitles-console"></a>

1. 登录到 [AWS 管理控制台](https://console.aws.amazon.com/transcribe/)。

1. 在导航窗格中，选择**转录作业**，然后选择**创建作业**（右上角）。这将打开**指定作业详细信息**页面。字幕选项位于**输出数据**面板中。

1. 为字幕文件选择所需的格式，然后为起始索引选择一个值。请注意， Amazon Transcribe 默认值为`0`，但`1`使用范围更广。如果您不确定要使用哪个值，我们建议您选择 `1`，因为这可能会提高与其它服务的兼容性。  
![Amazon Transcribe 控制台屏幕截图：“指定作业详细信息” 页面上的 “输出数据” 窗格。](http://docs.aws.amazon.com/zh_cn/transcribe/latest/dg/images/subtitles-startindex.png)

1. 在**指定作业详细信息**页面上填写要包含的任何其它字段，然后选择**下一步**。此时您将会看到**配置作业 - *可选***页面。

1. 选择**创建作业**以运行您的转录作业。

### AWS CLI
<a name="subtitles-cli"></a>

此示例使用 [start-transcription-job](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/transcribe/start-transcription-job.html) 命令和 `Subtitles` 参数。有关更多信息，请参阅[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html)和[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Subtitles.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Subtitles.html)。

```
aws transcribe start-transcription-job \
--region {{us-west-2}} \
--transcription-job-name {{my-first-transcription-job}} \
--media MediaFileUri=s3://{{amzn-s3-demo-bucket}}/{{my-input-files}}/{{my-media-file}}.{{flac}} \
--output-bucket-name {{amzn-s3-demo-bucket}} \
--output-key {{my-output-files}}/ \
--language-code {{en-US}} \
--subtitles Formats={{vtt}},{{srt}},OutputStartIndex={{1}}
```

以下是另一个使用 [start-transcription-job](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/transcribe/start-transcription-job.html) 命令的示例，以及一个将字幕添加到该作业的请求正文。

```
aws transcribe start-transcription-job \
--region {{us-west-2}} \
--cli-input-json file://{{my-first-subtitle-job}}.json
```

*my-first-subtitle-job.json* 文件包含以下请求正文。

```
{
  "TranscriptionJobName": "{{my-first-transcription-job}}",
  "Media": {
        "MediaFileUri": "s3://{{amzn-s3-demo-bucket}}/{{my-input-files}}/{{my-media-file}}.{{flac}}"
  },
  "OutputBucketName": "{{amzn-s3-demo-bucket}}",
  "OutputKey": "{{my-output-files}}/", 
  "LanguageCode": "{{en-US}}",
  "Subtitles": {
        "Formats": [
            "{{vtt}}","{{srt}}"
        ],             
        "OutputStartIndex": {{1}}
   }
}
```

### 适用于 Python (Boto3) 的 AWS SDK
<a name="subtitles-python-batch"></a>

此示例使用 [start\_transcription\_](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/transcribe.html#TranscribeService.Client.start_transcription_job) job 方法的`Subtitles`参数添加字幕。 适用于 Python (Boto3) 的 AWS SDK 有关更多信息，请参阅[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_StartTranscriptionJob.html)和[https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Subtitles.html](https://docs.aws.amazon.com/transcribe/latest/APIReference/API_Subtitles.html)。

有关使用 AWS 软件开发工具包的其他示例，包括特定功能、场景和跨服务示例，请参阅本章。[使用 Amazon Transcribe 的代码示例 AWS 软件开发工具包](service_code_examples.md)

```
from __future__ import print_function
import time
import boto3
transcribe = boto3.client('transcribe', '{{us-west-2}}')
job_name = "{{my-first-transcription-job}}"
job_uri = "s3://{{amzn-s3-demo-bucket}}/{{my-input-files}}/{{my-media-file}}.{{flac}}"
transcribe.start_transcription_job(
    TranscriptionJobName = job_name,
    Media = {
        'MediaFileUri': job_uri
    },
    OutputBucketName = '{{amzn-s3-demo-bucket}}',
    OutputKey = '{{my-output-files}}/', 
    LanguageCode = '{{en-US}}', 
    Subtitles = {
        'Formats': [
            '{{vtt}}','{{srt}}'
        ],
        'OutputStartIndex': {{1}} 
   }
)

while True:
    status = transcribe.get_transcription_job(TranscriptionJobName = job_name)
    if status['TranscriptionJob']['TranscriptionJobStatus'] in ['COMPLETED', 'FAILED']:
        break
    print("Not ready yet...")
    time.sleep(5)
print(status)
```