|
| 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] |
0 commit comments