|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2020 Google Inc. All Rights Reserved. |
| 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 a supplied job config. |
| 18 | +
|
| 19 | +Example usage: |
| 20 | + python create_job_from_ad_hoc.py --project-id <project-id> --location <location> --input-uri <uri> --output-uri <uri> |
| 21 | +""" |
| 22 | + |
| 23 | +import argparse |
| 24 | + |
| 25 | +from google.cloud.video import transcoder_v1beta1 |
| 26 | +from google.cloud.video.transcoder_v1beta1.services.transcoder_service import ( |
| 27 | + TranscoderServiceClient, |
| 28 | +) |
| 29 | + |
| 30 | +# [START transcoder_create_job_from_ad_hoc] |
| 31 | + |
| 32 | + |
| 33 | +def create_job_from_ad_hoc(project_id, location, input_uri, output_uri): |
| 34 | + """Creates a job based on an ad-hoc job configuration. |
| 35 | +
|
| 36 | + Args: |
| 37 | + project_id: The GCP project ID. |
| 38 | + location: The location to start the job in. |
| 39 | + input_uri: Uri of the video in the Cloud Storage bucket. |
| 40 | + output_uri: Uri of the video output folder in the Cloud Storage bucket.""" |
| 41 | + |
| 42 | + client = TranscoderServiceClient() |
| 43 | + |
| 44 | + parent = f"projects/{project_id}/locations/{location}" |
| 45 | + job = transcoder_v1beta1.types.Job() |
| 46 | + job.input_uri = input_uri |
| 47 | + job.output_uri = output_uri |
| 48 | + job.config = transcoder_v1beta1.types.JobConfig( |
| 49 | + elementary_streams=[ |
| 50 | + transcoder_v1beta1.types.ElementaryStream( |
| 51 | + key="video-stream0", |
| 52 | + video_stream=transcoder_v1beta1.types.VideoStream( |
| 53 | + codec="h264", |
| 54 | + height_pixels=360, |
| 55 | + width_pixels=640, |
| 56 | + bitrate_bps=550000, |
| 57 | + frame_rate=60, |
| 58 | + ), |
| 59 | + ), |
| 60 | + transcoder_v1beta1.types.ElementaryStream( |
| 61 | + key="video-stream1", |
| 62 | + video_stream=transcoder_v1beta1.types.VideoStream( |
| 63 | + codec="h264", |
| 64 | + height_pixels=720, |
| 65 | + width_pixels=1280, |
| 66 | + bitrate_bps=2500000, |
| 67 | + frame_rate=60, |
| 68 | + ), |
| 69 | + ), |
| 70 | + transcoder_v1beta1.types.ElementaryStream( |
| 71 | + key="audio-stream0", |
| 72 | + audio_stream=transcoder_v1beta1.types.AudioStream( |
| 73 | + codec="aac", bitrate_bps=64000 |
| 74 | + ), |
| 75 | + ), |
| 76 | + ], |
| 77 | + mux_streams=[ |
| 78 | + transcoder_v1beta1.types.MuxStream( |
| 79 | + key="sd", |
| 80 | + container="mp4", |
| 81 | + elementary_streams=["video-stream0", "audio-stream0"], |
| 82 | + ), |
| 83 | + transcoder_v1beta1.types.MuxStream( |
| 84 | + key="hd", |
| 85 | + container="mp4", |
| 86 | + elementary_streams=["video-stream1", "audio-stream0"], |
| 87 | + ), |
| 88 | + ], |
| 89 | + ) |
| 90 | + response = client.create_job(parent=parent, job=job) |
| 91 | + print(f"Job: {response.name}") |
| 92 | + return response |
| 93 | + |
| 94 | + |
| 95 | +# [END transcoder_create_job_from_ad_hoc] |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + parser = argparse.ArgumentParser() |
| 99 | + parser.add_argument("--project-id", help="Your Cloud project ID.", required=True) |
| 100 | + parser.add_argument( |
| 101 | + "--location", |
| 102 | + help="The location to start this job in.", |
| 103 | + default="us-central1", |
| 104 | + ) |
| 105 | + parser.add_argument( |
| 106 | + "--input_uri", |
| 107 | + help="Uri of the video in the Cloud Storage bucket.", |
| 108 | + required=True, |
| 109 | + ) |
| 110 | + parser.add_argument( |
| 111 | + "--output_uri", |
| 112 | + help="Uri of the video output folder in the Cloud Storage bucket. Must end in '/'.", |
| 113 | + required=True, |
| 114 | + ) |
| 115 | + args = parser.parse_args() |
| 116 | + create_job_from_ad_hoc( |
| 117 | + args.project_id, |
| 118 | + args.location, |
| 119 | + args.input_uri, |
| 120 | + args.output_uri, |
| 121 | + ) |
0 commit comments