Skip to content

Commit f0a5cbe

Browse files
irataxyengelke
andauthored
feat(samples): add Transcoder API samples (GoogleCloudPlatform#4927)
* feat(samples): add Transcoder API samples * add README * Add README * Update CODEOWNERS * address feedback. * Remove dependency on the resource manager API for testing. Co-authored-by: Charles Engelke <engelke@google.com>
1 parent bc643a4 commit f0a5cbe

19 files changed

+1110
-1
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
/kms/**/*.py @GoogleCloudPlatform/python-samples-owners
4646
/kubernetes_engine/**/*.py @GoogleCloudPlatform/python-samples-owners
4747
/language/**/*.py @telpirion @sirtorry @GoogleCloudPlatform/python-samples-owners
48-
/logging/**/*.py @GoogleCloudPlatform/observability-devx @GoogleCloudPlatform/python-samples-owners
48+
/media/**/*.py @irataxy @GoogleCloudPlatform/python-samples-owners
4949
/memorystore/**/*.py @GoogleCloudPlatform/python-samples-owners
5050
/ml_engine/**/*.py @alecglassford @GoogleCloudPlatform/python-samples-owners
5151
/monitoring/**/*.py @GoogleCloudPlatform/python-samples-owners

media/testdata/ChromeCast.mp4

9.81 MB
Binary file not shown.

media/transcoder/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Transcoder API Python Samples
2+
3+
This directory contains samples for the Transcoder API. Use this API to transcode videos into a variety of formats. The Transcoder API benefits broadcasters, production companies, businesses, and individuals looking to transform their video content for use across a variety of user devices. For more information, see the [Transcoder API documentation](https://cloud.google.com/transcoder/).
4+
5+
## Setup
6+
7+
To run the samples, you need to first follow the steps in [Before you begin](https://cloud.google.com/transcoder/docs/how-to/before-you-begin).
8+
9+
For more information on authentication, refer to the
10+
[Authentication Getting Started Guide](https://cloud.google.com/docs/authentication/getting-started).
11+
12+
## Install Dependencies
13+
14+
1. Clone python-docs-samples and change directory to the sample directory you want to use.
15+
16+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
17+
18+
1. Install [pip](https://pip.pypa.io/) and [virtualenv](https://virtualenv.pypa.io/) if you do not already have them. You may want to refer to the [Python Development Environment Setup Guide](https://cloud.google.com/python/setup) for Google Cloud Platform for instructions.
19+
20+
1. Create a virtualenv. Samples are compatible with Python 3.6+.
21+
22+
$ virtualenv env
23+
$ source env/bin/activate
24+
25+
1. Install the dependencies needed to run the samples.
26+
27+
$ pip install -r requirements.txt
28+
29+
## The client library
30+
31+
This sample uses the [Google Cloud Client Library for Python](https://googlecloudplatform.github.io/google-cloud-python/).
32+
You can read the documentation for more details on API usage and use GitHub
33+
to [browse the source](https://github.com/GoogleCloudPlatform/google-cloud-python) and [report issues](https://github.com/GoogleCloudPlatform/google-cloud-python/issues).
34+
35+
## Testing
36+
37+
Make sure to enable the Transcoder API on the test project. Set the following environment variables:
38+
39+
* `GOOGLE_CLOUD_PROJECT`
40+
* `GOOGLE_CLOUD_PROJECT_NUMBER`
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 job preset.
18+
19+
Example usage:
20+
python create_job_from_preset.py --project-id <project-id> --location <location> --input-uri <uri> --output-uri <uri> [--preset <preset>]
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_preset]
31+
32+
33+
def create_job_from_preset(project_id, location, input_uri, output_uri, preset):
34+
"""Creates a job based on a job preset.
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+
preset: The preset template."""
42+
43+
client = TranscoderServiceClient()
44+
45+
parent = f"projects/{project_id}/locations/{location}"
46+
job = transcoder_v1beta1.types.Job()
47+
job.input_uri = input_uri
48+
job.output_uri = output_uri
49+
job.template_id = preset
50+
51+
response = client.create_job(parent=parent, job=job)
52+
print(f"Job: {response.name}")
53+
return response
54+
55+
56+
# [END transcoder_create_job_from_preset]
57+
58+
if __name__ == "__main__":
59+
parser = argparse.ArgumentParser()
60+
parser.add_argument("--project-id", help="Your Cloud project ID.", required=True)
61+
parser.add_argument(
62+
"--location",
63+
help="The location to start this job in.",
64+
default="us-central1",
65+
)
66+
parser.add_argument(
67+
"--input_uri",
68+
help="Uri of the video in the Cloud Storage bucket.",
69+
required=True,
70+
)
71+
parser.add_argument(
72+
"--output_uri",
73+
help="Uri of the video output folder in the Cloud Storage bucket. Must end in '/'.",
74+
required=True,
75+
)
76+
parser.add_argument(
77+
"--preset",
78+
help="The preset template.",
79+
default="preset/web-hd",
80+
)
81+
args = parser.parse_args()
82+
create_job_from_preset(
83+
args.project_id,
84+
args.location,
85+
args.input_uri,
86+
args.output_uri,
87+
args.preset,
88+
)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 job template.
18+
19+
Example usage:
20+
python create_job_from_template.py --project-id <project-id> --location <location> --input-uri <uri> --output-uri <uri> --template-id <template-id>
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_template]
31+
32+
33+
def create_job_from_template(project_id, location, input_uri, output_uri, template_id):
34+
"""Creates a job based on a job template.
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+
template_id: The user-defined template ID."""
42+
43+
client = TranscoderServiceClient()
44+
45+
parent = f"projects/{project_id}/locations/{location}"
46+
job = transcoder_v1beta1.types.Job()
47+
job.input_uri = input_uri
48+
job.output_uri = output_uri
49+
job.template_id = template_id
50+
51+
response = client.create_job(parent=parent, job=job)
52+
print(f"Job: {response.name}")
53+
return response
54+
55+
56+
# [END transcoder_create_job_from_template]
57+
58+
if __name__ == "__main__":
59+
parser = argparse.ArgumentParser()
60+
parser.add_argument("--project-id", help="Your Cloud project ID.", required=True)
61+
parser.add_argument(
62+
"--location",
63+
help="The location to start this job in.",
64+
default="us-central1",
65+
)
66+
parser.add_argument(
67+
"--input_uri",
68+
help="Uri of the video in the Cloud Storage bucket.",
69+
required=True,
70+
)
71+
parser.add_argument(
72+
"--output_uri",
73+
help="Uri of the video output folder in the Cloud Storage bucket. Must end in '/'.",
74+
required=True,
75+
)
76+
parser.add_argument(
77+
"--template-id",
78+
help="The job template ID. The template must be located in the same location as the job.",
79+
required=True,
80+
)
81+
args = parser.parse_args()
82+
create_job_from_template(
83+
args.project_id,
84+
args.location,
85+
args.input_uri,
86+
args.output_uri,
87+
args.template_id,
88+
)

0 commit comments

Comments
 (0)