Skip to content

Commit ac4062a

Browse files
authored
feat(Transcoder): add batch mode sample and test (GoogleCloudPlatform#1892)
1 parent c5b6ce2 commit ac4062a

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

media/transcoder/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-video-transcoder": "^0.8.2",
3+
"google/cloud-video-transcoder": "^0.9.0",
44
"google/cloud-storage": "^1.9",
55
"ext-bcmath": "*"
66
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2023 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/main/media/transcoder/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\Transcoder;
26+
27+
# [START transcoder_create_job_from_preset_batch_mode]
28+
use Google\Cloud\Video\Transcoder\V1\Client\TranscoderServiceClient;
29+
use Google\Cloud\Video\Transcoder\V1\CreateJobRequest;
30+
use Google\Cloud\Video\Transcoder\V1\Job;
31+
32+
/**
33+
* Creates a job in batch mode based on a job preset.
34+
*
35+
* @param string $projectId The ID of your Google Cloud Platform project.
36+
* @param string $location The location of the job.
37+
* @param string $inputUri Uri of the video in the Cloud Storage bucket.
38+
* @param string $outputUri Uri of the video output folder in the Cloud Storage bucket.
39+
* @param string $preset The preset template (for example, "preset/web-hd").
40+
*/
41+
function create_job_from_preset_batch_mode($projectId, $location, $inputUri, $outputUri, $preset)
42+
{
43+
// Instantiate a client.
44+
$transcoderServiceClient = new TranscoderServiceClient();
45+
46+
$formattedParent = $transcoderServiceClient->locationName($projectId, $location);
47+
$job = new Job();
48+
$job->setInputUri($inputUri);
49+
$job->setOutputUri($outputUri);
50+
$job->setTemplateId($preset);
51+
$job->setMode(Job\ProcessingMode::PROCESSING_MODE_BATCH);
52+
$job->setBatchModePriority(10);
53+
$request = (new CreateJobRequest())
54+
->setParent($formattedParent)
55+
->setJob($job);
56+
57+
$response = $transcoderServiceClient->createJob($request);
58+
59+
// Print job name.
60+
printf('Job: %s' . PHP_EOL, $response->getName());
61+
}
62+
# [END transcoder_create_job_from_preset_batch_mode]
63+
64+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
65+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

media/transcoder/test/transcoderTest.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class transcoderTest extends TestCase
4747
private static $inputConcatVideo2Uri;
4848
private static $inputOverlayUri;
4949
private static $outputUriForPreset;
50+
private static $outputUriForPresetBatchMode;
5051
private static $outputUriForAdHoc;
5152
private static $outputUriForTemplate;
5253
private static $outputUriForAnimatedOverlay;
@@ -96,6 +97,7 @@ public static function setUpBeforeClass(): void
9697
self::$inputConcatVideo2Uri = sprintf('gs://%s/%s', $bucketName, self::$testConcatVideo2FileName);
9798
self::$inputOverlayUri = sprintf('gs://%s/%s', $bucketName, self::$testOverlayImageFileName);
9899
self::$outputUriForPreset = sprintf('gs://%s/test-output-preset/', $bucketName);
100+
self::$outputUriForPresetBatchMode = sprintf('gs://%s/test-output-preset-batch-mode/', $bucketName);
99101
self::$outputUriForAdHoc = sprintf('gs://%s/test-output-adhoc/', $bucketName);
100102
self::$outputUriForTemplate = sprintf('gs://%s/test-output-template/', $bucketName);
101103
self::$outputUriForAnimatedOverlay = sprintf('gs://%s/test-output-animated-overlay/', $bucketName);
@@ -172,7 +174,7 @@ public function testJobFromAdHoc()
172174
$jobId = explode('/', $createOutput);
173175
$jobId = trim($jobId[(count($jobId) - 1)]);
174176

175-
sleep(30);
177+
sleep(10);
176178
$this->assertJobStateSucceeded($jobId);
177179

178180
// Test Get method
@@ -214,7 +216,7 @@ public function testJobFromPreset()
214216
$jobId = explode('/', $output);
215217
$jobId = trim($jobId[(count($jobId) - 1)]);
216218

217-
sleep(30);
219+
sleep(10);
218220
$this->assertJobStateSucceeded($jobId);
219221

220222
$this->runFunctionSnippet('delete_job', [
@@ -224,6 +226,31 @@ public function testJobFromPreset()
224226
]);
225227
}
226228

229+
public function testJobFromPresetBatchMode()
230+
{
231+
$output = $this->runFunctionSnippet('create_job_from_preset_batch_mode', [
232+
self::$projectId,
233+
self::$location,
234+
self::$inputVideoUri,
235+
self::$outputUriForPresetBatchMode,
236+
self::$preset
237+
]);
238+
239+
$this->assertMatchesRegularExpression(sprintf('%s', self::$jobIdRegex), $output);
240+
241+
$jobId = explode('/', $output);
242+
$jobId = trim($jobId[(count($jobId) - 1)]);
243+
244+
sleep(10);
245+
$this->assertJobStateSucceeded($jobId);
246+
247+
$this->runFunctionSnippet('delete_job', [
248+
self::$projectId,
249+
self::$location,
250+
$jobId
251+
]);
252+
}
253+
227254
public function testJobFromTemplate()
228255
{
229256
$jobTemplateId = sprintf('php-test-template-%s', time());
@@ -246,7 +273,7 @@ public function testJobFromTemplate()
246273
$jobId = explode('/', $output);
247274
$jobId = trim($jobId[(count($jobId) - 1)]);
248275

249-
sleep(30);
276+
sleep(10);
250277
$this->assertJobStateSucceeded($jobId);
251278

252279
$this->runFunctionSnippet('delete_job', [
@@ -277,7 +304,7 @@ public function testJobAnimatedOverlay()
277304
$jobId = explode('/', $output);
278305
$jobId = trim($jobId[(count($jobId) - 1)]);
279306

280-
sleep(30);
307+
sleep(10);
281308
$this->assertJobStateSucceeded($jobId);
282309

283310
$this->runFunctionSnippet('delete_job', [
@@ -302,7 +329,7 @@ public function testJobStaticOverlay()
302329
$jobId = explode('/', $output);
303330
$jobId = trim($jobId[(count($jobId) - 1)]);
304331

305-
sleep(30);
332+
sleep(10);
306333
$this->assertJobStateSucceeded($jobId);
307334

308335
$this->runFunctionSnippet('delete_job', [
@@ -326,7 +353,7 @@ public function testJobPeriodicImagesSpritesheet()
326353
$jobId = explode('/', $output);
327354
$jobId = trim($jobId[(count($jobId) - 1)]);
328355

329-
sleep(30);
356+
sleep(10);
330357
$this->assertJobStateSucceeded($jobId);
331358

332359
$this->runFunctionSnippet('delete_job', [
@@ -350,7 +377,7 @@ public function testJobSetNumberImagesSpritesheet()
350377
$jobId = explode('/', $output);
351378
$jobId = trim($jobId[(count($jobId) - 1)]);
352379

353-
sleep(30);
380+
sleep(10);
354381
$this->assertJobStateSucceeded($jobId);
355382

356383
$this->runFunctionSnippet('delete_job', [
@@ -379,7 +406,7 @@ public function testJobConcat()
379406
$jobId = explode('/', $output);
380407
$jobId = trim($jobId[(count($jobId) - 1)]);
381408

382-
sleep(30);
409+
sleep(10);
383410
$this->assertJobStateSucceeded($jobId);
384411

385412
$this->runFunctionSnippet('delete_job', [

0 commit comments

Comments
 (0)