利用雲視頻實如今線教育和美女主播系統

利用雲視頻實如今線教育和美女主播系統。php

最近在整視頻直播,相對其餘web應用。視頻直播相對來講仍是有點複雜。使用FMS搭建了服務端測試一下,直播仍是不夠穩定。後來試了下阿里雲視頻服務,感受還能夠,可是它沒有提供客戶端。css

而後找到了網易雲視頻,它有提供了客戶端,試用一下,網易雲延遲比阿里雲會低點,而後就選他做爲視頻直播服務。html

網易雲的api示例是java的,問客服有沒有php的,而後發給我一個網易雲信的api 囧。java

沒辦法本身寫個,接口也簡單。git

class v163Class{
	private $AppKey;                //開發者平臺分配的AppKey
    private $AppSecret;             //開發者平臺分配的AppSecret,可刷新
    private $Nonce;					//隨機數(最大長度128個字符)
    private $CurTime;             	//當前UTC時間戳,從1970年1月1日0點0 分0 秒開始到如今的秒數(String)
    private $CheckSum;				//SHA1(AppSecret + Nonce + CurTime),三個參數拼接的字符串,進行SHA1哈希計算,轉化成16進制字符(String,小寫)
    const   HEX_DIGITS = "0123456789abcdef";
	public function __construct($AppKey,$AppSecret){
        $this->AppKey    = $AppKey;
        $this->AppSecret = $AppSecret;
    }
	/**生成驗證碼**/ 
	public function checkSumBuilder(){
    	//此部分生成隨機字符串
    	$hex_digits = self::HEX_DIGITS;
    	$this->Nonce;
    	for($i=0;$i<128;$i++){			//隨機字符串最大128個字符,也能夠小於該數
    		$this->Nonce.= $hex_digits[rand(0,15)];
    	}
    	$this->CurTime = (string)(time());	//當前時間戳,以秒爲單位

    	$join_string = $this->AppSecret.$this->Nonce.$this->CurTime;
    	$this->CheckSum = sha1($join_string);
 
    }
 
	/*****post請求******/
	public function postDataCurl($url,$data=array()){
    	$this->checkSumBuilder();		//發送請求前需先生成checkSum
		if(!empty($data)){
			$json=json_encode($data);
		}else{
			$json="";
		}
		$timeout = 5000;  
        $http_header = array(
            'AppKey:'.$this->AppKey,
            'Nonce:'.$this->Nonce,
            'CurTime:'.$this->CurTime,
            'CheckSum:'.$this->CheckSum,
            'Content-Type: application/json;charset=utf-8;',
			'Content-Length: ' . strlen($json)
        );
		$ch = curl_init(); 
		curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_POST, 1);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt ($ch, CURLOPT_HEADER, false); 
		curl_setopt ($ch, CURLOPT_HTTPHEADER,$http_header);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        
		$result = curl_exec($ch);  
        if (false === $result) {
            $result =  curl_errno($ch);
        }
		curl_close($ch); 
		return json_decode($result,true) ;
    } 
	/***頻道添加***/
	public function channel_add($name,$type=0){
		$url="https://vcloud.163.com/app/channel/create";
		return $data=$this->postDataCurl($url,array("name"=>$name,"type"=>$type));
	}
	/****頻道更新*****/
	public function channel_update($name,$cid,$type=0){
		$url="https://vcloud.163.com/app/channel/update";
		return $data=$this->postDataCurl($url,array("name"=>$name,"cid"=>$cid,"type"=>$type));
	}
	/****頻道刪除******/
	public function channel_delete($cid){
		$url="https://vcloud.163.com/app/channel/delete";
		return $data=$this->postDataCurl($url,array("cid"=>$cid));
	}
	/****獲取頻道信息******/
	public function channel_get($cid){
		$url="https://vcloud.163.com/app/channelstats";
		return $data=$this->postDataCurl($url,array("cid"=>$cid));
	}
	/***
	獲取頻道列表
	records	int	單頁記錄數,默認值爲10	否
	pnum	int	要取第幾頁,默認值爲1	否
	ofield	String	排序的域,支持的排序域爲:ctime(默認)	否
	sort	int	升序仍是降序,1升序,0降序,默認爲desc	否
	**/
	public function channel_list($option=array("records"=>10,"pnum"=>1,"ofield"=>"ctime","sort"=>1)){
		$url="https://vcloud.163.com/app/channellist";
		return $data=$this->postDataCurl($url,$option);
	}
	/**從新獲取推流地址***/
	public function channel_reset($cid){
		$url="https://vcloud.163.com/app/address";
		return $data=$this->postDataCurl($url,array("cid"=>$cid));
	}
	/*****
	設置頻道爲錄製狀態
	cid	String	頻道ID	是
	needRecord	int	1-開啓錄製; 0-關閉錄製	是
	format	int	1-flv; 0-mp4	是
	duration	int	錄製切片時長(分鐘),默認120分鐘	否
	filename	String	錄製後文件名,格式爲filename_YYYYMMDD-HHmmssYYYYMMDD-HHmmss, 
	文件名錄制起始時間(年月日時分秒) -錄製結束時間(年月日時分秒)	否
	****/
	
	public function channel_setRecord($cid,$option=array()){
		$url="https://vcloud.163.com/app/channel/setAlwaysRecord";
		return $data=$this->postDataCurl($url,$option);
	}
	/****暫停頻道*****/
	public function channel_pause($cid){
		$url="https://vcloud.163.com/app/channel/pause";
		return $data=$this->postDataCurl($url,array("cid"=>$cid));
	}
	/****批量暫停頻道****/
	public function channel_pauselist($cidList){
		$url="https://vcloud.163.com/app/channellist/pause";
		return $data=$this->postDataCurl($url,array("cidList"=>$cidList));
	}
	/****恢復頻道*****/
	public function channel_resume($cid){
		$url="https://vcloud.163.com/app/channel/resume";
		return $data=$this->postDataCurl($url,array("cid"=>$cid));
	}
	/****批量恢復頻道****/
	public function channel_resumelist($cidList){
		$url="https://vcloud.163.com/app/channellist/resume";
		return $data=$this->postDataCurl($url,array("cidList"=>$cidList));
	}
	/****獲取頻道的視頻地址*****/
	public function channel_videolist($cid){
		$url="https://vcloud.163.com/app/videolist";
		return $data=$this->postDataCurl($url,array("cid"=>$cid));
	}
	
	
}

網易有提供window客戶端,在使用的時候出現卡頓現象,因此仍是直接使用OBS。OBS是款免費的視頻直播客戶端,配置也簡單。在串流裏填下url便可開始直播。web

這樣就能夠開始直播。json

播放器的話使用video.js便可。api

<video id="zbvideo" class="video-js vjs-default-skin" controls preload="none" width="90%" height="398" poster="/static/images/videobg.jpg" data-setup="{}">
         <source src="{$data.zb_http}" />
          <source src="{$data.zb_hls}"  type="application/x-mpegURL"  />
          <source src="{$data.zb_rtmp}" type="rtmp" />
        </video>
<link href="/plugin/videojs/video-js.css" rel="stylesheet">
<script src="/plugin/videojs/ie8/videojs-ie8.min.js"></script> 
<script src="/plugin/videojs/video.js"></script>

這樣就完成一個直播服務了。app

我搭建了一個公開課 curl

添加公開課根據api自動生成直播地址,刷新直播地址,到期自動刪除直播地址。

演示地址:http://www.deitui.com/index.php?m=openclass  固然有直播的時候才能看視頻直播。

相關文章
相關標籤/搜索