Skip to content

Commit c71293e

Browse files
authored
Merge pull request GoogleCloudPlatform#767 from GoogleCloudPlatform/video-transcription
video speech transcription GA
2 parents 7cf5946 + 93e114f commit c71293e

File tree

5 files changed

+119
-2
lines changed

5 files changed

+119
-2
lines changed

video/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Video Intelligence API from PHP.
3737
list Lists commands
3838
safe-search Detect safe search in video using the Video Intelligence API
3939
shots Detect shots in video using the Video Intelligence API
40+
transcription Transcribe speech from a video
4041
```
4142
4243
Example:

video/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
"type": "project",
44
"require": {
55
"symfony/console": "^3.1",
6-
"google/cloud-videointelligence": "^1.0"
6+
"google/cloud-videointelligence": "^1.2"
77
},
88
"autoload": {
99
"files": [
1010
"src/analyze_labels.php",
1111
"src/analyze_labels_file.php",
1212
"src/analyze_explicit_content.php",
13-
"src/analyze_shots.php"
13+
"src/analyze_shots.php",
14+
"src/analyze_transcription.php"
1415
]
1516
},
1617
"require-dev": {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2018 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
namespace Google\Cloud\Samples\Video;
19+
20+
// [START video_speech_transcription_gcs]
21+
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
22+
use Google\Cloud\VideoIntelligence\V1\Feature;
23+
use Google\Cloud\VideoIntelligence\V1\VideoContext;
24+
use Google\Cloud\VideoIntelligence\V1\SpeechTranscriptionConfig;
25+
26+
/**
27+
* Transcribe speech from a video stored on GCS.
28+
*
29+
* @param string $path The cloud storage object to analyze.
30+
*/
31+
function analyze_transcription($uri, array $options = [])
32+
{
33+
# set configs
34+
$features = [Feature::SPEECH_TRANSCRIPTION];
35+
$speechTranscriptionConfig = (new SpeechTranscriptionConfig())
36+
->setLanguageCode('en-US')
37+
->setEnableAutomaticPunctuation(true);
38+
$videoContext = (new VideoContext())
39+
->setSpeechTranscriptionConfig($speechTranscriptionConfig);
40+
41+
# instantiate a client
42+
$client = new VideoIntelligenceServiceClient();
43+
44+
# execute a request.
45+
$operation = $client->annotateVideo([
46+
'inputUri' => $uri,
47+
'features' => $features,
48+
'videoContext' => $videoContext
49+
]);
50+
51+
print('Processing video for speech transcription...' . PHP_EOL);
52+
# Wait for the request to complete.
53+
$operation->pollUntilComplete($options);
54+
55+
# Print the result.
56+
if ($operation->operationSucceeded()) {
57+
$result = $operation->getResult();
58+
# there is only one annotation_result since only
59+
# one video is processed.
60+
$annotationResults = $result->getAnnotationResults()[0];
61+
$speechTranscriptions = $annotationResults ->getSpeechTranscriptions();
62+
63+
foreach ($speechTranscriptions as $transcription) {
64+
# the number of alternatives for each transcription is limited by
65+
# $max_alternatives in SpeechTranscriptionConfig
66+
# each alternative is a different possible transcription
67+
# and has its own confidence score.
68+
foreach ($transcription->getAlternatives() as $alternative) {
69+
print('Alternative level information' . PHP_EOL);
70+
71+
printf('Transcript: %s' . PHP_EOL, $alternative->getTranscript());
72+
printf('Confidence: %s' . PHP_EOL, $alternative->getConfidence());
73+
74+
print('Word level information:');
75+
foreach ($alternative->getWords() as $wordInfo) {
76+
printf('%s s - %s s: %s' . PHP_EOL,
77+
$wordInfo->getStartTime()->getSeconds(),
78+
$wordInfo->getEndTime()->getSeconds(),
79+
$wordInfo->getWord());
80+
}
81+
}
82+
}
83+
}
84+
$client->close();
85+
}
86+
// [END video_speech_transcription_gcs]

video/test/videoTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,30 @@ public function testAnalyzeShots()
8585
$this->assertContains(' to ', $output);
8686
}
8787

88+
public function testTranscription()
89+
{
90+
$output = $this->runCommand('transcription', [
91+
'uri' => $this->gcsUriTwo(),
92+
'--polling-interval-seconds' => 10,
93+
]);
94+
$this->assertContains('Transcript:', $output);
95+
$this->assertContains('Paris', $output);
96+
$this->assertContains('France', $output);
97+
}
98+
8899
private function gcsUri()
89100
{
90101
return sprintf(
91102
'gs://%s/video/cat_shortened.mp4',
92103
$this->requireEnv('GOOGLE_STORAGE_BUCKET')
93104
);
94105
}
106+
107+
private function gcsUriTwo()
108+
{
109+
return sprintf(
110+
'gs://%s/video/googlework_short.mp4',
111+
$this->requireEnv('GOOGLE_STORAGE_BUCKET')
112+
);
113+
}
95114
}

video/video.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@
8484
);
8585
});
8686

87+
$application->add(new Command('transcription'))
88+
->setDefinition($inputDefinition)
89+
->setDescription('Transcribe speech from a video')
90+
->setCode(function ($input, $output) {
91+
analyze_transcription(
92+
$input->getArgument('uri'),
93+
['pollingIntervalSeconds' => $input->getOption('polling-interval-seconds')]
94+
);
95+
});
96+
8797
// for testing
8898
if (getenv('PHPUNIT_TESTS') === '1') {
8999
return $application;

0 commit comments

Comments
 (0)