opencv | list live stream | transition live stream | Search

This code binds a live stream to a YouTube broadcast using the YouTube API, utilizing functions to import necessary modules and notebooks, authorize API credentials, retrieve broadcast and stream information, and execute the binding request. The bind_stream_to_broadcast function returns the stream information and broadcast ID, and is exported for use in other modules.

Run example

npm run import -- "bind youtube stream"

bind youtube stream

import_notebook("list broadcasts",
"globals("))
import_notebook("list live stream",
"globals("))
from googleapiclient.discovery import build

def bind_stream_to_broadcast():

    credentials = youtube_authorize()

    broadcast_id = list_broadcasts()[0]["id"]

    stream = list_livestream()

    print(stream['cdn']['ingestionInfo']['streamName'])

    # Build the YouTube API client
    youtube = build("youtube", "v3", credentials=credentials)

    request = youtube.liveBroadcasts().bind(
        id=broadcast_id,
        part="id,contentDetails",
        streamId=stream["id"]
    )
    response = request.execute()
    print("✅ Stream successfully bound to broadcast:", response)
    
    return stream, broadcast_id

__all__ = {
    "bind_stream_to_broadcast": bind_stream_to_broadcast
}

What the code could have been:

stream, broadcast_id = bind_stream_to_broadcast()

Code Breakdown

Importing Required Modules and Notebooks

import_notebook("list broadcasts", globals())
import_notebook("list live stream", globals())
from googleapiclient.discovery import build

Binding Stream to Broadcast

def bind_stream_to_broadcast():

Authorizing YouTube API Credentials

credentials = youtube_authorize()

Getting Broadcast ID and Stream Information

broadcast_id = list_broadcasts()[0]["id"]
stream = list_livestream()
print(stream['cdn']['ingestionInfo']['streamName'])

Building YouTube API Client and Binding Stream to Broadcast

youtube = build("youtube", "v3", credentials=credentials)

request = youtube.liveBroadcasts().bind(
    id=broadcast_id,
    part="id,contentDetails",
    streamId=stream["id"]
)
response = request.execute()
print("✅ Stream successfully bound to broadcast:", response)

Returning Stream and Broadcast ID

return stream, broadcast_id

Exporting Function

__all__ = {
    "bind_stream_to_broadcast": bind_stream_to_broadcast
}