Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

chore: move samples from GoogleCloudPlatform/python-docs-samples #125

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ Windows
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-cloud-video-transcoder


Next Steps
~~~~~~~~~~

- See the [Samples](./samples/snippets/README.md).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this link doesn't seem to show up properly, but feel free to address this in a different PR.

- Read the `Client Library Documentation`_ for Cloud Transcoder API
API to see other available methods on the client.
- Read the `Transcoder API Product documentation`_ to learn
Expand Down
40 changes: 40 additions & 0 deletions samples/snippets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Transcoder API Python Samples

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/).

## Setup

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).

For more information on authentication, refer to the
[Authentication Getting Started Guide](https://cloud.google.com/docs/authentication/getting-started).

## Install Dependencies

1. Clone python-docs-samples and change directory to the sample directory you want to use.

$ git clone https://github.com/googleapis/python-video-transcoder.git

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.

1. Create a virtualenv. Samples are compatible with Python 3.6+.

$ virtualenv env
$ source env/bin/activate

1. Install the dependencies needed to run the samples.

$ pip install -r requirements.txt

## The client library

This sample uses the [Google Cloud Client Library for Python](https://googlecloudplatform.github.io/google-cloud-python/).
You can read the documentation for more details on API usage and use GitHub
to [browse the source](https://github.com/GoogleCloudPlatform/google-cloud-python) and [report issues](https://github.com/GoogleCloudPlatform/google-cloud-python/issues).

## Testing

Make sure to enable the Transcoder API on the test project. Set the following environment variables:

* `GOOGLE_CLOUD_PROJECT`
* `GOOGLE_CLOUD_PROJECT_NUMBER`
118 changes: 118 additions & 0 deletions samples/snippets/create_job_from_ad_hoc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Cloud Transcoder sample for creating a job based on a supplied job config.

Example usage:
python create_job_from_ad_hoc.py --project_id <project-id> --location <location> --input_uri <uri> --output_uri <uri>
"""

# [START transcoder_create_job_from_ad_hoc]

import argparse

from google.cloud.video import transcoder_v1
from google.cloud.video.transcoder_v1.services.transcoder_service import (
TranscoderServiceClient,
)


def create_job_from_ad_hoc(project_id, location, input_uri, output_uri):
"""Creates a job based on an ad-hoc job configuration.

Args:
project_id: The GCP project ID.
location: The location to start the job in.
input_uri: Uri of the video in the Cloud Storage bucket.
output_uri: Uri of the video output folder in the Cloud Storage bucket."""

client = TranscoderServiceClient()

parent = f"projects/{project_id}/locations/{location}"
job = transcoder_v1.types.Job()
job.input_uri = input_uri
job.output_uri = output_uri
job.config = transcoder_v1.types.JobConfig(
elementary_streams=[
transcoder_v1.types.ElementaryStream(
key="video-stream0",
video_stream=transcoder_v1.types.VideoStream(
h264=transcoder_v1.types.VideoStream.H264CodecSettings(
height_pixels=360,
width_pixels=640,
bitrate_bps=550000,
frame_rate=60,
),
),
),
transcoder_v1.types.ElementaryStream(
key="video-stream1",
video_stream=transcoder_v1.types.VideoStream(
h264=transcoder_v1.types.VideoStream.H264CodecSettings(
height_pixels=720,
width_pixels=1280,
bitrate_bps=2500000,
frame_rate=60,
),
),
),
transcoder_v1.types.ElementaryStream(
key="audio-stream0",
audio_stream=transcoder_v1.types.AudioStream(
codec="aac", bitrate_bps=64000
),
),
],
mux_streams=[
transcoder_v1.types.MuxStream(
key="sd",
container="mp4",
elementary_streams=["video-stream0", "audio-stream0"],
),
transcoder_v1.types.MuxStream(
key="hd",
container="mp4",
elementary_streams=["video-stream1", "audio-stream0"],
),
],
)
response = client.create_job(parent=parent, job=job)
print(f"Job: {response.name}")
return response


# [END transcoder_create_job_from_ad_hoc]

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
parser.add_argument(
"--location", help="The location to start this job in.", default="us-central1",
)
parser.add_argument(
"--input_uri",
help="Uri of the video in the Cloud Storage bucket.",
required=True,
)
parser.add_argument(
"--output_uri",
help="Uri of the video output folder in the Cloud Storage bucket. Must end in '/'.",
required=True,
)
args = parser.parse_args()
create_job_from_ad_hoc(
args.project_id, args.location, args.input_uri, args.output_uri,
)
82 changes: 82 additions & 0 deletions samples/snippets/create_job_from_preset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Cloud Transcoder sample for creating a job based on a job preset.

Example usage:
python create_job_from_preset.py --project_id <project-id> --location <location> --input_uri <uri> --output_uri <uri> [--preset <preset>]
"""

# [START transcoder_create_job_from_preset]

import argparse

from google.cloud.video import transcoder_v1
from google.cloud.video.transcoder_v1.services.transcoder_service import (
TranscoderServiceClient,
)


def create_job_from_preset(project_id, location, input_uri, output_uri, preset):
"""Creates a job based on a job preset.

Args:
project_id: The GCP project ID.
location: The location to start the job in.
input_uri: Uri of the video in the Cloud Storage bucket.
output_uri: Uri of the video output folder in the Cloud Storage bucket.
preset: The preset template (for example, 'preset/web-hd')."""

client = TranscoderServiceClient()

parent = f"projects/{project_id}/locations/{location}"
job = transcoder_v1.types.Job()
job.input_uri = input_uri
job.output_uri = output_uri
job.template_id = preset

response = client.create_job(parent=parent, job=job)
print(f"Job: {response.name}")
return response


# [END transcoder_create_job_from_preset]

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
parser.add_argument(
"--location", help="The location to start this job in.", default="us-central1",
)
parser.add_argument(
"--input_uri",
help="Uri of the video in the Cloud Storage bucket.",
required=True,
)
parser.add_argument(
"--output_uri",
help="Uri of the video output folder in the Cloud Storage bucket. Must end in '/'.",
required=True,
)
parser.add_argument(
"--preset",
help="The preset template (for example, 'preset/web-hd').",
default="preset/web-hd",
)
args = parser.parse_args()
create_job_from_preset(
args.project_id, args.location, args.input_uri, args.output_uri, args.preset,
)
86 changes: 86 additions & 0 deletions samples/snippets/create_job_from_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Cloud Transcoder sample for creating a job based on a job template.

Example usage:
python create_job_from_template.py --project_id <project-id> --location <location> --input_uri <uri> --output_uri <uri> --template_id <template-id>
"""

# [START transcoder_create_job_from_template]

import argparse

from google.cloud.video import transcoder_v1
from google.cloud.video.transcoder_v1.services.transcoder_service import (
TranscoderServiceClient,
)


def create_job_from_template(project_id, location, input_uri, output_uri, template_id):
"""Creates a job based on a job template.

Args:
project_id: The GCP project ID.
location: The location to start the job in.
input_uri: Uri of the video in the Cloud Storage bucket.
output_uri: Uri of the video output folder in the Cloud Storage bucket.
template_id: The user-defined template ID."""

client = TranscoderServiceClient()

parent = f"projects/{project_id}/locations/{location}"
job = transcoder_v1.types.Job()
job.input_uri = input_uri
job.output_uri = output_uri
job.template_id = template_id

response = client.create_job(parent=parent, job=job)
print(f"Job: {response.name}")
return response


# [END transcoder_create_job_from_template]

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
parser.add_argument(
"--location", help="The location to start this job in.", default="us-central1",
)
parser.add_argument(
"--input_uri",
help="Uri of the video in the Cloud Storage bucket.",
required=True,
)
parser.add_argument(
"--output_uri",
help="Uri of the video output folder in the Cloud Storage bucket. Must end in '/'.",
required=True,
)
parser.add_argument(
"--template_id",
help="The job template ID. The template must be located in the same location as the job.",
required=True,
)
args = parser.parse_args()
create_job_from_template(
args.project_id,
args.location,
args.input_uri,
args.output_uri,
args.template_id,
)
Loading