URL
Hub
Stream
before next step, install git.
// install latest version
$ go get github.com/pili-engineering/pili-sdk-go.v2/pili
package main
import (
// ...
"github.com/pili-engineering/pili-sdk-go.v2/pili"
)
var (
AccessKey = "<QINIU ACCESS KEY>" // 替换成自己 Qiniu 账号的 AccessKey.
SecretKey = "<QINIU SECRET KEY>" // 替换成自己 Qiniu 账号的 SecretKey.
HubName = "<PILI HUB NAME>" // Hub 必须事先存在.
)
func main() {
// ...
mac := &pili.MAC{AccessKey, []byte(SecretKey)}
client := pili.New(mac, nil)
// ...
}
url := pili.RTMPPublishURL("publish-rtmp.test.com", "PiliSDKTest", "streamkey", mac, 60)
fmt.Println(url)
/*
rtmp://publish-rtmp.test.com/PiliSDKTest/streamkey?e=1463023142&token=7O7hf7Ld1RrC_fpZdFvU8aCgOPuhw2K4eapYOdII:-5IVlpFNNGJHwv-2qKwVIakC0ME=
*/
url := pili.RTMPPlayURL("live-rtmp.test.com", "PiliSDKTest", "streamkey")
fmt.Println(url)
/*
rtmp://live-rtmp.test.com/PiliSDKTest/streamkey
*/
url := pili.HLSPlayURL("live-hls.test.com", "PiliSDKTest", "streamkey")
fmt.Println(url)
/*
http://live-hls.test.com/PiliSDKTest/streamkey.m3u8
*/
url := pili.HDLPlayURL("live-hdl.test.com", "PiliSDKTest", "streamkey")
fmt.Println(url)
/*
http://live-hdl.test.com/PiliSDKTest/streamkey.flv
*/
url := pili.SnapshotPlayURL("live-snapshot.test.com", "PiliSDKTest", "streamkey")
fmt.Println(url)
/*
http://live-snapshot.test.com/PiliSDKTest/streamkey.jpg
*/
func main() {
mac := &pili.MAC{AccessKey, []byte(SecretKey)}
client := pili.New(mac, nil)
hub := client.Hub("PiliSDKTest")
// ...
}
stream, err := hub.Create(key)
if err != nil {
return
}
info, err := stream.Info()
if err != nil {
return
}
fmt.Println(info)
/*
{hub:PiliSDKTest,key:streamkey,disabled:false}
*/
stream := hub.Stream(key)
info, err := stream.Info()
if err != nil {
return
}
fmt.Println(info)
/*
{hub:PiliSDKTest,key:streamkey,disabled:false}
*/
keys, marker, err := hub.List(prefix, 10, "")
if err != nil {
return
}
fmt.Printf("keys=%v marker=%v\n", keys, marker)
/*
keys=[streamkey] marker=
*/
keys, marker, err := hub.ListLive(prefix, 10, "")
if err != nil {
return
}
fmt.Printf("keys=%v marker=%v\n", keys, marker)
/*
keys=[streamkey] marker=
*/
items, err := hub.BatchLiveStatus(streams)
if err != nil {
return
}
fmt.Println(items)
/*
[{streamKey1 {1487766696 172.21.2.14:63422 1042240 {46 24 0}}} {streamKey2 {1487768638 172.21.2.14:63793 1201352 {51 22 0}}}]
*/
stream := hub.Stream(key)
info, err := stream.Info()
if err != nil {
return
}
fmt.Println(info)
/*
{hub:PiliSDKTest,key:streamkey,disabled:false}
*/
stream := hub.Stream(key)
info, err := stream.Info()
if err != nil {
return
}
fmt.Println("before disable:", info)
err = stream.DisableTill(time.Now().Add(time.Minute).Unix())
if err != nil {
return
}
info, err = stream.Info()
if err != nil {
return
}
fmt.Println("after call disable:", info)
time.Sleep(time.Minute)
info, err = stream.Info()
if err != nil {
return
}
fmt.Println("after time.Minute:", info)
/*
before disable: {hub:PiliSDKTest,key:sdkexample1487765261435788231A,disabled:false}
after call disable: {hub:PiliSDKTest,key:sdkexample1487765261435788231A,disabled:true}
after time.Minute: {hub:PiliSDKTest,key:sdkexample1487765261435788231A,disabled:false}
*/
stream := hub.Stream(key)
info, err := stream.Info()
if err != nil {
return
}
fmt.Println("before enable:", info)
err = stream.Enable()
if err != nil {
return
}
info, err = stream.Info()
if err != nil {
return
}
fmt.Println("after enable:", info)
/*
before enable: {hub:PiliSDKTest,key:streamkey,disabled:true}
after enable: {hub:PiliSDKTest,key:streamkey,disabled:false}
*/
stream := hub.Stream(key)
status, err := stream.LiveStatus()
if err != nil {
return
}
fmt.Printf("%+v\n", status)
/*
&{StartAt:1463382400 ClientIP:172.21.1.214:52897 BPS:128854 FPS:{Audio:38 Video:23 Data:0}}
*/
stream := hub.Stream(key)
records, err := stream.HistoryActivity(0, 0)
if err != nil {
return
}
fmt.Println(records)
/*
[{1463382401 1463382441}]
*/
stream := hub.Stream(key)
opts := &pili.SaveasOptions{
Format: "mp4",
}
fname, persistentID, err := stream.Saveas(opts)
if err != nil {
return
}
fmt.Println(fname, persistentID)
/*
recordings/z1.PiliSDKTest.streamkey/1463156847_1463157463.mp4 z1.58ae57dc254f0e4d3f00000e
*/
stream := hub.Stream(key)
info, err := stream.Info()
if err != nil {
return
}
fmt.Println("before UpdateConverts:", info)
err = stream.UpdateConverts([]string{"480p", "720p"})
if err != nil {
return
}
info, err = stream.Info()
if err != nil {
return
}
fmt.Println("after UpdateConverts:", info)
/*
before UpdateConverts: {hub:PiliSDKTest,key:sdkexample1487834862156173949A,disabled:false,converts:[]}
after UpdateConverts: {hub:PiliSDKTest,key:sdkexample1487834862156173949A,disabled:false,converts:[480p 720p]}
*/
stream := hub.Stream(key)
opts := &pili.SnapshotOptions{
Format: "jpg",
}
fname, err := stream.Snapshot(opts)
if err != nil {
return
}
fmt.Println(fname)
/*
streamkey-2741961933532577110.jpg
*/