Skip to content

Commit 81d9ae3

Browse files
authored
adds local file to video (GoogleCloudPlatform#404)
1 parent 1be55b5 commit 81d9ae3

File tree

6 files changed

+101
-24
lines changed

6 files changed

+101
-24
lines changed

video/README.md

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
## Description
44

5-
This simple command-line application demonstrates how to invoke Google
5+
This simple command-line application demonstrates how to invoke Google
66
Video Intelligence API from PHP.
77

88
## Build and Run
99
1. **Enable APIs** - [Enable the Video Intelligence API](
1010
https://console.cloud.google.com/flows/enableapi?apiid=videointelligence.googleapis.com)
1111
and create a new project or select an existing project.
12-
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click
12+
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click
1313
"New Credentials"
1414
and select "Service Account Key". Create a new service account, use the JSON key type, and
1515
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
@@ -25,27 +25,20 @@ Video Intelligence API from PHP.
2525
5. Run `php video.php`. The following commands are available:
2626

2727
```
28-
help Displays help for a command
29-
list Lists commands
30-
shots Detect shot changes in video using Google Cloud Video Intelligence API
28+
faces Detect faces changes in video using the Video Intelligence API
29+
help Displays help for a command
30+
labels Detect labels in video using the Video Intelligence API
31+
labels-in-file Detect labels in a file using the Video Intelligence API
32+
list Lists commands
33+
safe-search Detect safe search in video using the Video Intelligence API
34+
shots Detect shots in video using the Video Intelligence API
3135
```
3236
3337
Example:
3438
3539
```
36-
$ php video.php shots gs://cloudmleap/video/next/fox-snatched.mp4
37-
annotation_results {
38-
input_uri: "\/cloudmleap\/video\/next\/fox-snatched.mp4"
39-
shot_annotations {
40-
start_time_offset: 41729
41-
end_time_offset: 1000984
42-
}
43-
shot_annotations {
44-
start_time_offset: 1042713
45-
end_time_offset: 6006032
46-
}
47-
...
48-
}
40+
$ php video.php shots gs://demomaker/cat.mp4
41+
0s to 14.833664s
4942
```
5043
5144

video/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"files": [
1010
"src/analyze_faces.php",
1111
"src/analyze_labels.php",
12+
"src/analyze_labels_file.php",
1213
"src/analyze_safe_search.php",
1314
"src/analyze_shots.php"
1415
]

video/src/analyze_labels_file.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2016 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 analyze_labels_file]
21+
use Google\Cloud\VideoIntelligence\V1beta1\VideoIntelligenceServiceClient;
22+
use Google\Cloud\Videointelligence\V1beta1\Feature;
23+
24+
/**
25+
* Finds labels in the video.
26+
*
27+
* @param string $path File path to a video file to analyze.
28+
*/
29+
function analyze_labels_file($path)
30+
{
31+
# Instantiate a client.
32+
$video = new VideoIntelligenceServiceClient();
33+
34+
# Read the local video file and convert it to base64
35+
$inputContent = base64_encode(
36+
file_get_contents($path)
37+
);
38+
39+
# Execute a request.
40+
$operation = $video->annotateVideo(
41+
'',
42+
[Feature::LABEL_DETECTION],
43+
['inputContent' => $inputContent]);
44+
45+
# Wait for the request to complete.
46+
$operation->pollUntilComplete();
47+
48+
# Print the result.
49+
if ($operation->operationSucceeded()) {
50+
$results = $operation->getResult()->getAnnotationResults()[0];
51+
foreach ($results->getLabelAnnotations() as $label) {
52+
printf($label->getDescription() . PHP_EOL);
53+
foreach ($label->getLocations() as $location) {
54+
printf(' %ss to %ss' . PHP_EOL,
55+
$location->getSegment()->getStartTimeOffset() / 1000000,
56+
$location->getSegment()->getEndTimeOffset() / 1000000);
57+
}
58+
}
59+
} else {
60+
print_r($operation->getError());
61+
}
62+
}
63+
// [END analyze_labels_file]

video/test/data/cat_shortened.mp4

1.22 MB
Binary file not shown.

video/test/videoTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
class videoTest extends \PHPUnit_Framework_TestCase
2626
{
27+
private static $gcsUri = 'gs://demomaker/cat.mp4';
28+
2729
public function setUp()
2830
{
2931
if (!getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
@@ -34,37 +36,45 @@ public function setUp()
3436

3537
public function testAnalyzeFaces()
3638
{
37-
$output = $this->runCommand('faces');
39+
$output = $this->runCommand('faces', ['uri' => self::$gcsUri]);
3840
$this->assertContains('left:', $output);
3941
}
4042

4143
public function testAnalyzeLabels()
4244
{
43-
$output = $this->runCommand('labels');
45+
$output = $this->runCommand('labels', ['uri' => self::$gcsUri]);
46+
$this->assertContains('Cat', $output);
47+
}
48+
49+
public function testAnalyzeLabelsInFile()
50+
{
51+
$output = $this->runCommand('labels-in-file', [
52+
'file' => __DIR__ . '/data/cat_shortened.mp4'
53+
]);
4454
$this->assertContains('Cat', $output);
4555
}
4656

4757
public function testAnalyzeSafeSearch()
4858
{
49-
$output = $this->runCommand('safe-search');
59+
$output = $this->runCommand('safe-search', ['uri' => self::$gcsUri]);
5060
$this->assertContains('adult:', $output);
5161
}
5262

5363
public function testAnalyzeShots()
5464
{
55-
$output = $this->runCommand('shots');
65+
$output = $this->runCommand('shots', ['uri' => self::$gcsUri]);
5666
$this->assertContains(' to ', $output);
5767
}
5868

59-
private function runCommand($commandName)
69+
private function runCommand($commandName, $args)
6070
{
6171
$application = require __DIR__ . '/../video.php';
6272
$command = $application->get($commandName);
6373
$commandTester = new CommandTester($command);
6474

6575
ob_start();
6676
$commandTester->execute(
67-
['uri' => 'gs://demomaker/cat.mp4'],
77+
$args,
6878
['interactive' => false]);
6979

7080
return ob_get_clean();

video/video.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@
5252
);
5353
});
5454

55+
$application->add(new Command('labels-in-file'))
56+
->addArgument('file', InputArgument::REQUIRED,
57+
'Path to a local video file.')
58+
->setDescription('Detect labels in a file using the Video Intelligence API')
59+
->setCode(function ($input, $output) {
60+
analyze_labels_file(
61+
$input->getArgument('file')
62+
);
63+
});
64+
5565
$application->add(new Command('safe-search'))
5666
->setDefinition($inputDefinition)
5767
->setDescription('Detect safe search in video using the Video Intelligence API')

0 commit comments

Comments
 (0)