|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2022 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Google Cloud Live Stream sample for creating a channel with a backup input. |
| 18 | + A channel resource represents the processor that performs a user-defined |
| 19 | + "streaming" operation. |
| 20 | +Example usage: |
| 21 | + python create_channel_with_backup_input.py --project_id <project-id> \ |
| 22 | + --location <location> --channel_id <channel-id> \ |
| 23 | + --primary_input_id <primary-input-id> \ |
| 24 | + --backup_input_id <backup-input-id> --output_uri <uri> |
| 25 | +""" |
| 26 | + |
| 27 | +# [START livestream_create_channel_with_backup_input] |
| 28 | + |
| 29 | +import argparse |
| 30 | + |
| 31 | +from google.cloud.video import live_stream_v1 |
| 32 | +from google.cloud.video.live_stream_v1.services.livestream_service import ( |
| 33 | + LivestreamServiceClient, |
| 34 | +) |
| 35 | +from google.protobuf import duration_pb2 as duration |
| 36 | + |
| 37 | + |
| 38 | +def create_channel_with_backup_input( |
| 39 | + project_id: str, |
| 40 | + location: str, |
| 41 | + channel_id: str, |
| 42 | + primary_input_id: str, |
| 43 | + backup_input_id: str, |
| 44 | + output_uri: str, |
| 45 | +) -> str: |
| 46 | + """Creates a channel. |
| 47 | + Args: |
| 48 | + project_id: The GCP project ID. |
| 49 | + location: The location in which to create the channel. |
| 50 | + channel_id: The user-defined channel ID. |
| 51 | + primary_input_id: The user-defined primary input ID. |
| 52 | + backup_input_id: The user-defined backup input ID. |
| 53 | + output_uri: Uri of the channel output folder in a Cloud Storage bucket.""" |
| 54 | + |
| 55 | + client = LivestreamServiceClient() |
| 56 | + parent = f"projects/{project_id}/locations/{location}" |
| 57 | + primary_input = ( |
| 58 | + f"projects/{project_id}/locations/{location}/inputs/{primary_input_id}" |
| 59 | + ) |
| 60 | + backup_input = ( |
| 61 | + f"projects/{project_id}/locations/{location}/inputs/{backup_input_id}" |
| 62 | + ) |
| 63 | + name = f"projects/{project_id}/locations/{location}/channels/{channel_id}" |
| 64 | + |
| 65 | + channel = live_stream_v1.types.Channel( |
| 66 | + name=name, |
| 67 | + input_attachments=[ |
| 68 | + live_stream_v1.types.InputAttachment( |
| 69 | + key="my-primary-input", |
| 70 | + input=primary_input, |
| 71 | + automatic_failover=live_stream_v1.types.InputAttachment.AutomaticFailover( |
| 72 | + input_keys=["my-backup-input"], |
| 73 | + ), |
| 74 | + ), |
| 75 | + live_stream_v1.types.InputAttachment( |
| 76 | + key="my-backup-input", |
| 77 | + input=backup_input, |
| 78 | + ), |
| 79 | + ], |
| 80 | + output=live_stream_v1.types.Channel.Output( |
| 81 | + uri=output_uri, |
| 82 | + ), |
| 83 | + elementary_streams=[ |
| 84 | + live_stream_v1.types.ElementaryStream( |
| 85 | + key="es_video", |
| 86 | + video_stream=live_stream_v1.types.VideoStream( |
| 87 | + h264=live_stream_v1.types.VideoStream.H264CodecSettings( |
| 88 | + profile="main", |
| 89 | + width_pixels=1280, |
| 90 | + height_pixels=720, |
| 91 | + bitrate_bps=1000000, |
| 92 | + frame_rate=30, |
| 93 | + ), |
| 94 | + ), |
| 95 | + ), |
| 96 | + live_stream_v1.types.ElementaryStream( |
| 97 | + key="es_audio", |
| 98 | + audio_stream=live_stream_v1.types.AudioStream( |
| 99 | + codec="aac", channel_count=2, bitrate_bps=160000 |
| 100 | + ), |
| 101 | + ), |
| 102 | + ], |
| 103 | + mux_streams=[ |
| 104 | + live_stream_v1.types.MuxStream( |
| 105 | + key="mux_video", |
| 106 | + elementary_streams=["es_video"], |
| 107 | + segment_settings=live_stream_v1.types.SegmentSettings( |
| 108 | + segment_duration=duration.Duration( |
| 109 | + seconds=2, |
| 110 | + ), |
| 111 | + ), |
| 112 | + ), |
| 113 | + live_stream_v1.types.MuxStream( |
| 114 | + key="mux_audio", |
| 115 | + elementary_streams=["es_audio"], |
| 116 | + segment_settings=live_stream_v1.types.SegmentSettings( |
| 117 | + segment_duration=duration.Duration( |
| 118 | + seconds=2, |
| 119 | + ), |
| 120 | + ), |
| 121 | + ), |
| 122 | + ], |
| 123 | + manifests=[ |
| 124 | + live_stream_v1.types.Manifest( |
| 125 | + file_name="manifest.m3u8", |
| 126 | + type_="HLS", |
| 127 | + mux_streams=["mux_video", "mux_audio"], |
| 128 | + max_segment_count=5, |
| 129 | + ), |
| 130 | + ], |
| 131 | + ) |
| 132 | + operation = client.create_channel( |
| 133 | + parent=parent, channel=channel, channel_id=channel_id |
| 134 | + ) |
| 135 | + response = operation.result(60) |
| 136 | + print(f"Channel: {response.name}") |
| 137 | + |
| 138 | + return response |
| 139 | + |
| 140 | + |
| 141 | +# [END livestream_create_channel_with_backup_input] |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + parser = argparse.ArgumentParser() |
| 145 | + parser.add_argument("--project_id", help="Your Cloud project ID.", required=True) |
| 146 | + parser.add_argument( |
| 147 | + "--location", |
| 148 | + help="The location in which to create the channel.", |
| 149 | + default="us-central1", |
| 150 | + ) |
| 151 | + parser.add_argument( |
| 152 | + "--channel_id", |
| 153 | + help="The user-defined channel ID.", |
| 154 | + required=True, |
| 155 | + ) |
| 156 | + parser.add_argument( |
| 157 | + "--primary_input_id", |
| 158 | + help="The user-defined primary input ID.", |
| 159 | + required=True, |
| 160 | + ) |
| 161 | + parser.add_argument( |
| 162 | + "--backup_input_id", |
| 163 | + help="The user-defined backup input ID.", |
| 164 | + required=True, |
| 165 | + ) |
| 166 | + parser.add_argument( |
| 167 | + "--output_uri", |
| 168 | + help="The Cloud Storage bucket (and optional folder) in which to save the livestream output.", |
| 169 | + required=True, |
| 170 | + ) |
| 171 | + args = parser.parse_args() |
| 172 | + create_channel_with_backup_input( |
| 173 | + args.project_id, |
| 174 | + args.location, |
| 175 | + args.channel_id, |
| 176 | + args.primary_input_id, |
| 177 | + args.backup_input_id, |
| 178 | + args.output_uri, |
| 179 | + ) |
0 commit comments