All Products
Search
Document Center

Object Storage Service:Manage LiveChannels by using OSS SDK for Python

Last Updated:Nov 13, 2024

This topic describes the common operations that you can perform on LiveChannels by using Object Storage Service (OSS) SDK for Python. For example, you can create, list, and delete LiveChannels.

Set up the environment

  • Python 3.6

    Note

    The examples in this topic are provided based on Python 3.6 and also applicable to Python 2.6, 2.7, 3.3, 3.4, and 3.5.

  • aliyun-oss-python-sdk 2.9.0

  • OBS Studio

  • IDE

Create a LiveChannel

Before you can upload audio and video data by using Real-Time Messaging Protocol (RTMP), you must call the PutLiveChannel operation to create a LiveChannel. The response to the PutLiveChannel request includes the URL that is used to ingest streams to the LiveChannel and the URL that is used to play the ingested streams.

Note

You can use the returned URLs to ingest and play streams. You can also perform operations based on the returned LiveChannel name, such as querying stream ingest status, querying stream ingest records, and disabling stream ingest.

If an existing LiveChannel has the same name as the LiveChannel to create, the existing LiveChannel is overwritten and the new LiveChannel has the default initial status and settings.

The following sample code provides an example on how to create a LiveChannel:

import os
import oss2

access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '**')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '***')
bucket_name = os.getenv('OSS_TEST_BUCKET', '********')
endpoint = os.getenv('OSS_TEST_ENDPOINT', '***')

# Create a bucket. 
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# Create and configure a LiveChannel. 
# Set the name of the channel to test_rtmp_live. Set the M3U8 file generated by live streaming to test.m3u8. The index file includes three TS files, and the duration of each TS file is 5 seconds. The value of 5 seconds in this example is the recommended value. The actual duration varies based on the key frame of the video file. 
channel_name = "test_rtmp_live"
playlist_name = "test.m3u8"

create_result = bucket.create_live_channel(
        channel_name,
        oss2.models.LiveChannelInfo(
            status = 'enabled',
            description = 'LiveChannel used for the test',
            target = oss2.models.LiveChannelInfoTarget(
                playlist_name = playlist_name,
                frag_count = 3,
                frag_duration = 5)))

List and delete LiveChannels

The following sample code provides an example on how to list and delete LiveChannels:

Note

If a bucket has multiple LiveChannels whose names contain the specified prefix, only the most recently created one of the LiveChannels is deleted when you perform the list and delete operation. If you want to delete a specific LiveChannel, we recommend that you set the prefix to the full name of the LiveChannel. By design, LiveChannel listing happens before LiveChannel deletion. When you perform the list and delete operation, OSS returns all LiveChannels whose names contain the specified prefix, including the one to delete. If the deletion is successful, no return value is returned for the deletion. If the specified prefix does not match a LiveChannel, an error is returned.

import os
import oss2

access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '**')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '***')
bucket_name = os.getenv('OSS_TEST_BUCKET', '********')
endpoint = os.getenv('OSS_TEST_ENDPOINT', '***')

# Create a bucket. 
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# List the LiveChannels that meet the requirements. 
# List all LiveChannels that meet the requirements in the bucket. 
# Parameter: prefix is of type str and specifies the prefix in the names of LiveChannels to list. If you do not specify this parameter, all LiveChannels in the bucket are listed. 
# return: class:`ListLiveChannelResult <oss2.models.ListLiveChannelResult>`
for info in oss2.LiveChannelIterator(bucket, prefix="test"):
    print(info.name)

# Delete the LiveChannel. 
bucket.delete_live_channel(info.name)

Configure the status of a LiveChannel

The following sample code provides an example on how to configure the status of a LiveChannel. If the sample code does not return an error, the status configuration is successful.

import os
import oss2

access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '**')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '***')
bucket_name = os.getenv('OSS_TEST_BUCKET', '********')
endpoint = os.getenv('OSS_TEST_ENDPOINT', '***')

# Create a bucket. 
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# Enable or disable a LiveChannel. 
bucket.put_live_channel_status(channel_name, 'enabled')
bucket.put_live_channel_status(channel_name, 'disabled')

Query RTMP ingest URLs and signatures (Only signature algorithm V1 is supported)

The following sample code provides an example on how to query Real-Time Messaging Protocol (RTMP) ingest URLs and signatures:

import os
import oss2

access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '**')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '***')
bucket_name = os.getenv('OSS_TEST_BUCKET', '********')
endpoint = os.getenv('OSS_TEST_ENDPOINT', '***')

# Create a bucket. 
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# Query the ingest URL and streaming URL. 
# After you create a LiveChannel, query play_url that is used to ingest the stream and publish_url that is used to watch the stream. play_url is the RTMP ingest URL. If the access control list (ACL) of your bucket is not public-read-write, you must include a signature in the URL. 
# Create a LiveChannel and obtain create_result before you obtain the URLs and include a signature. For more information, see the preceding LiveChannel creation example. 
publish_url = create_result.publish_url
play_url = create_result.play_url
print("Ingest URL:",publish_url)
print("Streaming URL:",play_url)

# After you query the ingest URL and streaming URL, you can ingest the stream to OSS and watch the stream. If the bucket ACL is not public-read-write, you need to sign the URL. If the bucket ACL is public-read-write, you can directly use publish_url to ingest the stream. 
# The value of expires in the example indicates the period of time in seconds before the stream that you want to ingest expires. 
# All the parameters are involved in the signature. 
# After you query signed_url, you can use the stream ingest tool to ingest streams to OSS. OSS checks the value of expires only when a stream connection is requested. A stream that is connected to OSS is not interrupted even if the URL expires during the stream ingest process. 
signed_url = bucket.sign_rtmp_url(channel_name, playlist_name, expires=3600)
print(signed_url)

Query the stream ingest status of a LiveChannel

The following sample code provides an example on how to query the stream ingest status of a LiveChannel:

import os
import oss2

access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '**')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '***')
bucket_name = os.getenv('OSS_TEST_BUCKET', '********')
endpoint = os.getenv('OSS_TEST_ENDPOINT', '***')

# Create a bucket. 
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# Query the status of the current stream. 
get_status = bucket.get_live_channel_stat(channel_name)
print("Connection time:",get_status.connected_time)
print("IP address of the stream ingest client:",get_status.remote_addr)
print("Stream ingest status:",get_status.status)

Generate and view a playlist

You can call the PostVodPlaylist operation to generate a VOD playlist for a specific LiveChannel. OSS queries the TS files that are generated by the streams ingested to the specified LiveChannel within a specific time period and converges the files into an M3U8 playlist.

The following sample code provides an example on how to generate and view a playlist:

import os
import oss2
import time

access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '**')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '***')
bucket_name = os.getenv('OSS_TEST_BUCKET', '********')
endpoint = os.getenv('OSS_TEST_ENDPOINT', '***')

# Create a bucket.
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

end_time = int(time.time())
start_time = end_time - 3600
generate_playlist = "my_vod_list.m3u8"
# Generate a VOD playlist. 
# If you want to use the TS segments generated by stream ingest to generate a VOD playlist, you can use the post_vod_playlist function. 
# In this example, the start time is the current time minus 3,600 seconds, and the end time is the current time. This time setting specifies that a playlist is generated for the stream of the previous hour. 
# After this operation is called, a playlist named my_vod_list.m3u8 is generated in OSS. 
bucket.post_vod_playlist(
                channel_name,
                playlist_name,
                start_time = start_time,
                end_time = end_time)

# If you want to view the playlist content within a specific period of time, you can use get_vod_playlist. 
result = bucket.get_vod_playlist(channel_name, start_time=start_time, end_time=end_time)
print("playlist:", result.playlist)

Query the configurations of a LiveChannel

The following sample code provides an example on how to query the configurations of a LiveChannel:

import os
import oss2

access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '**')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '***')
bucket_name = os.getenv('OSS_TEST_BUCKET', '********')
endpoint = os.getenv('OSS_TEST_ENDPOINT', '***')

# Create a bucket. 
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# Query the configurations of a LiveChannel. 
get_result = bucket.get_live_channel(channel_name)
print("-------------------")
print("Stream ingest configuration")
print(get_result.description)
print(get_result.status)
print(get_result.target.type)
print(get_result.target.frag_count)
print(get_result.target.frag_duration)
print(get_result.target.playlist_name)
print("-------------------")

Query the stream ingest records of a LiveChannel

You can call the GetLiveChannelHistory operation to query up to the 10 most recent stream ingest records of a LiveChannel. The following sample code provides an example on how to query the stream ingest records of a LiveChannel:

import os
import oss2

access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '**')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '***')
bucket_name = os.getenv('OSS_TEST_BUCKET', '********')
endpoint = os.getenv('OSS_TEST_ENDPOINT', '***')

# Create a bucket. 
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# Query the stream ingest records of a LiveChannel. 
history_result = bucket.get_live_channel_history(channel_name)
print("Number of stream ingest records:",len(history_result.records))

References

For more information about LiveChannels, see Overview, live_channel.py, and api.py.

FAQ

  • Why does the get_live_channel_stat function fail to return the stream ingest status, IP address of the client, and connection time?

    The LiveChannel whose status you query is not in the Live state. The get_live_channel_stat function can be called only on a LiveChannel that is being used to ingest stream from the client.

  • Can I use the get_live_channel_history function to query the start time and end time of a stream ingest record and the IP address of the stream ingest client?

    Yes, you can. For more information, see GetLiveChannelHistory.

  • What is the data type of LiveChannel information that list_live_channel returns?

    The LiveChannel information returned by list_live_channel is of type string. For more information, see GetLiveChannelHistory.

  • What is the valid data type of the value of the end_time parameter in a post_vod_playlist function call?

    The value of the end_time parameter in a post_vod_playlist function call must be of type integer. For more information, see PostVodPlaylist.

  • Why is the ['Code': 'InvalidArgument', 'Message': 'No ts file found in specified time span.'] error message returned?

    A VOD playlist can be generated only after the stream ingest files are uploaded.