Video Intelligence API 会将语音从支持的视频文件转录为文本。有两种受支持的模型,即“默认”和“视频”。
请求对视频执行语音转录
发送处理请求
下面演示了如何向 videos:annotate
方法发送 POST
请求。该示例使用通过 Google Cloud CLI 为项目设置的服务账号的访问令牌。如需了解有关安装 Google Cloud CLI、使用服务账号设置项目以及获取访问令牌的说明,请参阅 Video Intelligence 快速入门。
在使用任何请求数据之前,请先进行以下替换:
- INPUT_URI:包含要添加注释的文件的 Cloud Storage 存储分区(包括文件名)。必须以
gs://
开头。
例如:"inputUri": "gs://cloud-videointelligence-demo/assistant.mp4",
。 - LANGUAGE_CODE:[可选]请参阅支持的语言
- PROJECT_NUMBER:您的 Google Cloud 项目的数字标识符
HTTP 方法和网址:
POST https://videointelligence.googleapis.com/v1/videos:annotate
请求 JSON 正文:
{ "inputUri": "INPUT_URI ", "features": ["SPEECH_TRANSCRIPTION"], "videoContext": { "speechTranscriptionConfig": { "languageCode": "LANGUAGE_CODE ", "enableAutomaticPunctuation": true, "filterProfanity": true } } }
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project:PROJECT_NUMBER " \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://videointelligence.googleapis.com/v1/videos:annotate"
PowerShell (Windows)
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_NUMBER " }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://videointelligence.googleapis.com/v1/videos:annotate" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{ "name": "projects/PROJECT_NUMBER /locations/LOCATION_ID /operations/OPERATION_ID " }
如果请求成功,则 Video Intelligence 会为您的操作返回 name
。上面的示例展示了此类响应的示例,其中 project-number
是您的项目编号,operation-id
是为请求创建的长时间运行的操作的 ID。
获取结果
要获取请求的结果,您必须使用对 videos:annotate
的调用返回的操作名称发送 GET
,如下例所示。
在使用任何请求数据之前,请先进行以下替换:
- OPERATION_NAME:Video Intelligence API 返回的操作名称。操作名称采用
projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID
格式 - PROJECT_NUMBER:您的 Google Cloud 项目的数字标识符
HTTP 方法和网址:
GET https://videointelligence.googleapis.com/v1/OPERATION_NAME
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project:PROJECT_NUMBER " \
"https://videointelligence.googleapis.com/v1/OPERATION_NAME "
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_NUMBER " }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://videointelligence.googleapis.com/v1/OPERATION_NAME " | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{ "name": "projects/PROJECT_NUMBER /locations/LOCATION_ID /operations/OPERATION_ID ", "metadata": { "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress", "annotationProgress": [{ "inputUri": "/bucket-name-123/sample-video-short.mp4", "progressPercent": 100, "startTime": "2018-04-09T15:19:38.919779Z", "updateTime": "2018-04-09T15:21:17.652470Z" }] }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoResponse", "annotationResults": [ { "speechTranscriptions": [ { "alternatives": [ { "transcript": "and laughing going to talk about is the video intelligence API how many of you saw it at the keynote yesterday ", "confidence": 0.8442509, "words": [ { "startTime": "0.200s", "endTime": "0.800s", "word": "and" }, { "startTime": "0.800s", "endTime": "1.100s", "word": "laughing" }, { "startTime": "1.100s", "endTime": "1.200s", "word": "going" }, ...
下载注释结果
将来源中的注释复制到目标存储分区(请参阅复制文件和对象):
gcloud storage cp gcs_uri gs://my-bucket
注意:如果输出 gcs uri 由用户提供,则注释存储在该 gcs uri 中。
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
如需向 Video Intelligence 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
C#:请按照客户端库页面上的 C# 设置说明操作,然后访问 .NET 版 Video Intelligence 参考文档。
PHP:请按照客户端库页面上的 PHP 设置说明操作,然后访问 PHP 版 Video Intelligence 参考文档。
Ruby:请按照客户端库页面上的 Ruby 设置说明操作,然后访问 Ruby 版 Video Intelligence 参考文档。