Skip to content

Commit 2e85463

Browse files
authored
docs(samples): add batch mode sample and test (GoogleCloudPlatform#10458)
* docs(samples): add batch mode sample and test * add typehinting to arguments and returned values * use return value to verify a delete operation succeeded
1 parent 4d124c0 commit 2e85463

22 files changed

+462
-244
lines changed

video/transcoder/create_job_from_ad_hoc.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,23 @@
3030
)
3131

3232

33-
def create_job_from_ad_hoc(project_id, location, input_uri, output_uri):
33+
def create_job_from_ad_hoc(
34+
project_id: str,
35+
location: str,
36+
input_uri: str,
37+
output_uri: str,
38+
) -> transcoder_v1.types.resources.Job:
3439
"""Creates a job based on an ad-hoc job configuration.
3540
3641
Args:
3742
project_id: The GCP project ID.
3843
location: The location to start the job in.
3944
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."""
45+
output_uri: Uri of the video output folder in the Cloud Storage bucket.
46+
47+
Returns:
48+
The job resource.
49+
"""
4150

4251
client = TranscoderServiceClient()
4352

video/transcoder/create_job_from_preset.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,25 @@
3030
)
3131

3232

33-
def create_job_from_preset(project_id, location, input_uri, output_uri, preset):
33+
def create_job_from_preset(
34+
project_id: str,
35+
location: str,
36+
input_uri: str,
37+
output_uri: str,
38+
preset: str,
39+
) -> transcoder_v1.types.resources.Job:
3440
"""Creates a job based on a job preset.
3541
3642
Args:
3743
project_id: The GCP project ID.
3844
location: The location to start the job in.
3945
input_uri: Uri of the video in the Cloud Storage bucket.
4046
output_uri: Uri of the video output folder in the Cloud Storage bucket.
41-
preset: The preset template (for example, 'preset/web-hd')."""
47+
preset: The preset template (for example, 'preset/web-hd').
48+
49+
Returns:
50+
The job resource.
51+
"""
4252

4353
client = TranscoderServiceClient()
4454

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
)

video/transcoder/create_job_from_template.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,25 @@
3030
)
3131

3232

33-
def create_job_from_template(project_id, location, input_uri, output_uri, template_id):
33+
def create_job_from_template(
34+
project_id: str,
35+
location: str,
36+
input_uri: str,
37+
output_uri: str,
38+
template_id: str,
39+
) -> transcoder_v1.types.resources.Job:
3440
"""Creates a job based on a job template.
3541
3642
Args:
3743
project_id: The GCP project ID.
3844
location: The location to start the job in.
3945
input_uri: Uri of the video in the Cloud Storage bucket.
4046
output_uri: Uri of the video output folder in the Cloud Storage bucket.
41-
template_id: The user-defined template ID."""
47+
template_id: The user-defined template ID.
48+
49+
Returns:
50+
The job resource.
51+
"""
4252

4353
client = TranscoderServiceClient()
4454

video/transcoder/create_job_template.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@
3030
)
3131

3232

33-
def create_job_template(project_id, location, template_id):
33+
def create_job_template(
34+
project_id: str,
35+
location: str,
36+
template_id: str,
37+
) -> transcoder_v1.types.resources.JobTemplate:
3438
"""Creates a job template.
3539
3640
Args:
3741
project_id: The GCP project ID.
3842
location: The location to store this template in.
39-
template_id: The user-defined template ID."""
43+
template_id: The user-defined template ID.
44+
45+
Returns:
46+
The job template resource.
47+
"""
4048

4149
client = TranscoderServiceClient()
4250

video/transcoder/create_job_with_animated_overlay.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,24 @@
3232

3333

3434
def create_job_with_animated_overlay(
35-
project_id, location, input_uri, overlay_image_uri, output_uri
36-
):
35+
project_id: str,
36+
location: str,
37+
input_uri: str,
38+
overlay_image_uri: str,
39+
output_uri: str,
40+
) -> transcoder_v1.types.resources.Job:
3741
"""Creates a job based on an ad-hoc job configuration that includes an animated image overlay.
3842
3943
Args:
4044
project_id: The GCP project ID.
4145
location: The location to start the job in.
4246
input_uri: Uri of the video in the Cloud Storage bucket.
4347
overlay_image_uri: Uri of the image for the overlay in the Cloud Storage bucket.
44-
output_uri: Uri of the video output folder in the Cloud Storage bucket."""
48+
output_uri: Uri of the video output folder in the Cloud Storage bucket.
49+
50+
Returns:
51+
The job resource.
52+
"""
4553

4654
client = TranscoderServiceClient()
4755

video/transcoder/create_job_with_concatenated_inputs.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@
3535

3636

3737
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-
):
38+
project_id: str,
39+
location: str,
40+
input1_uri: str,
41+
start_time_input1: str,
42+
end_time_input1: str,
43+
input2_uri: str,
44+
start_time_input2: str,
45+
end_time_input2: str,
46+
output_uri: str,
47+
) -> transcoder_v1.types.resources.Job:
4848
"""Creates a job based on an ad-hoc job configuration that concatenates two input videos.
4949
5050
Args:
@@ -61,7 +61,11 @@ def create_job_with_concatenated_inputs(
6161
end_time_input2 (str): End time, in fractional seconds ending in 's'
6262
(e.g., '15s'), relative to the second input video timeline.
6363
output_uri (str): Uri of the video output folder in the Cloud Storage
64-
bucket."""
64+
bucket.
65+
66+
Returns:
67+
The job resource.
68+
"""
6569

6670
s1 = duration.Duration()
6771
s1.FromJsonString(start_time_input1)

video/transcoder/create_job_with_embedded_captions.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232

3333

3434
def create_job_with_embedded_captions(
35-
project_id,
36-
location,
37-
input_video_uri,
38-
input_captions_uri,
39-
output_uri,
40-
):
35+
project_id: str,
36+
location: str,
37+
input_video_uri: str,
38+
input_captions_uri: str,
39+
output_uri: str,
40+
) -> transcoder_v1.types.resources.Job:
4141
"""Creates a job based on an ad-hoc job configuration that embeds closed captions in the output video.
4242
4343
Args:
@@ -48,7 +48,11 @@ def create_job_with_embedded_captions(
4848
input_captions_uri (str): Uri of the input captions file in the Cloud
4949
Storage bucket.
5050
output_uri (str): Uri of the video output folder in the Cloud Storage
51-
bucket."""
51+
bucket.
52+
53+
Returns:
54+
The job resource.
55+
"""
5256

5357
client = TranscoderServiceClient()
5458

video/transcoder/create_job_with_periodic_images_spritesheet.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,22 @@
3232

3333

3434
def create_job_with_periodic_images_spritesheet(
35-
project_id, location, input_uri, output_uri
36-
):
35+
project_id: str,
36+
location: str,
37+
input_uri: str,
38+
output_uri: str,
39+
) -> transcoder_v1.types.resources.Job:
3740
"""Creates a job based on an ad-hoc job configuration that generates two spritesheets.
3841
3942
Args:
4043
project_id: The GCP project ID.
4144
location: The location to start the job in.
4245
input_uri: Uri of the video in the Cloud Storage bucket.
43-
output_uri: Uri of the video output folder in the Cloud Storage bucket."""
46+
output_uri: Uri of the video output folder in the Cloud Storage bucket.
47+
48+
Returns:
49+
The job resource.
50+
"""
4451

4552
client = TranscoderServiceClient()
4653

video/transcoder/create_job_with_set_number_images_spritesheet.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@
3131

3232

3333
def create_job_with_set_number_images_spritesheet(
34-
project_id, location, input_uri, output_uri
35-
):
34+
project_id: str,
35+
location: str,
36+
input_uri: str,
37+
output_uri: str,
38+
) -> transcoder_v1.types.resources.Job:
3639
"""Creates a job based on an ad-hoc job configuration that generates two spritesheets.
3740
3841
Args:
3942
project_id: The GCP project ID.
4043
location: The location to start the job in.
4144
input_uri: Uri of the video in the Cloud Storage bucket.
42-
output_uri: Uri of the video output folder in the Cloud Storage bucket."""
45+
output_uri: Uri of the video output folder in the Cloud Storage bucket.
46+
47+
Returns:
48+
The job resource.
49+
"""
4350

4451
client = TranscoderServiceClient()
4552

0 commit comments

Comments
 (0)