Skip to content

Commit c69211b

Browse files
authored
feat: add Transcoder API samples (GoogleCloudPlatform#1542)
1 parent b6d66b5 commit c69211b

21 files changed

+1608
-0
lines changed

media/transcoder/README

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Google Transcoder PHP Sample Application
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.svg
6+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googlecloudplatform/php-docs-samples&page=editor&working_dir=media/transcoder
7+
8+
## Description
9+
10+
This simple command-line application demonstrates how to invoke
11+
[Google Transcoder API][transcoder-api] from PHP.
12+
13+
[transcoder-api]: https://cloud.google.com/transcoder/docs/reference/libraries
14+
15+
## Build and Run
16+
1. **Enable APIs** - [Enable the Transcoder API](
17+
https://console.cloud.google.com/flows/enableapi?apiid=transcoder.googleapis.com)
18+
and create a new project or select an existing project.
19+
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click
20+
"New Credentials"
21+
and select "Service Account Key". Create a new service account, use the JSON key type, and
22+
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
23+
to the path of the JSON key that was downloaded.
24+
3. **Clone the repo** and cd into this directory
25+
```
26+
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
27+
$ cd media/transcoder
28+
```
29+
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
30+
Run `php composer.phar install` (if composer is installed locally) or `composer install`
31+
(if composer is installed globally).
32+
5. Execute the snippets in the [src/](src/) directory by running
33+
`php src/SNIPPET_NAME.php`. The usage will print for each if no arguments
34+
are provided:
35+
```sh
36+
$ php php src/create_job_from_ad_hoc.php
37+
Usage: create_job_from_ad_hoc.php $projectId $location $inputUri $outputUri
38+
39+
@param string $projectId The ID of your Google Cloud Platform project.
40+
@param string $location The location of the job.
41+
@param string $inputUri Uri of the video in the Cloud Storage bucket.
42+
@param string $outputUri Uri of the video output folder in the Cloud Storage bucket.
43+
44+
45+
$ php create_job_from_ad_hoc.php my-project us-central1 gs://my-bucket/input.mp4 gs://my-bucket/adhoc/
46+
Job: projects/657323817858/locations/us-central1/jobs/13beaa6b-5a33-4a86-b280-04b524546291
47+
```
48+
49+
See the [Transcoder Documentation](https://cloud.google.com/transcoder/docs/) for more information.
50+
51+
## Troubleshooting
52+
53+
* See the [Troubleshooting guide](https://cloud.google.com/transcoder/docs/troubleshooting) for more information.
54+
55+
## Contributing changes
56+
57+
* See [CONTRIBUTING.md](../CONTRIBUTING.md)
58+
59+
## Licensing
60+
61+
* See [LICENSE](../LICENSE)

media/transcoder/composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": {
3+
"google/cloud-video-transcoder": "^0.3.0",
4+
"google/cloud-storage": "^1.9"
5+
}
6+
}

media/transcoder/phpunit.xml.dist

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../../testing/bootstrap.php">
18+
<testsuites>
19+
<testsuite name="Google Transcoder API Tests">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">./srcdirectory>
29+
<exclude>
30+
<directory>./vendordirectory>
31+
exclude>
32+
whitelist>
33+
filter>
34+
<php>
35+
<env name="PHPUNIT_TESTS" value="1"/>
36+
php>
37+
phpunit>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
3+
/**
4+
* Copyright 2021 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+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/media/transcoder/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\Transcoder;
26+
27+
# [START transcoder_create_job_from_ad_hoc]
28+
use Google\Cloud\Video\Transcoder\V1\AudioStream;
29+
use Google\Cloud\Video\Transcoder\V1\ElementaryStream;
30+
use Google\Cloud\Video\Transcoder\V1\Job;
31+
use Google\Cloud\Video\Transcoder\V1\JobConfig;
32+
use Google\Cloud\Video\Transcoder\V1\MuxStream;
33+
use Google\Cloud\Video\Transcoder\V1\TranscoderServiceClient;
34+
use Google\Cloud\Video\Transcoder\V1\VideoStream;
35+
36+
/**
37+
* Creates a job based on an ad-hoc job configuration.
38+
*
39+
* @param string $projectId The ID of your Google Cloud Platform project.
40+
* @param string $location The location of the job.
41+
* @param string $inputUri Uri of the video in the Cloud Storage bucket.
42+
* @param string $outputUri Uri of the video output folder in the Cloud Storage bucket.
43+
*/
44+
function create_job_from_ad_hoc($projectId, $location, $inputUri, $outputUri)
45+
{
46+
// Instantiate a client.
47+
$transcoderServiceClient = new TranscoderServiceClient();
48+
49+
$formattedParent = $transcoderServiceClient->locationName($projectId, $location);
50+
$jobConfig =
51+
(new JobConfig())->setElementaryStreams([
52+
(new ElementaryStream())
53+
->setKey('video-stream0')
54+
->setVideoStream(
55+
(new VideoStream())
56+
->setH264(
57+
(new VideoStream\H264CodecSettings())
58+
->setBitrateBps(550000)
59+
->setFrameRate(60)
60+
->setHeightPixels(360)
61+
->setWidthPixels(640)
62+
)
63+
),
64+
(new ElementaryStream())
65+
->setKey('video-stream1')
66+
->setVideoStream(
67+
(new VideoStream())
68+
->setH264(
69+
(new VideoStream\H264CodecSettings())
70+
->setBitrateBps(2500000)
71+
->setFrameRate(60)
72+
->setHeightPixels(720)
73+
->setWidthPixels(1280)
74+
)
75+
),
76+
(new ElementaryStream())
77+
->setKey('audio-stream0')
78+
->setAudioStream(
79+
(new AudioStream())
80+
->setCodec('aac')
81+
->setBitrateBps(64000)
82+
)
83+
])->setMuxStreams([
84+
(new MuxStream())
85+
->setKey('sd')
86+
->setContainer('mp4')
87+
->setElementaryStreams(['video-stream0', 'audio-stream0']),
88+
(new MuxStream())
89+
->setKey('hd')
90+
->setContainer('mp4')
91+
->setElementaryStreams(['video-stream1', 'audio-stream0'])
92+
]);
93+
94+
$job = (new Job())
95+
->setInputUri($inputUri)
96+
->setOutputUri($outputUri)
97+
->setConfig($jobConfig);
98+
99+
$response = $transcoderServiceClient->createJob($formattedParent, $job);
100+
101+
// Print job name.
102+
printf('Job: %s' . PHP_EOL, $response->getName());
103+
}
104+
# [END transcoder_create_job_from_ad_hoc]
105+
106+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
107+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
3+
/**
4+
* Copyright 2021 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+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/media/transcoder/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\Transcoder;
26+
27+
# [START transcoder_create_job_from_preset]
28+
use Google\Cloud\Video\Transcoder\V1\TranscoderServiceClient;
29+
use Google\Cloud\Video\Transcoder\V1\Job;
30+
31+
/**
32+
* Creates a job based on a job preset.
33+
*
34+
* @param string $projectId The ID of your Google Cloud Platform project.
35+
* @param string $location The location of the job.
36+
* @param string $inputUri Uri of the video in the Cloud Storage bucket.
37+
* @param string $outputUri Uri of the video output folder in the Cloud Storage bucket.
38+
* @param string $preset The preset template (for example, "preset/web-hd").
39+
*/
40+
function create_job_from_preset($projectId, $location, $inputUri, $outputUri, $preset)
41+
{
42+
// Instantiate a client.
43+
$transcoderServiceClient = new TranscoderServiceClient();
44+
45+
$formattedParent = $transcoderServiceClient->locationName($projectId, $location);
46+
$job = new Job();
47+
$job->setInputUri($inputUri);
48+
$job->setOutputUri($outputUri);
49+
$job->setTemplateId($preset);
50+
51+
$response = $transcoderServiceClient->createJob($formattedParent, $job);
52+
53+
// Print job name.
54+
printf('Job: %s' . PHP_EOL, $response->getName());
55+
}
56+
# [END transcoder_create_job_from_preset]
57+
58+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
3+
/**
4+
* Copyright 2021 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+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/media/transcoder/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\Transcoder;
26+
27+
# [START transcoder_create_job_from_template]
28+
use Google\Cloud\Video\Transcoder\V1\TranscoderServiceClient;
29+
use Google\Cloud\Video\Transcoder\V1\Job;
30+
31+
/**
32+
* Creates a job based on a job template.
33+
*
34+
* @param string $projectId The ID of your Google Cloud Platform project.
35+
* @param string $location The location of the job.
36+
* @param string $inputUri Uri of the video in the Cloud Storage bucket.
37+
* @param string $outputUri Uri of the video output folder in the Cloud Storage bucket.
38+
* @param string $templateId The job template ID.
39+
*/
40+
function create_job_from_template($projectId, $location, $inputUri, $outputUri, $templateId)
41+
{
42+
// Instantiate a client.
43+
$transcoderServiceClient = new TranscoderServiceClient();
44+
45+
$formattedParent = $transcoderServiceClient->locationName($projectId, $location);
46+
$job = new Job();
47+
$job->setInputUri($inputUri);
48+
$job->setOutputUri($outputUri);
49+
$job->setTemplateId($templateId);
50+
51+
$response = $transcoderServiceClient->createJob($formattedParent, $job);
52+
53+
// Print job name.
54+
printf('Job: %s' . PHP_EOL, $response->getName());
55+
}
56+
# [END transcoder_create_job_from_template]
57+
58+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)