diff --git a/README.md b/README.md index b73369f..9088cb5 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,11 @@ - [x] stream.update() - [x] stream.disable() - [x] stream.enable() - - [x] stream.status() - [x] stream.rtmp_publish_url() - [x] stream.rtmp_live_urls() - [x] stream.hls_live_urls() - [x] stream.http_flv_live_urls() + - [x] stream.status() - [x] stream.segments() - [x] stream.hls_playback_urls() - [x] stream.snapshot() @@ -38,11 +38,11 @@ - [Update a Stream](#update-a-stream) - [Disable a Stream](#disable-a-stream) - [Enable a Stream](#enable-a-stream) - - [Get Stream status](#get-stream-status) - [Generate RTMP publish URL](#generate-rtmp-publish-url) - [Generate RTMP live play URLs](#generate-rtmp-live-play-urls) - [Generate HLS live play URLs](generate-hls-live-play-urls) - [Generate Http-Flv live play URLs](generate-http-flv-live-play-urls) + - [Get Stream status](#get-stream-status) - [Get Stream segments](#get-stream-segments) - [Generate HLS playback URLs](generate-hls-playback-urls) - [Save Stream as a file](#save-stream-as-a-file) @@ -96,6 +96,29 @@ hub = Hub(credentials, hub_name) # publishSecrity : optional, string, can be "dynamic" or "static", "dynamic" as default stream = hub.create_stream(title=None, publishKey=None, publishSecurity="static") # return stream object... +print "\ncreate_stream()\n", stream.to_json() +# { +# "publishSecurity": "dynamic", +# "hub": "test-origin", +# "title": "55db4a9ee3ba573b20000004", +# "publishKey": "976655fbf3bee71e", +# "disabled": false, +# "hosts": { +# "live": { +# "http": "e4kvkh.live1-http.z1.pili.qiniucdn.com", +# "rtmp": "e4kvkh.live1-rtmp.z1.pili.qiniucdn.com" +# }, +# "playback": { +# "http": "e4kvkh.playback1.z1.pili.qiniucdn.com" +# }, +# "publish": { +# "rtmp": "e4kvkh.publish.z1.pili.qiniup.com" +# } +# }, +# "updatedAt": "2015-08-24T16:47:26.786Z", +# "id": "z1.test-origin.55db4a9ee3ba573b20000004", +# "createdAt": "2015-08-24T16:47:26.786Z" +# } ``` #### Get a Stream @@ -104,6 +127,8 @@ stream = hub.create_stream(title=None, publishKey=None, publishSecurity="static" # stream_id: required, string stream = hub.get_stream(stream_id=id) # return stream object... +print "\nget_stream()\n", stream +# ``` #### List Streams @@ -112,12 +137,28 @@ stream = hub.get_stream(stream_id=id) # marker : optional, string # limit : optional, int # title : optional, string -res = hub.list_streams(marker=None, limit=50, title="prefix_") +res = hub.list_streams(marker=None, limit=10, title="prefix_") for s in res["items"]: # s is stream object... # Do someting... pass next = hub.list_streams(marker=res["marker"]) +print "\nlist_streams()\n", res +# { +# "marker": "10", +# "items": [ +# , +# , +# , +# , +# , +# , +# , +# , +# , +# +# ] +# } ``` ### Stream @@ -125,7 +166,29 @@ next = hub.list_streams(marker=res["marker"]) #### To JSON string ```python -stream.to_json() +print stream.to_json() +# { +# "publishSecurity":"static", +# "hub":"test-origin", +# "title":"55db4ecae3ba573b20000006", +# "publishKey":"new_secret_words", +# "disabled":false, +# "hosts":{ +# "live":{ +# "http":"e4kvkh.live1-http.z1.pili.qiniucdn.com", +# "rtmp":"e4kvkh.live1-rtmp.z1.pili.qiniucdn.com" +# }, +# "playback":{ +# "http":"e4kvkh.playback1.z1.pili.qiniucdn.com" +# }, +# "publish":{ +# "rtmp":"e4kvkh.publish.z1.pili.qiniup.com" +# } +# }, +# "updatedAt":"2015-08-24T13:05:15.272975102-04:00", +# "id":"z1.test-origin.55db4ecae3ba573b20000006", +# "createdAt":"2015-08-24T13:05:14.526-04:00" +# } ``` #### Update a Stream @@ -149,28 +212,12 @@ stream.disable() stream.enable() ``` -#### Get Stream status - -```python -status = stream.status() -print status -# { -# "addr": "222.73.202.226:2572", -# "status": "connected", -# "bytesPerSecond": 16870.200000000001, -# "framesPerSecond": { -# "audio": 42.200000000000003, -# "video": 14.733333333333333, -# "data": 0.066666666666666666, -# } -# } -``` - #### Generate RTMP publish URL ```python url = stream.rtmp_publish_url() print url +# rtmp://e4kvkh.publish.z1.pili.qiniup.com/test-origin/55db52e1e3ba573b2000000e?key=new_secret_words ``` #### Generate RTMP live play URLs @@ -182,6 +229,7 @@ for k in urls: # Get original RTMP live url original_url = urls["ORIGIN"] +# {"ORIGIN": "rtmp://e4kvkh.live1-rtmp.z1.pili.qiniucdn.com/test-origin/55db52e1e3ba573b2000000e"} ``` #### Generate HLS play live URLs @@ -193,6 +241,7 @@ for k in urls: # Get original HLS live url original_url = urls["ORIGIN"] +# {"ORIGIN": "http://e4kvkh.live1-http.z1.pili.qiniucdn.com/test-origin/55db52e1e3ba573b2000000e.m3u8"} ``` #### Generate Http-Flv live play URLs @@ -204,25 +253,43 @@ for k in urls: # Get original Http-Flv live url original_url = urls["ORIGIN"] +# {"ORIGIN": "http://e4kvkh.live1-http.z1.pili.qiniucdn.com/test-origin/55db52e1e3ba573b2000000e.flv"} +``` + +#### Get Stream status + +```python +status = stream.status() +print status +# { +# "addr": "222.73.202.226:2572", +# "status": "connected", +# "bytesPerSecond": 16870.200000000001, +# "framesPerSecond": { +# "audio": 42.200000000000003, +# "video": 14.733333333333333, +# "data": 0.066666666666666666, +# } +# } ``` #### Get Stream segments ```python -# start : optional, int64, in second, unix timestamp -# end : optional, int64, in second, unix timestamp -# limit : optional, uint32 +# start_second : optional, int64, in second, unix timestamp +# end_second : optional, int64, in second, unix timestamp +# limit : optional, uint32 # ...but you must provide both or none of the arguments. -segments = stream.segments(start_second=start, end_second=end, limit=None) +segments = stream.segments(start_second=None, end_second=None, limit=None) print segments # [ # { -# "start": , -# "end": +# "start": 1440282134, +# "end": 1440437833 # }, # { -# "start": , -# "end": +# "start": 1440437981, +# "end": 1440438835 # }, # ... # ] @@ -249,7 +316,7 @@ original_url = urls["ORIGIN"] # start : required, int64, in second, unix timestamp # end : required, int64, in second, unix timestamp # notifyUrl : optional, string -res = stream.save_as(name="videoName.mp4", format="mp4", start_second=start, end_second=end, notifyUrl=None) +res = stream.save_as(name="videoName.mp4", format="mp4", start=1440282134, end=1440437833, notifyUrl=None) print res # { # "url": "http://ey636h.vod1.z1.pili.qiniucdn.com/recordings/z1.test-hub.55d81a72e3ba5723280000ec/videoName.m3u8", @@ -259,6 +326,7 @@ print res ``` #### Snapshot stream + ```python # name : required, string # format : required, string see http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html @@ -277,9 +345,30 @@ API: `curl -D GET http://api.qiniu.com/status/get/prefop?id={PersistentId}` Doc reference: #### Delete a stream + ```python stream.delete() ``` ## History +- 1.5.0 + - Update Stream Create,Get,List + - hub.create_stream() + - hub.get_stream() + - hub.list_streams() + - Add Stream operations else + - stream.to_json() + - stream.update() + - stream.disable() + - stream.enable() + - stream.rtmp_publish_url() + - stream.rtmp_live_urls() + - stream.hls_live_urls() + - stream.http_flv_live_urls() + - stream.status() + - stream.segments() + - stream.hls_playback_urls() + - stream.snapshot() + - stream.save_as() + - stream.delete() diff --git a/example.py b/example.py index 0f0f8c5..3c10296 100644 --- a/example.py +++ b/example.py @@ -2,9 +2,13 @@ access_key = "Qiniu_AccessKey" secret_key = "Qiniu_SecretKey" +hub_name = "Pili_Hub_Name" # The Hub must be exists before use -hub_name = "Pili_Hub_Name" - +# Change API host as necessary +# +# pili.qiniuapi.com as deafult +# pili-lte.qiniuapi.com is the latest RC version +# conf.API_HOST = 'pili-lte.qiniuapi.com' credentials = Credentials(access_key, secret_key) @@ -28,14 +32,14 @@ # "rtmp": "e4kvkh.live1-rtmp.z1.pili.qiniucdn.com" # }, # "playback": { -# "http": "e4kvkh.hls.z0.pili.qiniucdn.com" +# "http": "e4kvkh.playback1.z1.pili.qiniucdn.com" # }, # "publish": { -# "rtmp": "e4kvkh.pub.z0.pili.qiniup.com" +# "rtmp": "e4kvkh.publish.z1.pili.qiniup.com" # } # }, # "updatedAt": "2015-08-24T16:47:26.786Z", -# "id": "z0.test-origin.55db4a9ee3ba573b20000004", +# "id": "z1.test-origin.55db4a9ee3ba573b20000004", # "createdAt": "2015-08-24T16:47:26.786Z" # } @@ -55,14 +59,14 @@ # "rtmp": "e4kvkh.live1-rtmp.z1.pili.qiniucdn.com" # }, # "playback": { -# "http": "e4kvkh.hls.z0.pili.qiniucdn.com" +# "http": "e4kvkh.playback1.z1.pili.qiniucdn.com" # }, # "publish": { -# "rtmp": "e4kvkh.pub.z0.pili.qiniup.com" +# "rtmp": "e4kvkh.publish.z1.pili.qiniup.com" # } # }, # "updatedAt": "2015-08-24T16:47:26.786Z", -# "id": "z0.test-origin.55db4a9ee3ba573b20000004", +# "id": "z1.test-origin.55db4a9ee3ba573b20000004", # "createdAt": "2015-08-24T16:47:26.786Z" # } @@ -105,18 +109,14 @@ # "rtmp":"e4kvkh.live1-rtmp.z1.pili.qiniucdn.com" # }, # "playback":{ -# "http":"e4kvkh.hls.z0.pili.qiniucdn.com" -# }, -# "play":{ -# "hls":"e4kvkh.live1-http.z1.pili.qiniucdn.com", -# "rtmp":"e4kvkh.live1-rtmp.z1.pili.qiniucdn.com" +# "http":"e4kvkh.playback1.z1.pili.qiniucdn.com" # }, # "publish":{ -# "rtmp":"e4kvkh.pub.z0.pili.qiniup.com" +# "rtmp":"e4kvkh.publish.z1.pili.qiniup.com" # } # }, # "updatedAt":"2015-08-24T13:05:15.272975102-04:00", -# "id":"z0.test-origin.55db4ecae3ba573b20000006", +# "id":"z1.test-origin.55db4ecae3ba573b20000006", # "createdAt":"2015-08-24T13:05:14.526-04:00" # } @@ -130,20 +130,20 @@ # Get Stream status print "\nStream status()\n", stream.status() -# { -# "status":"disconnected", -# "framesPerSecond":{ -# "data":0, -# "audio":0, -# "video":0 -# }, -# "bytesPerSecond":0, -# "addr":"" +# { +# "addr": "222.73.202.226:2572", +# "status": "connected", +# "bytesPerSecond": 16870.200000000001, +# "framesPerSecond": { +# "audio": 42.200000000000003, +# "video": 14.733333333333333, +# "data": 0.066666666666666666, +# } # } # Generate RTMP publish URL print "\nStream rtmp_publish_url()\n", stream.rtmp_publish_url() -# rtmp://e4kvkh.pub.z0.pili.qiniup.com/test-origin/55db52e1e3ba573b2000000e?key=new_secret_words +# rtmp://e4kvkh.publish.z1.pili.qiniup.com/test-origin/55db52e1e3ba573b2000000e?key=new_secret_words # Generate RTMP live play URLs print "\nStream rtmp_live_urls()\n", stream.rtmp_live_urls() @@ -157,11 +157,54 @@ print "\nStream http_flv_live_urls()\n", stream.http_flv_live_urls() # {"ORIGIN": "http://e4kvkh.live1-http.z1.pili.qiniucdn.com/test-origin/55db52e1e3ba573b2000000e.flv"} +# Get Stream segments +# start_second : optional, int64, in second, unix timestamp +# end_second : optional, int64, in second, unix timestamp +# limit : optional, uint32 +# ...but you must provide both or none of the arguments. +print "\nStream segments()\n", stream.segments() +# [ +# { +# "start": 1440282134, +# "end": 1440437833 +# }, +# { +# "start": 1440437981, +# "end": 1440438835 +# } +# ] + # Generate HLS playback URLs -# start : required, int64, in second, unix timestamp -# end : required, int64, in second, unix timestamp -print "\nStream hls_playback_urls(1440282134, 1440437833)\n", stream.hls_playback_urls() -# {"ORIGIN": "http://e4kvkh.hls.z0.pili.qiniucdn.com/test-origin/55db5699e3ba573b20000010.m3u8?start=1440282134&end=1440437833"} +# start = 1440282134 : required, int64, in second, unix timestamp +# end = 1440437833 : required, int64, in second, unix timestamp +print "\nStream hls_playback_urls(1440282134, 1440437833)\n", stream.hls_playback_urls(start, end) +# {"ORIGIN": "http://e4kvkh.playback1.z1.pili.qiniucdn.com/test-origin/55db5699e3ba573b20000010.m3u8?start=1440282134&end=1440437833"} + +# Save Stream as a file +# name : required, string +# format : required, string, see http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html +# start : required, int64, in second, unix timestamp +# end : required, int64, in second, unix timestamp +# notifyUrl : optional, string +res = stream.save_as(name="videoName.mp4", format="mp4", start=1440282134, end=1440437833, notifyUrl=None) +print res +# { +# "url": "http://ey636h.vod1.z1.pili.qiniucdn.com/recordings/z1.test-origin.55db5699e3ba573b20000010/videoName.m3u8", +# "targetUrl": "http://ey636h.vod1.z1.pili.qiniucdn.com/recordings/z1.test-origin.55db5699e3ba573b20000010/videoName.mp4", +# "persistentId": "z1.55d81c6c7823de5a49ad77b3" +# } + +# Snapshot stream +# name : required, string +# format : required, string see http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html +# time : optional, int64, in second, unix timestamp +# notifyUrl : optional, string +res = stream.snapshot(name="imageName.jpg", format="jpg", time=None, notifyUrl=None) +print res +# { +# "targetUrl": "http://ey636h.static1.z1.pili.qiniucdn.com/snapshots/z1.test-origin.55db5699e3ba573b20000010/imageName.jpg", +# "persistentId": "z1.55d81c247823de5a49ad729c" +# } # Delete a Stream stream.delete() diff --git a/pili/api.py b/pili/api.py index 14bfece..25fe162 100644 --- a/pili/api.py +++ b/pili/api.py @@ -1,7 +1,3 @@ -""" -All api for access pili. -More Info: http://pili-io.github.io/docs/v1/index.html -""" from .auth import auth_interface import pili.conf as conf from urllib2 import Request @@ -80,4 +76,3 @@ def snapshot_stream(stream_id, **args): encoded = json.dumps(args) url = "http://%s/%s/streams/%s/snapshot" % (conf.API_HOST, conf.API_VERSION, stream_id) return Request(url=url, data=encoded) - diff --git a/pili/stream.py b/pili/stream.py index 1ebd37c..bee74cb 100644 --- a/pili/stream.py +++ b/pili/stream.py @@ -5,7 +5,6 @@ import json import time - class Stream(object): """ Stream is used to control a stream. You should always create a Stream object by the @@ -121,4 +120,4 @@ def rtmp_publish_url(self): return url def to_json(self): - return json.dumps(self.data) \ No newline at end of file + return json.dumps(self.data)