Pili Streaming Cloud server-side library for Ruby
Features
- Stream Create,Get,List
- Stream operations else
Contents
Installation
Add this line to your application's Gemfile:
gem 'pili'
And then execute:
$ bundle
Or install it yourself as:
$ gem install pili
Usage
Configuration
require 'pili'
ACCESS_KEY = 'Qiniu_AccessKey'
SECRETE_KEY = 'Qiniu_SecretKey'
HUB_NAME = 'Pili_Hub_Name'
Hub
Instantiate a Pili Hub object
credentials = Pili::Credentials.new(ACCESS_KEY, SECRETE_KEY)
hub = Pili::Hub.new(credentials, HUB_NAME)
puts "Hub initialize =>\n#{hub.inspect}\n\n"
Create a new Stream
begin
title = nil
publish_key = nil
publish_security = nil
stream = hub.create_stream(title: title, publish_key: publish_key, publish_security: publish_security)
puts "Hub create_stream() =>\n#{stream.inspect}\n\n"
rescue Exception => e
puts "Hub create_stream() failed. Caught exception:\n#{e.http_body}\n\n"
end
Get a Stream
begin
stream = hub.get_stream(stream.id)
puts "Hub get_stream() =>\n#{stream.inspect}\n\n"
rescue Exception => e
puts "Hub get_stream() failed. Caught exception:\n#{e.http_body}\n\n"
end
List Streams
begin
status = nil
marker = nil
limit = nil
title = nil
streams = hub.list_streams(status: status, marker: marker, limit: limit, title: title)
puts "Hub list_streams() =>\n#{streams.inspect}\n\n"
rescue Exception => e
puts "Hub list_streams() failed. Caught exception:\n#{e.http_body}\n\n"
end
Stream
To JSON String
json_string = stream.to_json()
puts "Stream stream.to_json() =>\n#{json_string}\n\n"
Update a Stream
begin
stream.publish_key = "new_secret_words"
stream.publish_security = "static"
stream.disabled = nil
stream = stream.update()
puts "Stream update() =>\n#{stream.inspect}\n\n"
rescue Exception => e
puts "Stream update() failed. Caught exception:\n#{e.http_body}\n\n"
end
Disable a Stream
begin
stream = stream.disable()
puts "Stream disable() =>\n#{stream.inspect}\n\n"
rescue Exception => e
puts "Stream disable() failed. Caught exception:\n#{e.http_body}\n\n"
end
Enable a Stream
begin
stream = stream.enable()
puts "Stream enable() =>\n#{stream.inspect}\n\n"
rescue Exception => e
puts "Stream enable() failed. Caught exception:\n#{e.http_body}\n\n"
end
Get Stream status
begin
status_info = stream.status()
puts "Stream status() =>\n#{status_info.inspect}\n\n"
rescue Exception => e
puts "Stream status() failed. Caught exception:\n#{e.http_body}\n\n"
end
Generate RTMP publish URL
publish_url = stream.rtmp_publish_url()
puts "Stream rtmp_publish_url() =>\n#publish_url}\n\n"
Generate RTMP live play URLs
urls = stream.rtmp_live_urls()
puts "Stream rtmp_live_urls() =>\n#{urls.inspect}\n\n"
Generate HLS live play URLs
urls = stream.hls_live_urls()
puts "Stream hls_live_urls() =>\n#{urls.inspect}\n\n"
Generate HTTP-FLV live play URLs
urls = stream.http_flv_live_urls()
puts "Stream http_flv_live_urls() =>\n#{urls.inspect}\n\n"
Get Stream segments
begin
start_time = nil
end_time = nil
limit = nil
segments = stream.segments(start_time: start_time, end_time: end_time, limit: limit)
puts "Stream segments() =>\n#{segments.inspect}\n\n"
rescue Exception => e
puts "Stream segments() failed. Caught exception:\n#{e.http_body}\n\n"
end
Generate HLS playback URLs
start_time = 1440196065
end_time = 1440196105
urls = stream.hls_playback_urls(start_time, end_time)
puts "Stream hls_playback_urls() =>\n#{urls.inspect}\n\n"
Save Stream as a file
begin
name = "videoName.mp4"
start_time = 1440067100
end_time = 1440068104
format = "mp4"
notify_url = nil
pipeline = nil
result = stream.save_as(name, format, start_time, end_time, notify_url, pipeline)
puts "Stream save_as() =>\n#{result.inspect}\n\n"
rescue Exception => e
puts "Stream save_as() failed. Caught exception:\n#{e.http_body}\n\n"
end
While invoking saveAs() and snapshot(), you can get processing state via Qiniu FOP Service using persistentId.
API: curl -D GET http://api.qiniu.com/status/get/prefop?id={PersistentId}
Doc reference: http://developer.qiniu.com/docs/v6/api/overview/fop/persistent-fop.html#pfop-status
Snapshot Stream
begin
name = "imageName.jpg"
format = "jpg"
options = {
:time => 1440067100,
:notify_url => nil
}
result = stream.snapshot(name, format, options)
puts "Stream snapshot() =>\n#{result.inspect}\n\n"
rescue Exception => e
puts "Stream snapshot() failed. Caught exception:\n#{e.http_body}\n\n"
end
Delete a Stream
begin
result = stream.delete()
puts "Stream delete() =>\n#{result.inspect}\n\n"
rescue Exception => e
puts "Stream delete() failed. Caught exception:\n#{e.http_body}\n\n"
end
History
- 1.5.5
- 1.5.4
- 1.5.3
- Use saveas in hls_playback_urls
- 1.5.0
- Add stream.http_flv_live_urls()
- Add stream.disable()
- Add stream.enable()
- Add stream.snapshot()
- 1.3.0
- 1.2.0
- 1.0.1
- Add stream.status()
- Update stream.update()
- 1.0.0
- Update README create stream example code.
- 0.1.3
- Update README create stream example code.
- 0.1.2
- Fix stream list method parameter type.
- 0.1.0
- Update README
- Update get milliseconds method
- 0.0.1
- Init SDK
- Add Stream API
- Add publish and play policy