|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2022 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 | +"""Google Cloud Transcoder sample for creating a job based on concatenating two input videos. |
| 18 | +
|
| 19 | +Example usage: |
| 20 | + python create_job_with_concatenated_inputs.py --project_id <project-id> --location <location> \ |
| 21 | + --input1_uri <uri> --start_time_input1 <sec> --end_time_input1 <sec> \ |
| 22 | + --input2_uri <uri> --start_time_input2 <sec> --end_time_input2 <sec> \ |
| 23 | + --output_uri <uri> |
| 24 | +""" |
| 25 | + |
| 26 | +# [START transcoder_create_job_with_concatenated_inputs] |
| 27 | + |
| 28 | +import argparse |
| 29 | + |
| 30 | +from google.cloud.video import transcoder_v1 |
| 31 | +from google.cloud.video.transcoder_v1.services.transcoder_service import ( |
| 32 | + TranscoderServiceClient, |
| 33 | +) |
| 34 | +from google.protobuf import duration_pb2 as duration |
| 35 | + |
| 36 | + |
| 37 | +def create_job_with_concatenated_inputs( |
| 38 | + project_id, |
| 39 | + location, |
| 40 | + input1_uri, |
| 41 | + start_time_input1, |
| 42 | + end_time_input1, |
| 43 | + input2_uri, |
| 44 | + start_time_input2, |
| 45 | + end_time_input2, |
| 46 | + output_uri, |
| 47 | +): |
| 48 | + """Creates a job based on an ad-hoc job configuration that concatenates two input videos. |
| 49 | +
|
| 50 | + Args: |
| 51 | + project_id (str): The GCP project ID. |
| 52 | + location (str): The location to start the job in. |
| 53 | + input1_uri (str): Uri of the first video in the Cloud Storage bucket. |
| 54 | + start_time_input1 (str): Start time, in fractional seconds ending in 's' |
| 55 | + (e.g., '0s'), relative to the first input video timeline. |
| 56 | + end_time_input1 (str): End time, in fractional seconds ending in 's' |
| 57 | + (e.g., '8.1s'), relative to the first input video timeline. |
| 58 | + input2_uri (str): Uri of the second video in the Cloud Storage bucket. |
| 59 | + start_time_input2 (str): Start time, in fractional seconds ending in 's' |
| 60 | + (e.g., '3.5s'), relative to the second input video timeline. |
| 61 | + end_time_input2 (str): End time, in fractional seconds ending in 's' |
| 62 | + (e.g., '15s'), relative to the second input video timeline. |
| 63 | + output_uri (str): Uri of the video output folder in the Cloud Storage |
| 64 | + bucket.""" |
| 65 | + |
| 66 | + s1 = duration.Duration() |
| 67 | + s1.FromJsonString(start_time_input1) |
| 68 | + e1 = duration.Duration() |
| 69 | + e1.FromJsonString(end_time_input1) |
| 70 | + |
| 71 | + s2 = duration.Duration() |
| 72 | + s2.FromJsonString(start_time_input2) |
| 73 | + e2 = duration.Duration() |
| 74 | + e2.FromJsonString(end_time_input2) |
| 75 | + |
| 76 | + client = TranscoderServiceClient() |
| 77 | + |
| 78 | + parent = f"projects/{project_id}/locations/{location}" |
| 79 | + job = transcoder_v1.types.Job() |
| 80 | + job.output_uri = output_uri |
| 81 | + job.config = transcoder_v1.types.JobConfig( |
| 82 | + inputs=[ |
| 83 | + transcoder_v1.types.Input( |
| 84 | + key="input1", |
| 85 | + uri=input1_uri, |
| 86 | + ), |
| 87 | + transcoder_v1.types.Input( |
| 88 | + key="input2", |
| 89 | + uri=input2_uri, |
| 90 | + ), |
| 91 | + ], |
| 92 | + edit_list=[ |
| 93 | + transcoder_v1.types.EditAtom( |
| 94 | + key="atom1", |
| 95 | + inputs=["input1"], |
| 96 | + start_time_offset=s1, |
| 97 | + end_time_offset=e1, |
| 98 | + ), |
| 99 | + transcoder_v1.types.EditAtom( |
| 100 | + key="atom2", |
| 101 | + inputs=["input2"], |
| 102 | + start_time_offset=s2, |
| 103 | + end_time_offset=e2, |
| 104 | + ), |
| 105 | + ], |
| 106 | + elementary_streams=[ |
| 107 | + transcoder_v1.types.ElementaryStream( |
| 108 | + key="video-stream0", |
| 109 | + video_stream=transcoder_v1.types.VideoStream( |
| 110 | + h264=transcoder_v1.types.VideoStream.H264CodecSettings( |
| 111 | + height_pixels=360, |
| 112 | + width_pixels=640, |
| 113 | + bitrate_bps=550000, |
| 114 | + frame_rate=60, |
| 115 | + ), |
| 116 | + ), |
| 117 | + ), |
| 118 | + transcoder_v1.types.ElementaryStream( |
| 119 | + key="audio-stream0", |
| 120 | + audio_stream=transcoder_v1.types.AudioStream( |
| 121 | + codec="aac", bitrate_bps=64000 |
| 122 | + ), |
| 123 | + ), |
| 124 | + ], |
| 125 | + mux_streams=[ |
| 126 | + transcoder_v1.types.MuxStream( |
| 127 | + key="sd", |
| 128 | + container="mp4", |
| 129 | + elementary_streams=["video-stream0", "audio-stream0"], |
| 130 | + ), |
| 131 | + ], |
| 132 | + ) |
| 133 | + response = client.create_job(parent=parent, job=job) |
| 134 | + print(f"Job: {response.name}") |
| 135 | + return response |
| 136 | + |
| 137 | + |
| 138 | +# [END transcoder_create_job_with_concatenated_inputs] |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + parser = argparse.ArgumentParser() |
| 142 | + parser.add_argument("--project_id", help="Your Cloud project ID.", required=True) |
| 143 | + parser.add_argument( |
| 144 | + "--location", |
| 145 | + help="The location to start this job in.", |
| 146 | + default="us-central1", |
| 147 | + ) |
| 148 | + parser.add_argument( |
| 149 | + "--input1_uri", |
| 150 | + help="Uri of the first video in the Cloud Storage bucket.", |
| 151 | + required=True, |
| 152 | + ) |
| 153 | + parser.add_argument( |
| 154 | + "--start_time_input1", |
| 155 | + help="Start time, in fractional seconds ending in 's' (e.g., '1.1s'), " |
| 156 | + + "relative to the first input video timeline. Use this field to trim " |
| 157 | + + "content from the beginning of the first video.", |
| 158 | + required=True, |
| 159 | + ) |
| 160 | + parser.add_argument( |
| 161 | + "--end_time_input1", |
| 162 | + help="End time, in fractional seconds ending in 's' (e.g., '9.5s'), " |
| 163 | + + "relative to the first input video timeline. Use this field to trim " |
| 164 | + + "content from the end of the first video.", |
| 165 | + required=True, |
| 166 | + ) |
| 167 | + parser.add_argument( |
| 168 | + "--input2_uri", |
| 169 | + help="Uri of the second video in the Cloud Storage bucket.", |
| 170 | + required=True, |
| 171 | + ) |
| 172 | + parser.add_argument( |
| 173 | + "--start_time_input2", |
| 174 | + help="Start time, in fractional seconds ending in 's' (e.g., '1.1s'), " |
| 175 | + + "relative to the second input video timeline. Use this field to trim " |
| 176 | + + "content from the beginning of the second video.", |
| 177 | + required=True, |
| 178 | + ) |
| 179 | + parser.add_argument( |
| 180 | + "--end_time_input2", |
| 181 | + help="End time, in fractional seconds ending in 's' (e.g., '9.5s'), " |
| 182 | + + "relative to the second input video timeline. Use this field to trim " |
| 183 | + + "content from the end of the second video.", |
| 184 | + required=True, |
| 185 | + ) |
| 186 | + parser.add_argument( |
| 187 | + "--output_uri", |
| 188 | + help="Uri of the video output folder in the Cloud Storage bucket. " |
| 189 | + + "Must end in '/'.", |
| 190 | + required=True, |
| 191 | + ) |
| 192 | + args = parser.parse_args() |
| 193 | + create_job_with_concatenated_inputs( |
| 194 | + args.project_id, |
| 195 | + args.location, |
| 196 | + args.input1_uri, |
| 197 | + args.start_time_input1, |
| 198 | + args.end_time_input1, |
| 199 | + args.input2_uri, |
| 200 | + args.start_time_input2, |
| 201 | + args.end_time_input2, |
| 202 | + args.output_uri, |
| 203 | + ) |
0 commit comments