|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2023 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 in batch mode based on a |
| 18 | + job preset. |
| 19 | +
|
| 20 | +Example usage: |
| 21 | + python create_job_from_preset_batch_mode.py --project_id <project-id> --location <location> --input_uri <uri> --output_uri <uri> [--preset <preset>] |
| 22 | +""" |
| 23 | + |
| 24 | +# [START transcoder_create_job_from_preset_batch_mode] |
| 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_from_preset_batch_mode( |
| 35 | + project_id: str, |
| 36 | + location: str, |
| 37 | + input_uri: str, |
| 38 | + output_uri: str, |
| 39 | + preset: str, |
| 40 | +) -> transcoder_v1.types.resources.Job: |
| 41 | + """Creates a job in batch mode based on a job preset. |
| 42 | +
|
| 43 | + Args: |
| 44 | + project_id: The GCP project ID. |
| 45 | + location: The location to start the job in. |
| 46 | + input_uri: Uri of the video in the Cloud Storage bucket. |
| 47 | + output_uri: Uri of the video output folder in the Cloud Storage bucket. |
| 48 | + preset: The preset template (for example, 'preset/web-hd'). |
| 49 | +
|
| 50 | + Returns: |
| 51 | + The job resource. |
| 52 | + """ |
| 53 | + |
| 54 | + client = TranscoderServiceClient() |
| 55 | + |
| 56 | + parent = f"projects/{project_id}/locations/{location}" |
| 57 | + job = transcoder_v1.types.Job() |
| 58 | + job.input_uri = input_uri |
| 59 | + job.output_uri = output_uri |
| 60 | + job.template_id = preset |
| 61 | + job.mode = transcoder_v1.types.Job.ProcessingMode.PROCESSING_MODE_BATCH |
| 62 | + job.batch_mode_priority = 10 |
| 63 | + |
| 64 | + response = client.create_job(parent=parent, job=job) |
| 65 | + print(f"Job: {response.name}") |
| 66 | + return response |
| 67 | + |
| 68 | + |
| 69 | +# [END transcoder_create_job_from_preset_batch_mode] |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + parser = argparse.ArgumentParser() |
| 73 | + parser.add_argument("--project_id", help="Your Cloud project ID.", required=True) |
| 74 | + parser.add_argument( |
| 75 | + "--location", |
| 76 | + help="The location to start this job in.", |
| 77 | + default="us-central1", |
| 78 | + ) |
| 79 | + parser.add_argument( |
| 80 | + "--input_uri", |
| 81 | + help="Uri of the video in the Cloud Storage bucket.", |
| 82 | + required=True, |
| 83 | + ) |
| 84 | + parser.add_argument( |
| 85 | + "--output_uri", |
| 86 | + help="Uri of the video output folder in the Cloud Storage bucket. Must end in '/'.", |
| 87 | + required=True, |
| 88 | + ) |
| 89 | + parser.add_argument( |
| 90 | + "--preset", |
| 91 | + help="The preset template (for example, 'preset/web-hd').", |
| 92 | + default="preset/web-hd", |
| 93 | + ) |
| 94 | + args = parser.parse_args() |
| 95 | + create_job_from_preset_batch_mode( |
| 96 | + args.project_id, |
| 97 | + args.location, |
| 98 | + args.input_uri, |
| 99 | + args.output_uri, |
| 100 | + args.preset, |
| 101 | + ) |
0 commit comments