All Products
Search
Document Center

Object Storage Service:Manage LiveChannels (Python SDK V1)

Last Updated:Nov 29, 2025

This topic describes common LiveChannel operations using the Python software development kit (SDK), such as creating, listing, and deleting LiveChannels.

Prerequisites

  • Python 3.6

    Note

    The examples in this topic are written for Python 3.6 but are also compatible with Python 2.6, 2.7, 3.3, 3.4, and 3.5.

  • aliyun-oss-python-sdk 2.9.0

  • OBS Studio stream ingest tool

  • IDE

Create a LiveChannel

Before you can upload audio and video data 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 instance.
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# Create and configure the live channel.
# The channel name is test_rtmp_live. The generated M3U8 file for the live stream is named test.m3u8. This manifest contains three TS files, and each TS file has a duration of 5 seconds. This is a recommended value. The actual duration depends on the keyframes.
channel_name = "test_rtmp_live"
playlist_name = "test.m3u8"

create_result = bucket.create_live_channel(
        channel_name,
        oss2.models.LiveChannelInfo(
            status = 'enabled',
            description = 'A test live channel',
            target = oss2.models.LiveChannelInfoTarget(
                playlist_name = playlist_name,
                frag_count = 3,
                frag_duration = 5)))

List and delete LiveChannels

The following code shows how to list and delete LiveChannels:

Note

If multiple LiveChannels exist, this operation deletes only the latest LiveChannel that matches the prefix. To delete a specific stream, set the prefix parameter to the full name of the stream. The operation lists all LiveChannels that match the prefix, including the one to be deleted. A successful deletion does not return a value. If no stream matches the prefix, the call returns an error.

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 instance.
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# List LiveChannels that match the rule.
# List all eligible live channels in the bucket.
# param: prefix (Type: str) specifies the prefix of the live channel names to list. If not specified, all live channels 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)

Set the status of a LiveChannel

The following code shows how to set the status of a LiveChannel. If no error message is returned, the status is successfully set.

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 instance.
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# Enable or disable the live channel.
bucket.put_live_channel_status(channel_name, 'enabled')
bucket.put_live_channel_status(channel_name, 'disabled')

Get an RTMP ingest URL and signature (V1 signatures only)

The following code shows how to retrieve an RTMP ingest URL and signature:

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 instance.
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# Get the ingest and playback URLs.
# After you create a live channel, you get a publish_url for stream ingest (an RTMP ingest URL) and a play_url for playback (the URL of the M3U8 file generated from the stream).
# Before you get the URLs and signature, you must create a LiveChannel and get the create_result. For more information, see the example for creating a LiveChannel.
publish_url = create_result.publish_url
play_url = create_result.play_url
print("Ingest URL:", publish_url)
print("Playback URL:", play_url)

# After you get the ingest and playback URLs, you can ingest streams to and play streams from OSS. If the bucket ACL is not public-read-write, you must sign the ingest URL. If the bucket ACL is public-read-write, you can use the publish_url directly for stream ingest.
# The expires parameter specifies a relative time in seconds. It indicates the number of seconds until the ingest URL expires.
# All parameters are included in the signature.
# After you get the signed URL, you can use a stream ingest tool to start streaming. After a connection is established with OSS, the stream is not interrupted even if the URL expires. OSS checks the validity of the expires parameter only when a new stream ingest connection is established.
signed_url = bucket.sign_rtmp_url(channel_name, playlist_name, expires=3600)
print(signed_url)

Get the status information 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 instance.
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# View the status information of the current stream.
get_status = bucket.get_live_channel_stat(channel_name)
print("Connection Time:", get_status.connected_time)
print("IP of the Ingest Client:", get_status.remote_addr)
print("Ingest Status:", get_status.status)

Generate and view a playlist

The PostVodPlaylist operation generates a video-on-demand (VOD) playlist for a specified LiveChannel. OSS queries the TS files generated by the LiveChannel within a specified time range and combines them into an M3U8 playlist.

The following code shows 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 instance
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.
# To generate a VOD playlist from the TS files of a live stream, use the post_vod_playlist method.
# This example sets the start time to 3600 seconds before the current time and the end time to the current time. This generates a playlist for the stream ingested in the last hour.
# After this operation is successfully called, a playlist file 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)

# To view the content of a playlist for a specific time period, use get_vod_playlist.
result = bucket.get_vod_playlist(channel_name, start_time=start_time, end_time=end_time)
print("playlist:", result.playlist)

Get the configuration information 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 instance.
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# Get the configuration information of the 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("-------------------")

Get the stream ingest records of a LiveChannel

The GetLiveChannelHistory operation returns up to 10 of the most recent stream ingest records for a specified LiveChannel. The following code shows how to retrieve 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 instance.
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

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

References

For more information, see LiveChannel operations, live_channel.py, and api.py.

FAQ

  • Why can't I retrieve information such as the stream ingest status, client IP address, and connection time?

    To retrieve the stream ingest status information using get_live_channel_stat, the corresponding channel (channel_name) must be in the Live state. This means the client is connected to the ingest URL and is actively ingesting the stream.

  • Can I use .get_live_channel_history to retrieve the start and end times and the remote address of historical stream ingests?

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

  • What is the data type of the channel information obtained through list_live_channel?

    String. For more information, see ListLiveChannel.

  • What is the required format for the end_time parameter in the post_vod_playlist function?

    Integer. For more information, see PostVodPlaylist.

  • I receive the error "'Code': 'InvalidArgument', 'Message': 'No ts file found in specified time span.'".

    You can generate a VOD playlist only after the stream files have been uploaded.