|
| 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 that embeds captions in the output video. |
| 18 | +
|
| 19 | +Example usage: |
| 20 | + python create_job_with_embedded_captions.py --project_id <project-id> --location <location> \ |
| 21 | + --input_video_uri <uri> --input_captions_uri <uri> --output_uri <uri> |
| 22 | +""" |
| 23 | + |
| 24 | +# [START transcoder_create_job_with_embedded_captions] |
| 25 | + |
| 26 | +import argparse |
| 27 | + |
| 28 | +from google.cloud.video import transcoder_v1 |
| 29 | +from google.cloud.video.transcoder_v1.services.transcoder_service import ( |
| 30 | + TranscoderServiceClient, |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +def create_job_with_embedded_captions( |
| 35 | + project_id, |
| 36 | + location, |
| 37 | + input_video_uri, |
| 38 | + input_captions_uri, |
| 39 | + output_uri, |
| 40 | +): |
| 41 | + """Creates a job based on an ad-hoc job configuration that embeds captions in the output video. |
| 42 | +
|
| 43 | + Args: |
| 44 | + project_id (str): The GCP project ID. |
| 45 | + location (str): The location to start the job in. |
| 46 | + input_video_uri (str): Uri of the input video in the Cloud Storage |
| 47 | + bucket. |
| 48 | + input_captions_uri (str): Uri of the input captions file in the Cloud |
| 49 | + Storage bucket. |
| 50 | + output_uri (str): Uri of the video output folder in the Cloud Storage |
| 51 | + bucket.""" |
| 52 | + |
| 53 | + client = TranscoderServiceClient() |
| 54 | + |
| 55 | + parent = f"projects/{project_id}/locations/{location}" |
| 56 | + job = transcoder_v1.types.Job() |
| 57 | + job.output_uri = output_uri |
| 58 | + job.config = transcoder_v1.types.JobConfig( |
| 59 | + inputs=[ |
| 60 | + transcoder_v1.types.Input( |
| 61 | + key="input0", |
| 62 | + uri=input_video_uri, |
| 63 | + ), |
| 64 | + transcoder_v1.types.Input( |
| 65 | + key="caption-input0", |
| 66 | + uri=input_captions_uri, |
| 67 | + ), |
| 68 | + ], |
| 69 | + edit_list=[ |
| 70 | + transcoder_v1.types.EditAtom( |
| 71 | + key="atom0", |
| 72 | + inputs=["input0", "caption-input0"], |
| 73 | + ), |
| 74 | + ], |
| 75 | + elementary_streams=[ |
| 76 | + transcoder_v1.types.ElementaryStream( |
| 77 | + key="video-stream0", |
| 78 | + video_stream=transcoder_v1.types.VideoStream( |
| 79 | + h264=transcoder_v1.types.VideoStream.H264CodecSettings( |
| 80 | + height_pixels=360, |
| 81 | + width_pixels=640, |
| 82 | + bitrate_bps=550000, |
| 83 | + frame_rate=60, |
| 84 | + ), |
| 85 | + ), |
| 86 | + ), |
| 87 | + transcoder_v1.types.ElementaryStream( |
| 88 | + key="audio-stream0", |
| 89 | + audio_stream=transcoder_v1.types.AudioStream( |
| 90 | + codec="aac", bitrate_bps=64000 |
| 91 | + ), |
| 92 | + ), |
| 93 | + transcoder_v1.types.ElementaryStream( |
| 94 | + key="cea-stream0", |
| 95 | + # The following doesn't work because "mapping" is a reserved |
| 96 | + # argument name in GCP python client libraries (see |
| 97 | + # https://github.com/googleapis/proto-plus-python/blob/main/proto/message.py#L447): |
| 98 | + # |
| 99 | + # text_stream=transcoder_v1.types.TextStream( |
| 100 | + # codec="cea608", |
| 101 | + # mapping=[ |
| 102 | + # transcoder_v1.types.TextStream.TextMapping( |
| 103 | + # atom_key="atom0", |
| 104 | + # input_key="caption-input0", |
| 105 | + # input_track=0, |
| 106 | + # ), |
| 107 | + # ], |
| 108 | + # ), |
| 109 | + # Use a python dictionary as a workaround: |
| 110 | + text_stream={ |
| 111 | + "codec": "cea608", |
| 112 | + "mapping": [ |
| 113 | + { |
| 114 | + "atom_key": "atom0", |
| 115 | + "input_key": "caption-input0", |
| 116 | + "input_track": 0, |
| 117 | + } |
| 118 | + ], |
| 119 | + }, |
| 120 | + ), |
| 121 | + ], |
| 122 | + mux_streams=[ |
| 123 | + transcoder_v1.types.MuxStream( |
| 124 | + key="sd", |
| 125 | + container="mp4", |
| 126 | + elementary_streams=["video-stream0", "audio-stream0"], |
| 127 | + ), |
| 128 | + transcoder_v1.types.MuxStream( |
| 129 | + key="sd-hls", |
| 130 | + container="ts", |
| 131 | + elementary_streams=["video-stream0", "audio-stream0"], |
| 132 | + ), |
| 133 | + transcoder_v1.types.MuxStream( |
| 134 | + key="sd-dash", |
| 135 | + container="fmp4", |
| 136 | + elementary_streams=["video-stream0"], |
| 137 | + ), |
| 138 | + transcoder_v1.types.MuxStream( |
| 139 | + key="audio-dash", |
| 140 | + container="fmp4", |
| 141 | + elementary_streams=["audio-stream0"], |
| 142 | + ), |
| 143 | + ], |
| 144 | + manifests=[ |
| 145 | + transcoder_v1.types.Manifest( |
| 146 | + file_name="manifest.m3u8", |
| 147 | + type_="HLS", |
| 148 | + mux_streams=["sd-hls"], |
| 149 | + ), |
| 150 | + transcoder_v1.types.Manifest( |
| 151 | + file_name="manifest.mpd", |
| 152 | + type_="DASH", |
| 153 | + mux_streams=["sd-dash", "audio-dash"], |
| 154 | + ), |
| 155 | + ], |
| 156 | + ) |
| 157 | + response = client.create_job(parent=parent, job=job) |
| 158 | + print(f"Job: {response.name}") |
| 159 | + return response |
| 160 | + |
| 161 | + |
| 162 | +# [END transcoder_create_job_with_embedded_captions] |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + parser = argparse.ArgumentParser() |
| 166 | + parser.add_argument("--project_id", help="Your Cloud project ID.", required=True) |
| 167 | + parser.add_argument( |
| 168 | + "--location", |
| 169 | + help="The location to start this job in.", |
| 170 | + default="us-central1", |
| 171 | + ) |
| 172 | + parser.add_argument( |
| 173 | + "--input_video_uri", |
| 174 | + help="Uri of the input video in the Cloud Storage bucket.", |
| 175 | + required=True, |
| 176 | + ) |
| 177 | + parser.add_argument( |
| 178 | + "--input_captions_uri", |
| 179 | + help="Uri of the input captions file in the Cloud Storage bucket.", |
| 180 | + required=True, |
| 181 | + ) |
| 182 | + parser.add_argument( |
| 183 | + "--output_uri", |
| 184 | + help="Uri of the video output folder in the Cloud Storage bucket. " |
| 185 | + + "Must end in '/'.", |
| 186 | + required=True, |
| 187 | + ) |
| 188 | + args = parser.parse_args() |
| 189 | + create_job_with_embedded_captions( |
| 190 | + args.project_id, |
| 191 | + args.location, |
| 192 | + args.input_video_uri, |
| 193 | + args.input_captions_uri, |
| 194 | + args.output_uri, |
| 195 | + ) |
0 commit comments