首先直播不是七牛云默认开通的服务,需在直播云服务栏目下进行申请。然后点击新建直播空间,输入直播空间名称和直播域名等信息,这里需要注意的是直播空间名称即为后面代码中需用到 hub_name,另外直播所使用的域名不仅需进行工信部备案,且需通过公安部备案。紧接着根据需要进行对应域名的解析工作:
直播推流域名
RTMP pili-publish.yourdomain.com
直播播放域名
RTMP pili-live-rtmp.yourdomain.com
HLS pili-live-hls.yourdomain.com
HDL pili-live-hdl.yourdomain.com
直播封面 pili-live-snapshot.yourdomain.com
持久化域名
点播域名 pili-vod.yourdomain.com
遗憾的是当前官方代码包仅支持 Python 2, Alan使用 Python 2来进行了测试,经验证可以调通数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 安装 SDK pip install pili2 # 示例代码 import time import pili AccessKey = "" # 替换成自己 Qiniu 账号的 AccessKey SecretKey = "" # 替换成自己 Qiniu 账号的 SecretKey hub_name = "" # Hub 必须事先存在 mac = pili.Mac(AccessKey, SecretKey) client = pili.Client(mac) stream_title1 = 'demo' stream_pre = '' # Generate RTMP publish URL print ("RTMP publish URL:") print (pili.rtmp_publish_url("publish-rtmp.yourdomain.com", hub_name, stream_title1, mac, 60)) #Generate RTMP play URL print ("RTMP play URL:") print (pili.rtmp_play_url("live-rtmp.yourdomain.com", hub_name, stream_title1)) #Generate HLS play URL print ("HLS live URL:") print (pili.hls_play_url("live-hls.yourdomain.com", hub_name, stream_title1)) # http://live-hls.yourdomain.com/PiliSDKTest/streamtitle.m3u8 #Generate HDL play URL print ("HDL live URL:") print (pili.hdl_play_url("live-hdl.yourdomain.com", hub_name, stream_title1)) # Generate Snapshot play URL print ("snapshot URL:") print (pili.snapshot_play_url("live-snapshot.yourdomain.com", hub_name, stream_title1)) # Hub # Instantiate a Pili Hub object mac = pili.Mac(AccessKey, SecretKey) client = pili.Client(mac) hub = client.hub(hub_name) # Create a new Stream # stream = hub.create(stream_title1) # print ("create stream:") # print (stream) # Get a Stream print ("get stream:") stream = hub.get(stream_title1) print (stream) # List Streams print ("list streams:") print (hub.list(prefix=stream_pre, limit=2)) |
执行结果如下