|
| 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_with_concatenated_inputs] |
| 28 | +use Google\Cloud\Video\Transcoder\V1\AudioStream; |
| 29 | +use Google\Cloud\Video\Transcoder\V1\EditAtom; |
| 30 | +use Google\Cloud\Video\Transcoder\V1\ElementaryStream; |
| 31 | +use Google\Cloud\Video\Transcoder\V1\Input; |
| 32 | +use Google\Cloud\Video\Transcoder\V1\Job; |
| 33 | +use Google\Cloud\Video\Transcoder\V1\JobConfig; |
| 34 | +use Google\Cloud\Video\Transcoder\V1\MuxStream; |
| 35 | +use Google\Cloud\Video\Transcoder\V1\TranscoderServiceClient; |
| 36 | +use Google\Cloud\Video\Transcoder\V1\VideoStream; |
| 37 | +use Google\Protobuf\Duration; |
| 38 | + |
| 39 | +/** |
| 40 | + * Creates a job based on a supplied job config that concatenates two input videos. |
| 41 | + * |
| 42 | + * @param string $projectId The ID of your Google Cloud Platform project. |
| 43 | + * @param string $location The location of the job. |
| 44 | + * @param string $input1Uri Uri of the first video in the Cloud Storage bucket. |
| 45 | + * @param float $startTimeInput1 Start time, in fractional seconds, relative to the first input video timeline. |
| 46 | + * @param float $endTimeInput1 End time, in fractional seconds, relative to the first input video timeline. |
| 47 | + * @param string $input2Uri Uri of the second video in the Cloud Storage bucket. |
| 48 | + * @param float $startTimeInput2 Start time, in fractional seconds, relative to the second input video timeline. |
| 49 | + * @param float $endTimeInput2 End time, in fractional seconds, relative to the second input video timeline. |
| 50 | + * @param string $outputUri Uri of the video output folder in the Cloud Storage bucket. |
| 51 | + */ |
| 52 | +function create_job_with_concatenated_inputs($projectId, $location, $input1Uri, $startTimeInput1, $endTimeInput1, $input2Uri, $startTimeInput2, $endTimeInput2, $outputUri) |
| 53 | +{ |
| 54 | + $startTimeInput1Sec = (int) floor(abs($startTimeInput1)); |
| 55 | + $startTimeInput1Nanos = (int) (1000000000 * bcsub(abs($startTimeInput1), floor(abs($startTimeInput1)), 4)); |
| 56 | + $endTimeInput1Sec = (int) floor(abs($endTimeInput1)); |
| 57 | + $endTimeInput1Nanos = (int) (1000000000 * bcsub(abs($endTimeInput1), floor(abs($endTimeInput1)), 4)); |
| 58 | + |
| 59 | + $startTimeInput2Sec = (int) floor(abs($startTimeInput2)); |
| 60 | + $startTimeInput2Nanos = (int) (1000000000 * bcsub(abs($startTimeInput2), floor(abs($startTimeInput2)), 4)); |
| 61 | + $endTimeInput2Sec = (int) floor(abs($endTimeInput2)); |
| 62 | + $endTimeInput2Nanos = (int) (1000000000 * bcsub(abs($endTimeInput2), floor(abs($endTimeInput2)), 4)); |
| 63 | + |
| 64 | + // Instantiate a client. |
| 65 | + $transcoderServiceClient = new TranscoderServiceClient(); |
| 66 | + |
| 67 | + $formattedParent = $transcoderServiceClient->locationName($projectId, $location); |
| 68 | + $jobConfig = |
| 69 | + (new JobConfig())->setInputs([ |
| 70 | + (new Input()) |
| 71 | + ->setKey('input1') |
| 72 | + ->setUri($input1Uri), |
| 73 | + (new Input()) |
| 74 | + ->setKey('input2') |
| 75 | + ->setUri($input2Uri) |
| 76 | + ])->setEditList([ |
| 77 | + (new EditAtom()) |
| 78 | + ->setKey('atom1') |
| 79 | + ->setInputs(['input1']) |
| 80 | + ->setStartTimeOffset(new Duration(['seconds' => $startTimeInput1Sec, 'nanos' => $startTimeInput1Nanos])) |
| 81 | + ->setEndTimeOffset(new Duration(['seconds' => $endTimeInput1Sec, 'nanos' => $endTimeInput1Nanos])), |
| 82 | + (new EditAtom()) |
| 83 | + ->setKey('atom2') |
| 84 | + ->setInputs(['input2']) |
| 85 | + ->setStartTimeOffset(new Duration(['seconds' => $startTimeInput2Sec, 'nanos' => $startTimeInput2Nanos])) |
| 86 | + ->setEndTimeOffset(new Duration(['seconds' => $endTimeInput2Sec, 'nanos' => $endTimeInput2Nanos])), |
| 87 | + ])->setElementaryStreams([ |
| 88 | + (new ElementaryStream()) |
| 89 | + ->setKey('video-stream0') |
| 90 | + ->setVideoStream( |
| 91 | + (new VideoStream())->setH264( |
| 92 | + (new VideoStream\H264CodecSettings()) |
| 93 | + ->setBitrateBps(550000) |
| 94 | + ->setFrameRate(60) |
| 95 | + ->setHeightPixels(360) |
| 96 | + ->setWidthPixels(640) |
| 97 | + ) |
| 98 | + ), |
| 99 | + (new ElementaryStream()) |
| 100 | + ->setKey('audio-stream0') |
| 101 | + ->setAudioStream( |
| 102 | + (new AudioStream()) |
| 103 | + ->setCodec('aac') |
| 104 | + ->setBitrateBps(64000) |
| 105 | + ) |
| 106 | + ])->setMuxStreams([ |
| 107 | + (new MuxStream()) |
| 108 | + ->setKey('sd') |
| 109 | + ->setContainer('mp4') |
| 110 | + ->setElementaryStreams(['video-stream0', 'audio-stream0']) |
| 111 | + ]); |
| 112 | + |
| 113 | + $job = (new Job()) |
| 114 | + ->setOutputUri($outputUri) |
| 115 | + ->setConfig($jobConfig); |
| 116 | + |
| 117 | + $response = $transcoderServiceClient->createJob($formattedParent, $job); |
| 118 | + |
| 119 | + // Print job name. |
| 120 | + printf('Job: %s' . PHP_EOL, $response->getName()); |
| 121 | +} |
| 122 | +# [END transcoder_create_job_with_concatenated_inputs] |
| 123 | + |
| 124 | +require_once __DIR__ . '/../../../testing/sample_helpers.php'; |
| 125 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments