diff --git a/packages/google-cloud-video-stitcher/README.rst b/packages/google-cloud-video-stitcher/README.rst index 55cf1a4308f3..530c29039aa3 100644 --- a/packages/google-cloud-video-stitcher/README.rst +++ b/packages/google-cloud-video-stitcher/README.rst @@ -73,6 +73,7 @@ Windows Next Steps ~~~~~~~~~~ +- See the `Samples`_. - Read the `Client Library Documentation`_ for Video Stitcher to see other available methods on the client. - Read the `Video Stitcher Product documentation`_ to learn @@ -80,5 +81,6 @@ Next Steps - View this `README`_ to see the full list of Cloud APIs that we cover. +.. _Samples: https://github.com/googleapis/python-video-stitcher/blob/main/samples/snippets/README.md .. _Video Stitcher Product documentation: https://cloud.google.com/video-stitcher/docs .. _README: https://github.com/googleapis/google-cloud-python/blob/main/README.rst \ No newline at end of file diff --git a/packages/google-cloud-video-stitcher/samples/snippets/README.md b/packages/google-cloud-video-stitcher/samples/snippets/README.md new file mode 100644 index 000000000000..2d2dda050594 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/README.md @@ -0,0 +1,45 @@ +# Video Stitcher API Python Samples + +This directory contains samples for the Video Stitcher API. Use this API to +generate dynamic content for delivery to client devices. You can call the Video +Stitcher API from your servers to dynamically insert ads into video-on-demand +and live streams for your users. For more information, see the +[Video Stitcher API documentation](https://cloud.google.com/video-stitcher/). + +## Setup + +To run the samples, you need to first follow the steps in +[Before you begin](https://cloud.google.com/video-stitcher/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-video-stitcher and change directories to the sample directory +you want to use. + + $ git clone https://github.com/googleapis/python-video-stitcher.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 + +## Testing + +Make sure to enable the Video Stitcher API on the test project. Set the +following environment variables: + +* `GOOGLE_CLOUD_PROJECT` +* `GOOGLE_CLOUD_PROJECT_NUMBER` \ No newline at end of file diff --git a/packages/google-cloud-video-stitcher/samples/snippets/cdn_key_test.py b/packages/google-cloud-video-stitcher/samples/snippets/cdn_key_test.py new file mode 100644 index 000000000000..86de02b2d721 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/cdn_key_test.py @@ -0,0 +1,142 @@ +# Copyright 2022 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. + +import os +import uuid + +from google.api_core.exceptions import NotFound +import pytest + +import create_cdn_key +import delete_cdn_key +import get_cdn_key +import list_cdn_keys +import update_cdn_key + +location = "us-central1" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +gcdn_cdn_key_id = f"my-python-test-cdn-key-{uuid.uuid4()}" +akamai_cdn_key_id = f"my-python-test-cdn-key-{uuid.uuid4()}" + +hostname = "cdn.example.com" +updated_hostname = "updated.example.com" + +gcdn_key_name = "gcdn-key" +gcdn_private_key = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg==" +updated_gcdn_private_key = "VGhpcyBpcyBhbiB1cGRhdGVkIHRlc3Qgc3RyaW5nLg==" +akamai_key = gcdn_private_key +updated_akamai_key = updated_gcdn_private_key + + +def test_cdn_key_operations(capsys: pytest.fixture) -> None: + + try: + delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id) + except NotFound as e: + print(f"Ignoring NotFound, details: {e}") + out, _ = capsys.readouterr() + + try: + delete_cdn_key.delete_cdn_key(project_id, location, akamai_cdn_key_id) + except NotFound as e: + print(f"Ignoring NotFound, details: {e}") + out, _ = capsys.readouterr() + + # GCDN CDN key tests + + create_cdn_key.create_cdn_key( + project_id, + location, + gcdn_cdn_key_id, + hostname, + gcdn_key_name, + gcdn_private_key, + ) + out, _ = capsys.readouterr() + assert gcdn_cdn_key_id in out + + list_cdn_keys.list_cdn_keys(project_id, location) + out, _ = capsys.readouterr() + assert gcdn_cdn_key_id in out + + # Update the hostname only + response = update_cdn_key.update_cdn_key( + project_id, location, gcdn_cdn_key_id, updated_hostname + ) + out, _ = capsys.readouterr() + assert gcdn_cdn_key_id in out + assert updated_hostname in response.hostname + + # Update the private key; the private key value is not returned by the client + response = update_cdn_key.update_cdn_key( + project_id, + location, + gcdn_cdn_key_id, + hostname, + gcdn_key_name, + updated_gcdn_private_key, + ) + out, _ = capsys.readouterr() + assert gcdn_cdn_key_id in out + + get_cdn_key.get_cdn_key(project_id, location, gcdn_cdn_key_id) + out, _ = capsys.readouterr() + assert gcdn_cdn_key_id in out + + delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id) + out, _ = capsys.readouterr() + assert "Deleted CDN key" in out + + # Akamai CDN key tests + + create_cdn_key.create_cdn_key( + project_id, + location, + akamai_cdn_key_id, + hostname, + akamai_token_key=akamai_key, + ) + out, _ = capsys.readouterr() + assert akamai_cdn_key_id in out + + list_cdn_keys.list_cdn_keys(project_id, location) + out, _ = capsys.readouterr() + assert akamai_cdn_key_id in out + + # Update the hostname only + response = update_cdn_key.update_cdn_key( + project_id, location, akamai_cdn_key_id, updated_hostname + ) + out, _ = capsys.readouterr() + assert akamai_cdn_key_id in out + assert updated_hostname in response.hostname + + # Update the private key; the private key value is not returned by the client + response = update_cdn_key.update_cdn_key( + project_id, + location, + akamai_cdn_key_id, + hostname, + akamai_token_key=updated_akamai_key, + ) + out, _ = capsys.readouterr() + assert akamai_cdn_key_id in out + + get_cdn_key.get_cdn_key(project_id, location, akamai_cdn_key_id) + out, _ = capsys.readouterr() + assert akamai_cdn_key_id in out + + delete_cdn_key.delete_cdn_key(project_id, location, akamai_cdn_key_id) + out, _ = capsys.readouterr() + assert "Deleted CDN key" in out diff --git a/packages/google-cloud-video-stitcher/samples/snippets/create_cdn_key.py b/packages/google-cloud-video-stitcher/samples/snippets/create_cdn_key.py new file mode 100644 index 000000000000..b50c607d0933 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/create_cdn_key.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for creating a CDN key. A CDN key is used +to retrieve protected media. +Example usage: + python create_cdn_key.py --project_id --location --cdn_key_id \ + --hostname [--gcdn_keyname --gcdn_private_key | --akamai_token_key ] +""" + +# [START video_stitcher_create_cdn_key] + +import argparse + +from google.cloud.video import stitcher_v1 +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def create_cdn_key( + project_id: str, + location: str, + cdn_key_id: str, + hostname: str, + gcdn_keyname: str = None, + gcdn_private_key: str = None, + akamai_token_key: str = None, +) -> str: + """Creates a Google Cloud or Akamai CDN key. + Args: + project_id: The GCP project ID. + location: The location in which to create the CDN key. + cdn_key_id: The user-defined CDN key ID. + hostname: The hostname to which this CDN key applies. + gcdn_keyname: Applies to a Google Cloud CDN key. A base64-encoded string secret. + gcdn_private_key: Applies to a Google Cloud CDN key. Public name of the key. + akamai_token_key: Applies to an Akamai CDN key. A base64-encoded string token key.""" + + client = VideoStitcherServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + + cdn_key = stitcher_v1.types.CdnKey( + name=cdn_key_id, + hostname=hostname, + ) + + if akamai_token_key is not None: + cdn_key.akamai_cdn_key = stitcher_v1.types.AkamaiCdnKey( + token_key=akamai_token_key, + ) + elif gcdn_keyname is not None: + cdn_key.google_cdn_key = stitcher_v1.types.GoogleCdnKey( + key_name=gcdn_keyname, + private_key=gcdn_private_key, + ) + + response = client.create_cdn_key( + parent=parent, cdn_key_id=cdn_key_id, cdn_key=cdn_key + ) + print(f"CDN key: {response.name}") + return response + + +# [END video_stitcher_create_cdn_key] + +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 in which to create the CDN key.", + default="us-central1", + ) + parser.add_argument( + "--cdn_key_id", + help="The user-defined CDN key ID.", + required=True, + ) + parser.add_argument( + "--hostname", + help="The hostname to which this CDN key applies.", + required=True, + ) + parser.add_argument( + "--gcdn_keyname", + help="Applies to a Google Cloud CDN key. The base64-encoded string secret.", + ) + parser.add_argument( + "--gcdn_private_key", + help="Applies to a Google Cloud CDN key. Public name of the key.", + ) + parser.add_argument( + "--akamai_token_key", + help="Applies to an Akamai CDN key. The base64-encoded string token key.", + ) + args = parser.parse_args() + create_cdn_key( + args.project_id, + args.location, + args.cdn_key_id, + args.hostname, + args.gcdn_keyname, + args.gcdn_private_key, + args.akamai_token_key, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/create_live_session.py b/packages/google-cloud-video-stitcher/samples/snippets/create_live_session.py new file mode 100644 index 000000000000..1cbf30453ef4 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/create_live_session.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for creating a live stream session in +which to insert ads. +Example usage: + python create_live_session.py --project_id \ + --location --live_stream_uri --ad_tag_uri \ + --slate_id +""" + +# [START video_stitcher_create_live_session] + +import argparse + +from google.cloud.video import stitcher_v1 +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def create_live_session( + project_id: str, location: str, live_stream_uri: str, ad_tag_uri: str, slate_id: str +) -> str: + """Creates a live session. Live sessions are ephemeral resources that expire + after a few minutes. + Args: + project_id: The GCP project ID. + location: The location in which to create the session. + live_stream_uri: Uri of the livestream to stitch; this URI must reference either an MPEG-DASH + manifest (.mpd) file or an M3U playlist manifest (.m3u8) file. + ad_tag_uri: Uri of the ad tag. + slate_id: The user-defined slate ID of the default slate to use when no slates are specified in an ad break's message.""" + + client = VideoStitcherServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + + # Create dictionaries and pass them to the LiveSession constructor + ad_tag_map = {"default": stitcher_v1.AdTag(uri=ad_tag_uri)} + + live_session = stitcher_v1.types.LiveSession( + source_uri=live_stream_uri, ad_tag_map=ad_tag_map, default_slate_id=slate_id + ) + + response = client.create_live_session(parent=parent, live_session=live_session) + print(f"Live session: {response.name}") + return response + + +# [END video_stitcher_create_live_session] + +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 in which to create the live session.", + default="us-central1", + ) + parser.add_argument( + "--live_stream_uri", + help="The Uri of the livestream to stitch (.mpd or .m3u8 file) in double quotes.", + required=True, + ) + parser.add_argument( + "--ad_tag_uri", + help="Uri of the ad tag in double quotes.", + required=True, + ) + parser.add_argument( + "--slate_id", + help="The user-defined slate ID of the default slate.", + required=True, + ) + args = parser.parse_args() + create_live_session( + args.project_id, + args.location, + args.live_stream_uri, + args.ad_tag_uri, + args.slate_id, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/create_slate.py b/packages/google-cloud-video-stitcher/samples/snippets/create_slate.py new file mode 100644 index 000000000000..87b7f00b8d68 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/create_slate.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for creating a slate. A slate is displayed +when ads are not available. +Example usage: + python create_slate.py --project_id --location \ + --slate_id --slate_uri +""" + +# [START video_stitcher_create_slate] + +import argparse + +from google.cloud.video import stitcher_v1 +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def create_slate(project_id: str, location: str, slate_id: str, slate_uri: str) -> str: + """Creates a slate. + Args: + project_id: The GCP project ID. + location: The location in which to create the slate. + slate_id: The user-defined slate ID. + slate_uri: Uri of the video slate; must be an MP4 video with at least one audio track.""" + + client = VideoStitcherServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + + slate = stitcher_v1.types.Slate( + uri=slate_uri, + ) + + response = client.create_slate(parent=parent, slate_id=slate_id, slate=slate) + print(f"Slate: {response.name}") + return response + + +# [END video_stitcher_create_slate] + +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 in which to create the slate.", + default="us-central1", + ) + parser.add_argument( + "--slate_id", + help="The user-defined slate ID.", + required=True, + ) + parser.add_argument( + "--slate_uri", + help="Uri of the video slate; must be an MP4 video with at least one audio track.", + required=True, + ) + args = parser.parse_args() + create_slate( + args.project_id, + args.location, + args.slate_id, + args.slate_uri, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/create_vod_session.py b/packages/google-cloud-video-stitcher/samples/snippets/create_vod_session.py new file mode 100644 index 000000000000..7806d88aaf89 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/create_vod_session.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for creating a video on demand (VOD) +session in which to insert ads. +Example usage: + python create_vod_session.py --project_id \ + --location --source_uri --ad_tag_uri +""" + +# [START video_stitcher_create_vod_session] + +import argparse + +from google.cloud.video import stitcher_v1 +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def create_vod_session( + project_id: str, location: str, source_uri: str, ad_tag_uri: str +) -> str: + """Creates a VOD session. VOD sessions are ephemeral resources that expire + after a few hours. + Args: + project_id: The GCP project ID. + location: The location in which to create the session. + source_uri: Uri of the media to stitch; this URI must reference either an MPEG-DASH + manifest (.mpd) file or an M3U playlist manifest (.m3u8) file. + ad_tag_uri: Uri of the ad tag.""" + + client = VideoStitcherServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + + vod_session = stitcher_v1.types.VodSession( + source_uri=source_uri, ad_tag_uri=ad_tag_uri + ) + + response = client.create_vod_session(parent=parent, vod_session=vod_session) + print(f"VOD session: {response.name}") + return response + + +# [END video_stitcher_create_vod_session] + +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 in which to create the VOD session.", + default="us-central1", + ) + parser.add_argument( + "--source_uri", + help="The Uri of the media to stitch (.mpd or .m3u8 file) in double quotes.", + required=True, + ) + parser.add_argument( + "--ad_tag_uri", + help="Uri of the ad tag in double quotes.", + required=True, + ) + args = parser.parse_args() + create_vod_session( + args.project_id, + args.location, + args.source_uri, + args.ad_tag_uri, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/delete_cdn_key.py b/packages/google-cloud-video-stitcher/samples/snippets/delete_cdn_key.py new file mode 100644 index 000000000000..4ad11417ab07 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/delete_cdn_key.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for deleting a CDN key. +Example usage: + python delete_cdn_key.py --project_id --location \ + --cdn_key_id +""" + +# [START video_stitcher_delete_cdn_key] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def delete_cdn_key(project_id: str, location: str, cdn_key_id: str) -> str: + """Deletes a CDN key. + Args: + project_id: The GCP project ID. + location: The location of the CDN key. + cdn_key_id: The user-defined CDN key ID.""" + + client = VideoStitcherServiceClient() + + name = f"projects/{project_id}/locations/{location}/cdnKeys/{cdn_key_id}" + response = client.delete_cdn_key(name=name) + print("Deleted CDN key") + return response + + +# [END video_stitcher_delete_cdn_key] + +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 of the CDN key.", + required=True, + ) + parser.add_argument( + "--cdn_key_id", + help="The user-defined CDN key ID.", + required=True, + ) + args = parser.parse_args() + delete_cdn_key( + args.project_id, + args.location, + args.cdn_key_id, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/delete_slate.py b/packages/google-cloud-video-stitcher/samples/snippets/delete_slate.py new file mode 100644 index 000000000000..95a36df82b2e --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/delete_slate.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for deleting a slate. +Example usage: + python delete_slate.py --project_id --location \ + --slate_id +""" + +# [START video_stitcher_delete_slate] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def delete_slate(project_id: str, location: str, slate_id: str) -> str: + """Deletes a slate. + Args: + project_id: The GCP project ID. + location: The location of the slate. + slate_id: The user-defined slate ID.""" + + client = VideoStitcherServiceClient() + + name = f"projects/{project_id}/locations/{location}/slates/{slate_id}" + response = client.delete_slate(name=name) + print("Deleted slate") + return response + + +# [END video_stitcher_delete_slate] + +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 of the slate.", + required=True, + ) + parser.add_argument( + "--slate_id", + help="The user-defined slate ID.", + required=True, + ) + args = parser.parse_args() + delete_slate( + args.project_id, + args.location, + args.slate_id, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/get_cdn_key.py b/packages/google-cloud-video-stitcher/samples/snippets/get_cdn_key.py new file mode 100644 index 000000000000..fc10ddb9cdf1 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/get_cdn_key.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for getting a CDN key. +Example usage: + python get_cdn_key.py --project_id --location \ + --cdn_key_id +""" + +# [START video_stitcher_get_cdn_key] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def get_cdn_key(project_id: str, location: str, cdn_key_id: str) -> str: + """Gets a CDN key. + Args: + project_id: The GCP project ID. + location: The location of the CDN key. + cdn_key_id: The user-defined CDN key ID.""" + + client = VideoStitcherServiceClient() + + name = f"projects/{project_id}/locations/{location}/cdnKeys/{cdn_key_id}" + response = client.get_cdn_key(name=name) + print(f"CDN key: {response.name}") + return response + + +# [END video_stitcher_get_cdn_key] + +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 of the CDN key.", + required=True, + ) + parser.add_argument( + "--cdn_key_id", + help="The user-defined CDN key ID.", + required=True, + ) + args = parser.parse_args() + get_cdn_key( + args.project_id, + args.location, + args.cdn_key_id, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/get_live_ad_tag_detail.py b/packages/google-cloud-video-stitcher/samples/snippets/get_live_ad_tag_detail.py new file mode 100644 index 000000000000..4c5d82841b8b --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/get_live_ad_tag_detail.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for getting the specified ad tag detail +for a live stream session. +Example usage: + python get_live_ad_tag_detail.py --project_id \ + --location --session_id \ + --ad_tag_details_id +""" + +# [START video_stitcher_get_live_ad_tag_detail] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def get_live_ad_tag_detail( + project_id: str, location: str, session_id: str, ad_tag_detail_id: str +) -> str: + """Gets the specified ad tag detail for a live session. + Args: + project_id: The GCP project ID. + location: The location of the session. + session_id: The ID of the live session. + ad_tag_detail_id: The ID of the ad tag details.""" + + client = VideoStitcherServiceClient() + + name = client.live_ad_tag_detail_path( + project_id, location, session_id, ad_tag_detail_id + ) + response = client.get_live_ad_tag_detail(name=name) + print(f"Live ad tag detail: {response.name}") + return response + + +# [END video_stitcher_get_live_ad_tag_detail] + +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 of the live session.", required=True + ) + parser.add_argument( + "--session_id", help="The ID of the live session.", required=True + ) + parser.add_argument( + "--ad_tag_detail_id", help="The ID of the ad tag details.", required=True + ) + args = parser.parse_args() + get_live_ad_tag_detail( + args.project_id, args.location, args.session_id, args.ad_tag_detail_id + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/get_live_session.py b/packages/google-cloud-video-stitcher/samples/snippets/get_live_session.py new file mode 100644 index 000000000000..c848dddee139 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/get_live_session.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for getting a live stream session. +Example usage: + python get_live_session.py --project_id --location \ + --session_id +""" + +# [START video_stitcher_get_live_session] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def get_live_session(project_id: str, location: str, session_id: str) -> str: + """Gets a live session. Live sessions are ephemeral resources that expire + after a few minutes. + Args: + project_id: The GCP project ID. + location: The location of the session. + session_id: The ID of the live session.""" + + client = VideoStitcherServiceClient() + + name = client.live_session_path(project_id, location, session_id) + response = client.get_live_session(name=name) + print(f"Live session: {response.name}") + return response + + +# [END video_stitcher_get_live_session] + +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 of the live session.", required=True + ) + parser.add_argument( + "--session_id", help="The ID of the live session.", required=True + ) + args = parser.parse_args() + get_live_session(args.project_id, args.location, args.session_id) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/get_slate.py b/packages/google-cloud-video-stitcher/samples/snippets/get_slate.py new file mode 100644 index 000000000000..7e2b909e3bab --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/get_slate.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for getting a slate. +Example usage: + python get_slate.py --project_id --location \ + --slate_id +""" + +# [START video_stitcher_get_slate] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def get_slate(project_id: str, location: str, slate_id: str) -> str: + """Gets a slate. + Args: + project_id: The GCP project ID. + location: The location of the slate. + slate_id: The user-defined slate ID.""" + + client = VideoStitcherServiceClient() + + name = f"projects/{project_id}/locations/{location}/slates/{slate_id}" + response = client.get_slate(name=name) + print(f"Slate: {response.name}") + return response + + +# [END video_stitcher_get_slate] + +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 of the slate.", + required=True, + ) + parser.add_argument( + "--slate_id", + help="The user-defined slate ID.", + required=True, + ) + args = parser.parse_args() + get_slate( + args.project_id, + args.location, + args.slate_id, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/get_vod_ad_tag_detail.py b/packages/google-cloud-video-stitcher/samples/snippets/get_vod_ad_tag_detail.py new file mode 100644 index 000000000000..a8b82129e2c7 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/get_vod_ad_tag_detail.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for getting the specified ad tag detail +for a video on demand (VOD) session. +Example usage: + python get_vod_ad_tag_detail.py --project_id --location --session_id --ad_tag_details_id +""" + +# [START video_stitcher_get_vod_ad_tag_detail] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def get_vod_ad_tag_detail( + project_id: str, location: str, session_id: str, ad_tag_detail_id: str +) -> str: + """Gets the specified ad tag detail for a VOD session. + Args: + project_id: The GCP project ID. + location: The location of the session. + session_id: The ID of the VOD session. + ad_tag_detail_id: The ID of the ad tag details.""" + + client = VideoStitcherServiceClient() + + name = client.vod_ad_tag_detail_path( + project_id, location, session_id, ad_tag_detail_id + ) + response = client.get_vod_ad_tag_detail(name=name) + print(f"VOD ad tag detail: {response.name}") + return response + + +# [END video_stitcher_get_vod_ad_tag_detail] + +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 of the VOD session.", required=True + ) + parser.add_argument( + "--session_id", help="The ID of the VOD session.", required=True + ) + parser.add_argument( + "--ad_tag_detail_id", help="The ID of the ad tag details.", required=True + ) + args = parser.parse_args() + get_vod_ad_tag_detail( + args.project_id, args.location, args.session_id, args.ad_tag_detail_id + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/get_vod_session.py b/packages/google-cloud-video-stitcher/samples/snippets/get_vod_session.py new file mode 100644 index 000000000000..a27cda077812 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/get_vod_session.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for getting a video on demand (VOD) +session. +Example usage: + python get_vod_session.py --project_id --location \ + --session_id +""" + +# [START video_stitcher_get_vod_session] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def get_vod_session(project_id: str, location: str, session_id: str) -> str: + """Gets a VOD session. VOD sessions are ephemeral resources that expire + after a few hours. + Args: + project_id: The GCP project ID. + location: The location of the session. + session_id: The ID of the VOD session.""" + + client = VideoStitcherServiceClient() + + name = client.vod_session_path(project_id, location, session_id) + response = client.get_vod_session(name=name) + print(f"VOD session: {response.name}") + return response + + +# [END video_stitcher_get_vod_session] + +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 of the VOD session.", required=True + ) + parser.add_argument( + "--session_id", help="The ID of the VOD session.", required=True + ) + args = parser.parse_args() + get_vod_session(args.project_id, args.location, args.session_id) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/get_vod_stitch_detail.py b/packages/google-cloud-video-stitcher/samples/snippets/get_vod_stitch_detail.py new file mode 100644 index 000000000000..92bb6a9f3d74 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/get_vod_stitch_detail.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for getting the specified stitch detail +for a video on demand (VOD) session. +Example usage: + python get_vod_stitch_detail.py --project_id \ + --location --session_id \ + --stitch_details_id +""" + +# [START video_stitcher_get_vod_stitch_detail] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def get_vod_stitch_detail( + project_id: str, location: str, session_id: str, stitch_detail_id: str +) -> str: + """Gets the specified stitch detail for a VOD session. + Args: + project_id: The GCP project ID. + location: The location of the session. + session_id: The ID of the VOD session. + stitch_detail_id: The ID of the stitch details.""" + + client = VideoStitcherServiceClient() + + name = client.vod_stitch_detail_path( + project_id, location, session_id, stitch_detail_id + ) + response = client.get_vod_stitch_detail(name=name) + print(f"VOD stitch detail: {response.name}") + return response + + +# [END video_stitcher_get_vod_stitch_detail] + +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 of the VOD session.", required=True + ) + parser.add_argument( + "--session_id", help="The ID of the VOD session.", required=True + ) + parser.add_argument( + "--stitch_detail_id", help="The ID of the stitch details.", required=True + ) + args = parser.parse_args() + get_vod_stitch_detail( + args.project_id, args.location, args.session_id, args.stitch_detail_id + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/list_cdn_keys.py b/packages/google-cloud-video-stitcher/samples/snippets/list_cdn_keys.py new file mode 100644 index 000000000000..4db22f204da5 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/list_cdn_keys.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for listing all CDN keys in a location. +Example usage: + python list_cdn_keys.py --project_id --location +""" + +# [START video_stitcher_list_cdn_keys] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def list_cdn_keys(project_id: str, location: str) -> str: + """Lists all CDN keys in a location. + Args: + project_id: The GCP project ID. + location: The location of the CDN keys.""" + + client = VideoStitcherServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + response = client.list_cdn_keys(parent=parent) + print("CDN keys:") + for cdn_key in response.cdn_keys: + print({cdn_key.name}) + + return response + + +# [END video_stitcher_list_cdn_keys] + +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 of the CDN keys.", + required=True, + ) + args = parser.parse_args() + list_cdn_keys( + args.project_id, + args.location, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/list_live_ad_tag_details.py b/packages/google-cloud-video-stitcher/samples/snippets/list_live_ad_tag_details.py new file mode 100644 index 000000000000..769a78907978 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/list_live_ad_tag_details.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for listing the ad tag details for a +live session. +Example usage: + python list_live_ad_tag_details.py --project_id \ + --location --session_id +""" + +# [START video_stitcher_list_live_ad_tag_details] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def list_live_ad_tag_details(project_id: str, location: str, session_id: str) -> str: + """Lists the ad tag details for the specified live session. + Args: + project_id: The GCP project ID. + location: The location of the session. + session_id: The ID of the live session.""" + + client = VideoStitcherServiceClient() + + parent = client.live_session_path(project_id, location, session_id) + page_result = client.list_live_ad_tag_details(parent=parent) + print("Live ad tag details:") + for response in page_result: + print(response) + + return page_result + + +# [END video_stitcher_list_live_ad_tag_details] + +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 of the live session.", required=True + ) + parser.add_argument( + "--session_id", help="The ID of the live session.", required=True + ) + args = parser.parse_args() + list_live_ad_tag_details(args.project_id, args.location, args.session_id) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/list_slates.py b/packages/google-cloud-video-stitcher/samples/snippets/list_slates.py new file mode 100644 index 000000000000..c4a0f6df85e3 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/list_slates.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for listing all slates in a location. +Example usage: + python list_slates.py --project_id --location +""" + +# [START video_stitcher_list_slates] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def list_slates(project_id: str, location: str) -> str: + """Lists all slates in a location. + Args: + project_id: The GCP project ID. + location: The location of the slates.""" + + client = VideoStitcherServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + response = client.list_slates(parent=parent) + print("Slates:") + for slate in response.slates: + print({slate.name}) + + return response + + +# [END video_stitcher_list_slates] + +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 of the slates.", + required=True, + ) + args = parser.parse_args() + list_slates( + args.project_id, + args.location, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/list_vod_ad_tag_details.py b/packages/google-cloud-video-stitcher/samples/snippets/list_vod_ad_tag_details.py new file mode 100644 index 000000000000..f92a923f1b0c --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/list_vod_ad_tag_details.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for listing the ad tag details for a video +on demand (VOD) session. +Example usage: + python list_vod_ad_tag_details.py --project_id \ + --location --session_id +""" + +# [START video_stitcher_list_vod_ad_tag_details] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def list_vod_ad_tag_details(project_id: str, location: str, session_id: str) -> str: + """Lists the ad tag details for the specified VOD session. + Args: + project_id: The GCP project ID. + location: The location of the session. + session_id: The ID of the VOD session.""" + + client = VideoStitcherServiceClient() + + parent = client.vod_session_path(project_id, location, session_id) + page_result = client.list_vod_ad_tag_details(parent=parent) + print("VOD ad tag details:") + for response in page_result: + print(response) + + return response + + +# [END video_stitcher_list_vod_ad_tag_details] + +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 of the VOD session.", required=True + ) + parser.add_argument( + "--session_id", help="The ID of the VOD session.", required=True + ) + args = parser.parse_args() + list_vod_ad_tag_details(args.project_id, args.location, args.session_id) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/list_vod_stitch_details.py b/packages/google-cloud-video-stitcher/samples/snippets/list_vod_stitch_details.py new file mode 100644 index 000000000000..7bfc17992f4c --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/list_vod_stitch_details.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for listing the stitch details for a +video on demand (VOD) session. +Example usage: + python list_vod_stitch_details.py --project_id \ + --location --session_id +""" + +# [START video_stitcher_list_vod_stitch_details] + +import argparse + +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def list_vod_stitch_details(project_id: str, location: str, session_id: str) -> str: + """Lists the stitch details for the specified VOD session. + Args: + project_id: The GCP project ID. + location: The location of the session. + session_id: The ID of the VOD session.""" + + client = VideoStitcherServiceClient() + + parent = client.vod_session_path(project_id, location, session_id) + page_result = client.list_vod_stitch_details(parent=parent) + print("VOD stitch details:") + for response in page_result: + print(response) + + return response + + +# [END video_stitcher_list_vod_stitch_details] + +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 of the VOD session.", required=True + ) + parser.add_argument( + "--session_id", help="The ID of the VOD session.", required=True + ) + args = parser.parse_args() + list_vod_stitch_details(args.project_id, args.location, args.session_id) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/live_session_test.py b/packages/google-cloud-video-stitcher/samples/snippets/live_session_test.py new file mode 100644 index 000000000000..54c8d0e63b8e --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/live_session_test.py @@ -0,0 +1,134 @@ +# Copyright 2022 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. + +import os +import re +import uuid + +from google.api_core.exceptions import NotFound +import pytest +import requests + +import create_live_session +import create_slate +import delete_slate +import get_live_ad_tag_detail +import get_live_session +import list_live_ad_tag_details + +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] +location = "us-central1" +input_bucket_name = "cloud-samples-data/media/" +input_video_file_name = "hls-live/manifest.m3u8" +live_stream_uri = ( + f"https://storage.googleapis.com/{input_bucket_name}{input_video_file_name}" +) +# Single Inline Linear (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags) +ad_tag_uri = "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=" +slate_id = f"my-python-test-slate-{uuid.uuid4()}" +slate_video_file_name = "ForBiggerJoyrides.mp4" +slate_uri = f"https://storage.googleapis.com/{input_bucket_name}{slate_video_file_name}" + + +def test_live_session_operations(capsys: pytest.fixture) -> None: + + # Test setup + + slate_name = f"projects/{project_number}/locations/{location}/slates/{slate_id}" + + try: + delete_slate.delete_slate(project_id, location, slate_id) + except NotFound as e: + print(f"Ignoring NotFound, details: {e}") + out, _ = capsys.readouterr() + + create_slate.create_slate(project_id, location, slate_id, slate_uri) + out, _ = capsys.readouterr() + assert slate_name in out + + # Tests + + create_live_session_response = create_live_session.create_live_session( + project_id, location, live_stream_uri, ad_tag_uri, slate_id + ) + out, _ = capsys.readouterr() + session_name_prefix = ( + f"projects/{project_number}/locations/{location}/liveSessions/" + ) + assert session_name_prefix in out + + str_slice = out.split("/") + session_id = str_slice[len(str_slice) - 1].rstrip("\n") + session_name = ( + f"projects/{project_number}/locations/{location}/liveSessions/{session_id}" + ) + assert session_name in out + + get_live_session.get_live_session(project_id, location, session_id) + out, _ = capsys.readouterr() + assert session_name in out + + # Clean up slate as it is no longer needed + + delete_slate.delete_slate(project_id, location, slate_id) + out, _ = capsys.readouterr() + assert "Deleted slate" in out + + # No list or delete methods for live sessions + + # Ad tag details + + # To get ad tag details, you need to curl the main manifest and + # a rendition first. This supplies media player information to the API. + # + # Curl the play_uri first. The last line of the response will contain a + # renditions location. Curl the live session name with the rendition + # location appended. + + r = requests.get(create_live_session_response.play_uri) + match = re.search("renditions/.*", r.text) + assert match + renditions = match.group() + assert "renditions/" in renditions + + # create_live_session_response.play_uri will be in the following format: + # /projects/{project}/locations/{location}/liveSessions/{session-id}/manifest.m3u8?signature=... + # Replace manifest.m3u8?signature=... with the renditions location. + arr = create_live_session_response.play_uri.split("/") + arr.pop() + tmp = "/".join(arr) + renditions_uri = f"{tmp}/{renditions}" + r = requests.get(renditions_uri) + + list_live_ad_tag_details_response = ( + list_live_ad_tag_details.list_live_ad_tag_details( + project_id, location, session_id + ) + ) + out, _ = capsys.readouterr() + ad_tag_details_name_prefix = f"projects/{project_number}/locations/{location}/liveSessions/{session_id}/liveAdTagDetails/" + assert ad_tag_details_name_prefix in out + + str_slice = list_live_ad_tag_details_response.live_ad_tag_details[0].name.split("/") + ad_tag_details_id = str_slice[len(str_slice) - 1].rstrip("\n") + ad_tag_details_name = f"projects/{project_number}/locations/{location}/liveSessions/{session_id}/liveAdTagDetails/{ad_tag_details_id}" + assert ad_tag_details_name in out + + # b/231626944 for projectNumber below + get_live_ad_tag_detail.get_live_ad_tag_detail( + project_number, location, session_id, ad_tag_details_id + ) + out, _ = capsys.readouterr() + assert ad_tag_details_name in out diff --git a/packages/google-cloud-video-stitcher/samples/snippets/noxfile.py b/packages/google-cloud-video-stitcher/samples/snippets/noxfile.py new file mode 100644 index 000000000000..3b3ffa5d2b0f --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/noxfile.py @@ -0,0 +1,310 @@ +# Copyright 2019 Google LLC +# +# 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. + +from __future__ import print_function + +import glob +import os +from pathlib import Path +import sys +from typing import Callable, Dict, List, Optional + +import nox + +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING +# DO NOT EDIT THIS FILE EVER! +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING + +BLACK_VERSION = "black==22.3.0" +ISORT_VERSION = "isort==5.10.1" + +# Copy `noxfile_config.py` to your directory and modify it instead. + +# `TEST_CONFIG` dict is a configuration hook that allows users to +# modify the test configurations. The values here should be in sync +# with `noxfile_config.py`. Users will copy `noxfile_config.py` into +# their directory and modify it. + +TEST_CONFIG = { + # You can opt out from the test for specific Python versions. + "ignored_versions": [], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "enforce_type_hints": False, + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + # If you need to use a specific version of pip, + # change pip_version_override to the string representation + # of the version number, for example, "20.2.4" + "pip_version_override": None, + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} + + +try: + # Ensure we can import noxfile_config in the project's directory. + sys.path.append(".") + from noxfile_config import TEST_CONFIG_OVERRIDE +except ImportError as e: + print("No user noxfile_config found: detail: {}".format(e)) + TEST_CONFIG_OVERRIDE = {} + +# Update the TEST_CONFIG with the user supplied values. +TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) + + +def get_pytest_env_vars() -> Dict[str, str]: + """Returns a dict for pytest invocation.""" + ret = {} + + # Override the GCLOUD_PROJECT and the alias. + env_key = TEST_CONFIG["gcloud_project_env"] + # This should error out if not set. + ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key] + + # Apply user supplied envs. + ret.update(TEST_CONFIG["envs"]) + return ret + + +# DO NOT EDIT - automatically generated. +# All versions used to test samples. +ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9", "3.10"] + +# Any default versions that should be ignored. +IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] + +TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) + +INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ( + "True", + "true", +) + +# Error if a python version is missing +nox.options.error_on_missing_interpreters = True + +# +# Style Checks +# + + +def _determine_local_import_names(start_dir: str) -> List[str]: + """Determines all import names that should be considered "local". + + This is used when running the linter to insure that import order is + properly checked. + """ + file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] + return [ + basename + for basename, extension in file_ext_pairs + if extension == ".py" + or os.path.isdir(os.path.join(start_dir, basename)) + and basename not in ("__pycache__") + ] + + +# Linting with flake8. +# +# We ignore the following rules: +# E203: whitespace before ‘:’ +# E266: too many leading ‘#’ for block comment +# E501: line too long +# I202: Additional newline in a section of imports +# +# We also need to specify the rules which are ignored by default: +# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] +FLAKE8_COMMON_ARGS = [ + "--show-source", + "--builtin=gettext", + "--max-complexity=20", + "--import-order-style=google", + "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", + "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", + "--max-line-length=88", +] + + +@nox.session +def lint(session: nox.sessions.Session) -> None: + if not TEST_CONFIG["enforce_type_hints"]: + session.install("flake8", "flake8-import-order") + else: + session.install("flake8", "flake8-import-order", "flake8-annotations") + + local_names = _determine_local_import_names(".") + args = FLAKE8_COMMON_ARGS + [ + "--application-import-names", + ",".join(local_names), + ".", + ] + session.run("flake8", *args) + + +# +# Black +# + + +@nox.session +def blacken(session: nox.sessions.Session) -> None: + """Run black. Format code to uniform standard.""" + session.install(BLACK_VERSION) + python_files = [path for path in os.listdir(".") if path.endswith(".py")] + + session.run("black", *python_files) + + +# +# format = isort + black +# + + +@nox.session +def format(session: nox.sessions.Session) -> None: + """ + Run isort to sort imports. Then run black + to format code to uniform standard. + """ + session.install(BLACK_VERSION, ISORT_VERSION) + python_files = [path for path in os.listdir(".") if path.endswith(".py")] + + # Use the --fss option to sort imports using strict alphabetical order. + # See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections + session.run("isort", "--fss", *python_files) + session.run("black", *python_files) + + +# +# Sample Tests +# + + +PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] + + +def _session_tests( + session: nox.sessions.Session, post_install: Callable = None +) -> None: + # check for presence of tests + test_list = glob.glob("*_test.py") + glob.glob("test_*.py") + test_list.extend(glob.glob("tests")) + + if len(test_list) == 0: + print("No tests found, skipping directory.") + return + + if TEST_CONFIG["pip_version_override"]: + pip_version = TEST_CONFIG["pip_version_override"] + session.install(f"pip=={pip_version}") + """Runs py.test for a particular project.""" + concurrent_args = [] + if os.path.exists("requirements.txt"): + if os.path.exists("constraints.txt"): + session.install("-r", "requirements.txt", "-c", "constraints.txt") + else: + session.install("-r", "requirements.txt") + with open("requirements.txt") as rfile: + packages = rfile.read() + + if os.path.exists("requirements-test.txt"): + if os.path.exists("constraints-test.txt"): + session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt") + else: + session.install("-r", "requirements-test.txt") + with open("requirements-test.txt") as rtfile: + packages += rtfile.read() + + if INSTALL_LIBRARY_FROM_SOURCE: + session.install("-e", _get_repo_root()) + + if post_install: + post_install(session) + + if "pytest-parallel" in packages: + concurrent_args.extend(["--workers", "auto", "--tests-per-worker", "auto"]) + elif "pytest-xdist" in packages: + concurrent_args.extend(["-n", "auto"]) + + session.run( + "pytest", + *(PYTEST_COMMON_ARGS + session.posargs + concurrent_args), + # Pytest will return 5 when no tests are collected. This can happen + # on travis where slow and flaky tests are excluded. + # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html + success_codes=[0, 5], + env=get_pytest_env_vars(), + ) + + +@nox.session(python=ALL_VERSIONS) +def py(session: nox.sessions.Session) -> None: + """Runs py.test for a sample using the specified version of Python.""" + if session.python in TESTED_VERSIONS: + _session_tests(session) + else: + session.skip( + "SKIPPED: {} tests are disabled for this sample.".format(session.python) + ) + + +# +# Readmegen +# + + +def _get_repo_root() -> Optional[str]: + """Returns the root folder of the project.""" + # Get root of this repository. Assume we don't have directories nested deeper than 10 items. + p = Path(os.getcwd()) + for i in range(10): + if p is None: + break + if Path(p / ".git").exists(): + return str(p) + # .git is not available in repos cloned via Cloud Build + # setup.py is always in the library's root, so use that instead + # https://github.com/googleapis/synthtool/issues/792 + if Path(p / "setup.py").exists(): + return str(p) + p = p.parent + raise Exception("Unable to detect repository root.") + + +GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) + + +@nox.session +@nox.parametrize("path", GENERATED_READMES) +def readmegen(session: nox.sessions.Session, path: str) -> None: + """(Re-)generates the readme for a sample.""" + session.install("jinja2", "pyyaml") + dir_ = os.path.dirname(path) + + if os.path.exists(os.path.join(dir_, "requirements.txt")): + session.install("-r", os.path.join(dir_, "requirements.txt")) + + in_file = os.path.join(dir_, "README.rst.in") + session.run( + "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/noxfile_config.py b/packages/google-cloud-video-stitcher/samples/snippets/noxfile_config.py new file mode 100644 index 000000000000..706b3f3a0be9 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/noxfile_config.py @@ -0,0 +1,38 @@ +# Copyright 2021 Google LLC +# +# 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. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +# You can copy this file into your directory, then it will be inported from +# the noxfile.py. + +# The source of truth: +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": [], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "enforce_type_hints": True, + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + # "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} diff --git a/packages/google-cloud-video-stitcher/samples/snippets/requirements-test.txt b/packages/google-cloud-video-stitcher/samples/snippets/requirements-test.txt new file mode 100644 index 000000000000..8514f9226911 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/requirements-test.txt @@ -0,0 +1,2 @@ +pytest==7.0.1 +requests==2.26.0 \ No newline at end of file diff --git a/packages/google-cloud-video-stitcher/samples/snippets/requirements.txt b/packages/google-cloud-video-stitcher/samples/snippets/requirements.txt new file mode 100644 index 000000000000..439a120ab837 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/requirements.txt @@ -0,0 +1,3 @@ +google-api-python-client==2.39.0 +grpcio==1.44.0 +google-cloud-video-stitcher==0.1.1 \ No newline at end of file diff --git a/packages/google-cloud-video-stitcher/samples/snippets/slate_test.py b/packages/google-cloud-video-stitcher/samples/snippets/slate_test.py new file mode 100644 index 000000000000..86079c5cfa95 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/slate_test.py @@ -0,0 +1,78 @@ +# Copyright 2022 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. + +import os +import uuid + +from google.api_core.exceptions import NotFound +import pytest + +import create_slate +import delete_slate +import get_slate +import list_slates +import update_slate + +location = "us-west1" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] +slate_id = f"my-python-test-slate-{uuid.uuid4()}" + +input_bucket_name = "cloud-samples-data/media/" +slate_video_file_name = "ForBiggerEscapes.mp4" +updated_slate_video_file_name = "ForBiggerJoyrides.mp4" + +slate_uri = f"https://storage.googleapis.com/{input_bucket_name}{slate_video_file_name}" +updated_slate_uri = ( + f"https://storage.googleapis.com/{input_bucket_name}{updated_slate_video_file_name}" +) + + +def test_slate_operations(capsys: pytest.fixture) -> None: + + slate_name_project_number = ( + f"projects/{project_number}/locations/{location}/slates/{slate_id}" + ) + slate_name_project_id = ( + f"projects/{project_id}/locations/{location}/slates/{slate_id}" + ) + + try: + delete_slate.delete_slate(project_id, location, slate_id) + except NotFound as e: + print(f"Ignoring NotFound, details: {e}") + out, _ = capsys.readouterr() + + create_slate.create_slate(project_id, location, slate_id, slate_uri) + out, _ = capsys.readouterr() + assert slate_name_project_number in out + + list_slates.list_slates(project_id, location) + out, _ = capsys.readouterr() + assert slate_name_project_id in out + + response = update_slate.update_slate( + project_id, location, slate_id, updated_slate_uri + ) + out, _ = capsys.readouterr() + assert slate_name_project_id in out + assert updated_slate_uri in response.uri + + get_slate.get_slate(project_id, location, slate_id) + out, _ = capsys.readouterr() + assert slate_name_project_id in out + + delete_slate.delete_slate(project_id, location, slate_id) + out, _ = capsys.readouterr() + assert "Deleted slate" in out diff --git a/packages/google-cloud-video-stitcher/samples/snippets/update_cdn_key.py b/packages/google-cloud-video-stitcher/samples/snippets/update_cdn_key.py new file mode 100644 index 000000000000..b56ca0e67d53 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/update_cdn_key.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for updating a CDN key. +Example usage: + python update_cdn_key.py --project_id --location \ + --cdn_key_id --hostname \ + [--gcdn_keyname --gcdn_private_key | --akamai_token_key ] +""" + +# [START video_stitcher_update_cdn_key] + +import argparse + +from google.cloud.video import stitcher_v1 +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) +from google.protobuf import field_mask_pb2 as field_mask + + +def update_cdn_key( + project_id: str, + location: str, + cdn_key_id: str, + hostname: str, + gcdn_keyname: str = None, + gcdn_private_key: str = None, + akamai_token_key: str = None, +) -> str: + """Updates a Google Cloud or Akamai CDN key. + Args: + project_id: The GCP project ID. + location: The location of the CDN key. + cdn_key_id: The user-defined CDN key ID. + hostname: The hostname to which this CDN key applies. + gcdn_keyname: Applies to a Google Cloud CDN key. A base64-encoded string secret. + gcdn_private_key: Applies to a Google Cloud CDN key. Public name of the key. + akamai_token_key: Applies to an Akamai CDN key. A base64-encoded string token key.""" + + client = VideoStitcherServiceClient() + + name = f"projects/{project_id}/locations/{location}/cdnKeys/{cdn_key_id}" + + cdn_key = stitcher_v1.types.CdnKey( + name=name, + hostname=hostname, + ) + + if akamai_token_key is not None: + cdn_key.akamai_cdn_key = stitcher_v1.types.AkamaiCdnKey( + token_key=akamai_token_key, + ) + update_mask = field_mask.FieldMask(paths=["hostname", "akamai_cdn_key"]) + elif gcdn_keyname is not None: + cdn_key.google_cdn_key = stitcher_v1.types.GoogleCdnKey( + key_name=gcdn_keyname, + private_key=gcdn_private_key, + ) + update_mask = field_mask.FieldMask(paths=["hostname", "google_cdn_key"]) + else: + update_mask = field_mask.FieldMask(paths=["hostname"]) + + response = client.update_cdn_key(cdn_key=cdn_key, update_mask=update_mask) + print(f"Updated CDN key: {response.name}") + return response + + +# [END video_stitcher_update_cdn_key] + +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 of the CDN key.", + ) + parser.add_argument( + "--cdn_key_id", + help="The user-defined CDN key ID.", + required=True, + ) + parser.add_argument( + "--hostname", + help="The hostname to which this CDN key applies.", + required=True, + ) + parser.add_argument( + "--gcdn_keyname", + help="Applies to a Google Cloud CDN key. The base64-encoded string secret.", + ) + parser.add_argument( + "--gcdn_private_key", + help="Applies to a Google Cloud CDN key. Public name of the key.", + ) + parser.add_argument( + "--akamai_token_key", + help="Applies to an Akamai CDN key. The base64-encoded string token key.", + ) + args = parser.parse_args() + update_cdn_key( + args.project_id, + args.location, + args.cdn_key_id, + args.hostname, + args.gcdn_keyname, + args.gcdn_private_key, + args.akamai_token_key, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/update_slate.py b/packages/google-cloud-video-stitcher/samples/snippets/update_slate.py new file mode 100644 index 000000000000..d796f855bce6 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/update_slate.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +# Copyright 2022 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 Video Stitcher sample for updating a slate. +Example usage: + python update_slate.py --project_id --location \ + --slate_id --slate_uri +""" + +# [START video_stitcher_update_slate] + +import argparse + +from google.cloud.video import stitcher_v1 +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) +from google.protobuf import field_mask_pb2 as field_mask + + +def update_slate(project_id: str, location: str, slate_id: str, slate_uri: str) -> str: + """Updates a slate. + Args: + project_id: The GCP project ID. + location: The location of the slate. + slate_id: The existing slate's ID. + slate_uri: Updated uri of the video slate; must be an MP4 video with at least one audio track.""" + + client = VideoStitcherServiceClient() + + name = f"projects/{project_id}/locations/{location}/slates/{slate_id}" + slate = stitcher_v1.types.Slate( + name=name, + uri=slate_uri, + ) + update_mask = field_mask.FieldMask(paths=["uri"]) + + response = client.update_slate(slate=slate, update_mask=update_mask) + print(f"Updated slate: {response.name}") + return response + + +# [END video_stitcher_update_slate] + +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 of the slate.", + required=True, + ) + parser.add_argument( + "--slate_id", + help="The existing slate's ID.", + required=True, + ) + parser.add_argument( + "--slate_uri", + help="Updated uri of the video slate; must be an MP4 video with at least one audio track.", + required=True, + ) + args = parser.parse_args() + update_slate( + args.project_id, + args.location, + args.slate_id, + args.slate_uri, + ) diff --git a/packages/google-cloud-video-stitcher/samples/snippets/vod_session_test.py b/packages/google-cloud-video-stitcher/samples/snippets/vod_session_test.py new file mode 100644 index 000000000000..187b1612d4d2 --- /dev/null +++ b/packages/google-cloud-video-stitcher/samples/snippets/vod_session_test.py @@ -0,0 +1,94 @@ +# Copyright 2022 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. + +import os + +import pytest + +import create_vod_session +import get_vod_ad_tag_detail +import get_vod_session +import get_vod_stitch_detail +import list_vod_ad_tag_details +import list_vod_stitch_details + +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] +location = "us-central1" +input_bucket_name = "cloud-samples-data/media/" +input_video_file_name = "hls-vod/manifest.m3u8" +vod_uri = f"https://storage.googleapis.com/{input_bucket_name}{input_video_file_name}" +# VMAP Pre-roll (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags) +ad_tag_uri = "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/vmap_ad_samples&sz=640x480&cust_params=sample_ar%3Dpreonly&ciu_szs=300x250%2C728x90&gdfp_req=1&ad_rule=1&output=vmap&unviewed_position_start=1&env=vp&impl=s&correlator=" + + +def test_vod_session_operations(capsys: pytest.fixture) -> None: + + create_vod_session.create_vod_session(project_id, location, vod_uri, ad_tag_uri) + out, _ = capsys.readouterr() + session_name_prefix = f"projects/{project_number}/locations/{location}/vodSessions/" + assert session_name_prefix in out + + str_slice = out.split("/") + session_id = str_slice[len(str_slice) - 1].rstrip("\n") + session_name = ( + f"projects/{project_number}/locations/{location}/vodSessions/{session_id}" + ) + assert session_name in out + + get_vod_session.get_vod_session(project_id, location, session_id) + out, _ = capsys.readouterr() + assert session_name in out + + # No list or delete methods for VOD sessions + + # Ad tag details + + response = list_vod_ad_tag_details.list_vod_ad_tag_details( + project_id, location, session_id + ) + out, _ = capsys.readouterr() + ad_tag_details_name_prefix = f"projects/{project_number}/locations/{location}/vodSessions/{session_id}/vodAdTagDetails/" + assert ad_tag_details_name_prefix in out + + str_slice = response.name.split("/") + ad_tag_details_id = str_slice[len(str_slice) - 1].rstrip("\n") + ad_tag_details_name = f"projects/{project_number}/locations/{location}/vodSessions/{session_id}/vodAdTagDetails/{ad_tag_details_id}" + assert ad_tag_details_name in out + + get_vod_ad_tag_detail.get_vod_ad_tag_detail( + project_id, location, session_id, ad_tag_details_id + ) + out, _ = capsys.readouterr() + assert ad_tag_details_name in out + + # Stitch details + + response = list_vod_stitch_details.list_vod_stitch_details( + project_id, location, session_id + ) + out, _ = capsys.readouterr() + stitch_details_name_prefix = f"projects/{project_number}/locations/{location}/vodSessions/{session_id}/vodStitchDetails/" + assert stitch_details_name_prefix in out + + str_slice = response.name.split("/") + stitch_details_id = str_slice[len(str_slice) - 1].rstrip("\n") + stitch_details_name = f"projects/{project_number}/locations/{location}/vodSessions/{session_id}/vodStitchDetails/{stitch_details_id}" + assert stitch_details_name in out + + get_vod_stitch_detail.get_vod_stitch_detail( + project_id, location, session_id, stitch_details_id + ) + out, _ = capsys.readouterr() + assert stitch_details_name in out