Skip to content

Commit 128a303

Browse files
authored
feat: add spritesheet samples and tests (GoogleCloudPlatform#4878)
b:181782257 - [X] **Tests** pass: `mvn clean verify` **required** - [X] **Lint** passes: `mvn -P lint checkstyle:check` **required** - [X] Please **merge** this PR for me once it is approved.
1 parent f55d55f commit 128a303

File tree

4 files changed

+604
-0
lines changed

4 files changed

+604
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2021 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_with_periodic_images_spritesheet]
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.SpriteSheet;
31+
import com.google.cloud.video.transcoder.v1beta1.TranscoderServiceClient;
32+
import com.google.cloud.video.transcoder.v1beta1.VideoStream;
33+
import com.google.protobuf.Duration;
34+
import java.io.IOException;
35+
36+
public class CreateJobWithPeriodicImagesSpritesheet {
37+
38+
public static final String smallSpritesheetFilePrefix = "small-sprite-sheet";
39+
public static final String largeSpritesheetFilePrefix = "large-sprite-sheet";
40+
public static final String spritesheetFileSuffix = "0000000000.jpeg";
41+
42+
public static void main(String[] args) throws IOException {
43+
// TODO(developer): Replace these variables before running the sample.
44+
String projectId = "my-project-id";
45+
String location = "us-central1";
46+
String inputUri = "gs://my-bucket/my-video-file";
47+
String outputUri = "gs://my-bucket/my-output-folder/";
48+
49+
createJobWithPeriodicImagesSpritesheet(projectId, location, inputUri, outputUri);
50+
}
51+
52+
// Creates a job from an ad-hoc configuration and generates two spritesheets from the input video.
53+
// Each spritesheet contains images that are captured periodically based on a user-defined time
54+
// interval.
55+
public static void createJobWithPeriodicImagesSpritesheet(
56+
String projectId, String location, String inputUri, String outputUri) throws IOException {
57+
// Initialize client that will be used to send requests. This client only needs to be created
58+
// once, and can be reused for multiple requests.
59+
try (TranscoderServiceClient transcoderServiceClient = TranscoderServiceClient.create()) {
60+
61+
VideoStream videoStream0 =
62+
VideoStream.newBuilder()
63+
.setCodec("h264")
64+
.setBitrateBps(550000)
65+
.setFrameRate(60)
66+
.setHeightPixels(360)
67+
.setWidthPixels(640)
68+
.build();
69+
70+
AudioStream audioStream0 =
71+
AudioStream.newBuilder().setCodec("aac").setBitrateBps(64000).build();
72+
73+
// Generates a spritesheet of small images taken periodically from the input video. To
74+
// preserve the source aspect ratio, you should set the spriteWidthPixels field or the
75+
// spriteHeightPixels field, but not both.
76+
SpriteSheet smallSpriteSheet =
77+
SpriteSheet.newBuilder()
78+
.setFilePrefix(smallSpritesheetFilePrefix)
79+
.setSpriteHeightPixels(32)
80+
.setSpriteWidthPixels(64)
81+
.setInterval(Duration.newBuilder().setSeconds(7).build())
82+
.build();
83+
84+
// Generates a spritesheet of larger images taken periodically from the input video. To
85+
SpriteSheet largeSpriteSheet =
86+
SpriteSheet.newBuilder()
87+
.setFilePrefix(largeSpritesheetFilePrefix)
88+
.setSpriteHeightPixels(72)
89+
.setSpriteWidthPixels(128)
90+
.setInterval(Duration.newBuilder().setSeconds(7).build())
91+
.build();
92+
93+
JobConfig config =
94+
JobConfig.newBuilder()
95+
.addInputs(Input.newBuilder().setKey("input0").setUri(inputUri))
96+
.setOutput(Output.newBuilder().setUri(outputUri))
97+
.addElementaryStreams(
98+
ElementaryStream.newBuilder()
99+
.setKey("video_stream0")
100+
.setVideoStream(videoStream0))
101+
.addElementaryStreams(
102+
ElementaryStream.newBuilder()
103+
.setKey("audio_stream0")
104+
.setAudioStream(audioStream0))
105+
.addMuxStreams(
106+
MuxStream.newBuilder()
107+
.setKey("sd")
108+
.setContainer("mp4")
109+
.addElementaryStreams("video_stream0")
110+
.addElementaryStreams("audio_stream0")
111+
.build())
112+
.addSpriteSheets(smallSpriteSheet) // Add the spritesheet config to the job config
113+
.addSpriteSheets(largeSpriteSheet) // Add the spritesheet config to the job config
114+
.build();
115+
116+
var createJobRequest =
117+
CreateJobRequest.newBuilder()
118+
.setJob(
119+
Job.newBuilder()
120+
.setInputUri(inputUri)
121+
.setOutputUri(outputUri)
122+
.setConfig(config)
123+
.build())
124+
.setParent(LocationName.of(projectId, location).toString())
125+
.build();
126+
127+
// Send the job creation request and process the response.
128+
Job job = transcoderServiceClient.createJob(createJobRequest);
129+
System.out.println("Job: " + job.getName());
130+
}
131+
}
132+
}
133+
// [END transcoder_create_job_with_periodic_images_spritesheet]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright 2021 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_with_set_number_images_spritesheet]
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.SpriteSheet;
31+
import com.google.cloud.video.transcoder.v1beta1.TranscoderServiceClient;
32+
import com.google.cloud.video.transcoder.v1beta1.VideoStream;
33+
import java.io.IOException;
34+
35+
public class CreateJobWithSetNumberImagesSpritesheet {
36+
37+
public static final String smallSpritesheetFilePrefix = "small-sprite-sheet";
38+
public static final String largeSpritesheetFilePrefix = "large-sprite-sheet";
39+
public static final String spritesheetFileSuffix = "0000000000.jpeg";
40+
41+
public static void main(String[] args) throws IOException {
42+
// TODO(developer): Replace these variables before running the sample.
43+
String projectId = "my-project-id";
44+
String location = "us-central1";
45+
String inputUri = "gs://my-bucket/my-video-file";
46+
String outputUri = "gs://my-bucket/my-output-folder/";
47+
48+
createJobWithSetNumberImagesSpritesheet(projectId, location, inputUri, outputUri);
49+
}
50+
51+
// Creates a job from an ad-hoc configuration and generates two spritesheets from the input video.
52+
// Each spritesheet contains a set number of images.
53+
public static void createJobWithSetNumberImagesSpritesheet(
54+
String projectId, String location, String inputUri, String outputUri) throws IOException {
55+
// Initialize client that will be used to send requests. This client only needs to be created
56+
// once, and can be reused for multiple requests.
57+
try (TranscoderServiceClient transcoderServiceClient = TranscoderServiceClient.create()) {
58+
59+
VideoStream videoStream0 =
60+
VideoStream.newBuilder()
61+
.setCodec("h264")
62+
.setBitrateBps(550000)
63+
.setFrameRate(60)
64+
.setHeightPixels(360)
65+
.setWidthPixels(640)
66+
.build();
67+
68+
AudioStream audioStream0 =
69+
AudioStream.newBuilder().setCodec("aac").setBitrateBps(64000).build();
70+
71+
// Generates a 10x10 spritesheet of small images from the input video. To preserve the source
72+
// aspect ratio, you should set the spriteWidthPixels field or the spriteHeightPixels
73+
// field, but not both.
74+
SpriteSheet smallSpriteSheet =
75+
SpriteSheet.newBuilder()
76+
.setFilePrefix(smallSpritesheetFilePrefix)
77+
.setSpriteHeightPixels(32)
78+
.setSpriteWidthPixels(64)
79+
.setColumnCount(10)
80+
.setRowCount(10)
81+
.setTotalCount(100)
82+
.build();
83+
84+
// Generates a 10x10 spritesheet of larger images from the input video.
85+
SpriteSheet largeSpriteSheet =
86+
SpriteSheet.newBuilder()
87+
.setFilePrefix(largeSpritesheetFilePrefix)
88+
.setSpriteHeightPixels(72)
89+
.setSpriteWidthPixels(128)
90+
.setColumnCount(10)
91+
.setRowCount(10)
92+
.setTotalCount(100)
93+
.build();
94+
95+
JobConfig config =
96+
JobConfig.newBuilder()
97+
.addInputs(Input.newBuilder().setKey("input0").setUri(inputUri))
98+
.setOutput(Output.newBuilder().setUri(outputUri))
99+
.addElementaryStreams(
100+
ElementaryStream.newBuilder()
101+
.setKey("video_stream0")
102+
.setVideoStream(videoStream0))
103+
.addElementaryStreams(
104+
ElementaryStream.newBuilder()
105+
.setKey("audio_stream0")
106+
.setAudioStream(audioStream0))
107+
.addMuxStreams(
108+
MuxStream.newBuilder()
109+
.setKey("sd")
110+
.setContainer("mp4")
111+
.addElementaryStreams("video_stream0")
112+
.addElementaryStreams("audio_stream0")
113+
.build())
114+
.addSpriteSheets(smallSpriteSheet) // Add the spritesheet config to the job config
115+
.addSpriteSheets(largeSpriteSheet) // Add the spritesheet config to the job config
116+
.build();
117+
118+
var createJobRequest =
119+
CreateJobRequest.newBuilder()
120+
.setJob(
121+
Job.newBuilder()
122+
.setInputUri(inputUri)
123+
.setOutputUri(outputUri)
124+
.setConfig(config)
125+
.build())
126+
.setParent(LocationName.of(projectId, location).toString())
127+
.build();
128+
129+
// Send the job creation request and process the response.
130+
Job job = transcoderServiceClient.createJob(createJobRequest);
131+
System.out.println("Job: " + job.getName());
132+
}
133+
}
134+
}
135+
// [END transcoder_create_job_with_set_number_images_spritesheet]

0 commit comments

Comments
 (0)