Skip to content

Commit 69919c9

Browse files
authored
(feat) add Transcoder samples (GoogleCloudPlatform#4238)
* (feat) add Transcoder samples * (feat) add Transcoder samples * (feat) lint fix * Address feedback * Add UUIDs to bucket and job template names for tests * Update pom file * Update pom file
1 parent 39e3e2b commit 69919c9

24 files changed

Lines changed: 2042 additions & 0 deletions

.kokoro/tests/run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ if [[ "$SCRIPT_DEBUG" != "true" ]]; then
5555

5656
# Setup required env variables
5757
export GOOGLE_CLOUD_PROJECT=java-docs-samples-testing
58+
export GOOGLE_CLOUD_PROJECT_NUMBER="779844219229" # For Transcoder samples
5859
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-acct.json
5960
# For Tasks samples
6061
export QUEUE_ID=my-appengine-queue

media/transcoder/pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2020 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<groupId>com.example</groupId>
22+
<artifactId>transcoder</artifactId>
23+
<version>1.0-SNAPSHOT</version>
24+
<packaging>jar</packaging>
25+
<!--
26+
The parent pom defines common style checks and testing strategies for our samples.
27+
Removing or replacing it should not affect the execution of the samples in anyway.
28+
-->
29+
<parent>
30+
<groupId>com.google.cloud.samples</groupId>
31+
<artifactId>shared-configuration</artifactId>
32+
<version>1.0.21</version>
33+
</parent>
34+
<properties>
35+
<maven.compiler.target>11</maven.compiler.target>
36+
<maven.compiler.source>11</maven.compiler.source>
37+
</properties>
38+
<!-- [START dependencies] -->
39+
<!-- Using libraries-bom to manage versions.
40+
See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
41+
<dependencyManagement>
42+
<dependencies>
43+
<dependency>
44+
<groupId>com.google.cloud</groupId>
45+
<artifactId>libraries-bom</artifactId>
46+
<version>15.0.0</version>
47+
<type>pom</type>
48+
<scope>import</scope>
49+
</dependency>
50+
</dependencies>
51+
</dependencyManagement>
52+
<dependencies>
53+
<dependency>
54+
<groupId>com.google.cloud</groupId>
55+
<artifactId>google-cloud-video-transcoder</artifactId>
56+
<version>0.1.3</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.google.cloud</groupId>
60+
<artifactId>google-cloud-core</artifactId>
61+
<scope>compile</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-compiler-plugin</artifactId>
66+
<version>3.8.1</version>
67+
</dependency>
68+
<!-- [END dependencies] -->
69+
<!-- Test dependencies -->
70+
<dependency>
71+
<groupId>junit</groupId>
72+
<artifactId>junit</artifactId>
73+
<version>4.13.1</version>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.google.cloud</groupId>
78+
<artifactId>google-cloud-storage</artifactId>
79+
<version>1.113.3</version>
80+
</dependency>
81+
</dependencies>
82+
</project>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.transcoder;
18+
19+
// [START transcoder_create_job_from_ad_hoc]
20+
21+
import com.google.cloud.video.transcoder.v1beta1.AudioStream;
22+
import com.google.cloud.video.transcoder.v1beta1.CreateJobRequest;
23+
import com.google.cloud.video.transcoder.v1beta1.ElementaryStream;
24+
import com.google.cloud.video.transcoder.v1beta1.Input;
25+
import com.google.cloud.video.transcoder.v1beta1.Job;
26+
import com.google.cloud.video.transcoder.v1beta1.JobConfig;
27+
import com.google.cloud.video.transcoder.v1beta1.LocationName;
28+
import com.google.cloud.video.transcoder.v1beta1.MuxStream;
29+
import com.google.cloud.video.transcoder.v1beta1.Output;
30+
import com.google.cloud.video.transcoder.v1beta1.TranscoderServiceClient;
31+
import com.google.cloud.video.transcoder.v1beta1.VideoStream;
32+
import java.io.IOException;
33+
34+
public class CreateJobFromAdHoc {
35+
36+
public static void main(String[] args) throws Exception {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String projectId = "my-project-id";
39+
String location = "us-central1";
40+
String inputUri = "gs://my-bucket/my-video-file";
41+
String outputUri = "gs://my-bucket/my-output-folder/";
42+
43+
createJobFromAdHoc(projectId, location, inputUri, outputUri);
44+
}
45+
46+
// Creates a job from an ad-hoc configuration.
47+
public static void createJobFromAdHoc(
48+
String projectId, String location, String inputUri, String outputUri) throws IOException {
49+
// Initialize client that will be used to send requests. This client only needs to be created
50+
// once, and can be reused for multiple requests.
51+
try (TranscoderServiceClient transcoderServiceClient = TranscoderServiceClient.create()) {
52+
53+
VideoStream videoStream0 =
54+
VideoStream.newBuilder()
55+
.setCodec("h264")
56+
.setBitrateBps(550000)
57+
.setFrameRate(60)
58+
.setHeightPixels(360)
59+
.setWidthPixels(640)
60+
.build();
61+
VideoStream videoStream1 =
62+
VideoStream.newBuilder()
63+
.setCodec("h264")
64+
.setBitrateBps(2500000)
65+
.setFrameRate(60)
66+
.setHeightPixels(720)
67+
.setWidthPixels(1280)
68+
.build();
69+
AudioStream audioStream0 =
70+
AudioStream.newBuilder().setCodec("aac").setBitrateBps(64000).build();
71+
JobConfig config =
72+
JobConfig.newBuilder()
73+
.addInputs(Input.newBuilder().setKey("input0").setUri(inputUri))
74+
.setOutput(Output.newBuilder().setUri(outputUri))
75+
.addElementaryStreams(
76+
ElementaryStream.newBuilder()
77+
.setKey("video_stream0")
78+
.setVideoStream(videoStream0))
79+
.addElementaryStreams(
80+
ElementaryStream.newBuilder()
81+
.setKey("video_stream1")
82+
.setVideoStream(videoStream1))
83+
.addElementaryStreams(
84+
ElementaryStream.newBuilder()
85+
.setKey("audio_stream0")
86+
.setAudioStream(audioStream0))
87+
.addMuxStreams(
88+
MuxStream.newBuilder()
89+
.setKey("sd")
90+
.setContainer("mp4")
91+
.addElementaryStreams("video_stream0")
92+
.addElementaryStreams("audio_stream0")
93+
.build())
94+
.addMuxStreams(
95+
MuxStream.newBuilder()
96+
.setKey("hd")
97+
.setContainer("mp4")
98+
.addElementaryStreams("video_stream1")
99+
.addElementaryStreams("audio_stream0")
100+
.build())
101+
.build();
102+
103+
var createJobRequest =
104+
CreateJobRequest.newBuilder()
105+
.setJob(
106+
Job.newBuilder()
107+
.setInputUri(inputUri)
108+
.setOutputUri(outputUri)
109+
.setConfig(config)
110+
.build())
111+
.setParent(LocationName.of(projectId, location).toString())
112+
.build();
113+
114+
// Send the job creation request and process the response.
115+
Job job = transcoderServiceClient.createJob(createJobRequest);
116+
System.out.println("Job: " + job.getName());
117+
}
118+
}
119+
}
120+
// [END transcoder_create_job_from_ad_hoc]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.transcoder;
18+
19+
// [START transcoder_create_job_from_preset]
20+
21+
import com.google.cloud.video.transcoder.v1beta1.CreateJobRequest;
22+
import com.google.cloud.video.transcoder.v1beta1.Job;
23+
import com.google.cloud.video.transcoder.v1beta1.LocationName;
24+
import com.google.cloud.video.transcoder.v1beta1.TranscoderServiceClient;
25+
import java.io.IOException;
26+
27+
public class CreateJobFromPreset {
28+
29+
public static void main(String[] args) throws Exception {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "my-project-id";
32+
String location = "us-central1";
33+
String inputUri = "gs://my-bucket/my-video-file";
34+
String outputUri = "gs://my-bucket/my-output-folder/";
35+
String preset = "preset/web-hd";
36+
37+
createJobFromPreset(projectId, location, inputUri, outputUri, preset);
38+
}
39+
40+
// Creates a job from a preset.
41+
public static void createJobFromPreset(
42+
String projectId, String location, String inputUri, String outputUri, String preset)
43+
throws IOException {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests.
46+
try (TranscoderServiceClient transcoderServiceClient = TranscoderServiceClient.create()) {
47+
48+
var createJobRequest =
49+
CreateJobRequest.newBuilder()
50+
.setJob(
51+
Job.newBuilder()
52+
.setInputUri(inputUri)
53+
.setOutputUri(outputUri)
54+
.setTemplateId(preset)
55+
.build())
56+
.setParent(LocationName.of(projectId, location).toString())
57+
.build();
58+
59+
// Send the job creation request and process the response.
60+
Job job = transcoderServiceClient.createJob(createJobRequest);
61+
System.out.println("Job: " + job.getName());
62+
}
63+
}
64+
}
65+
// [END transcoder_create_job_from_preset]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.transcoder;
18+
19+
// [START transcoder_create_job_from_template]
20+
21+
import com.google.cloud.video.transcoder.v1beta1.CreateJobRequest;
22+
import com.google.cloud.video.transcoder.v1beta1.Job;
23+
import com.google.cloud.video.transcoder.v1beta1.LocationName;
24+
import com.google.cloud.video.transcoder.v1beta1.TranscoderServiceClient;
25+
import java.io.IOException;
26+
27+
public class CreateJobFromTemplate {
28+
29+
public static void main(String[] args) throws Exception {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "my-project-id";
32+
String location = "us-central1";
33+
String inputUri = "gs://my-bucket/my-video-file";
34+
String outputUri = "gs://my-bucket/my-output-folder/";
35+
String templateId = "my-job-template";
36+
37+
createJobFromTemplate(projectId, location, inputUri, outputUri, templateId);
38+
}
39+
40+
// Creates a job from a job template.
41+
public static void createJobFromTemplate(
42+
String projectId, String location, String inputUri, String outputUri, String templateId)
43+
throws IOException {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests.
46+
try (TranscoderServiceClient transcoderServiceClient = TranscoderServiceClient.create()) {
47+
48+
var createJobRequest =
49+
CreateJobRequest.newBuilder()
50+
.setJob(
51+
Job.newBuilder()
52+
.setInputUri(inputUri)
53+
.setOutputUri(outputUri)
54+
.setTemplateId(templateId)
55+
.build())
56+
.setParent(LocationName.of(projectId, location).toString())
57+
.build();
58+
59+
// Send the job creation request and process the response.
60+
Job job = transcoderServiceClient.createJob(createJobRequest);
61+
System.out.println("Job: " + job.getName());
62+
}
63+
}
64+
}
65+
// [END transcoder_create_job_from_template]

0 commit comments

Comments
 (0)