如今的直播平台到处都是了,自己动手用PHP去做一个视频直播,那是可以的,你可以用到swoole。但是会花很长时间去开发与测试吧!也不知道开发出来的效果会不会不稳定,所以为了节省更多的时间,开发视频直播基本都会用到第三方稳定的API,今天要讲到的是用PHP与阿里云视频直播API开发视频直播
swoole做直播具体步骤如下:(带摄像头的笔记本)
1、Swoole创建 2个监听 一个WebSocket监听服务(用于视频流传输)。一个本地 【Unix Socket文件描述符】(用于ffmpeg转码后的视频流接受与转发给WebSocket客户端。)
2、浏览器使用 jsmpeg 这个项目连接 Swoole 提供的WebSocket 服务来获取视频流播放。
3、使用ffmpeg 把直播的视频流 转码 为 mpeg 视频流格式 发送到 本地的Swoole 监听的【Unix Socket】文件描述符
比如:$serv->addlistener(“/var/run/myserv.sock”, 0,SWOOLE_UNIX_STREAM); 文件描述符:/var/run/myserv.sock
4、Swoole 监听的文件描述符 的 onReceive 事件会收到 ffmpeg 转码的视频流。在通过 Swoole的WebSocket方法push 给连接到 WebSocket服务的 客户端发送视频流。
PHP与阿里云视频直播API开发视频直播
1.下载sdk放入项目文件夹中
核心就是aliyun-php-sdk-core,它的配置文件会自动加载相应的类
2.引入文件
include_once LIB_PATH . 'ORG/aliyun-openapi/aliyun-php-sdk-core/Config.php';
3.配置客户端对象,需要Access Key ID,Access Key Secret
$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", "xxxx", "xxxx"); // Access Key ID,Access Key Secret
$client = new DefaultAcsClient($iClientProfile);
4.调用请求类,并配置参数,就拿直播推流历史为例
$request = new live\Request\V20161101\DescribeLiveStreamsPublishListRequest();
$request ->setDomainName('live.yunlutong.com');
$request ->setAppName('yunlutong');
$request ->setStreamName('demo');
$request ->setStartTime('2020-03-01T19:00:00Z');
$request ->setEndTime('2020-03-29T19:00:00Z');
5.发起请求
//针对阿里云进行请求
$response = $client->getAcsResponse($request);
exit(json_encode($response));
完整代码如下
<?php
/**
* 直播相关接口
*/class LiveAction extends ApiAction
{
protected function _initialize()
{
parent::_initialize();
}
// 获取推流历史
public function DescribeLiveStreamsPublishList() {
include_once LIB_PATH . 'ORG/aliyun-openapi/aliyun-php-sdk-core/Config.php';
$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", "xxxx", "xxxx"); // Access Key ID,Access Key Secret
$client = new DefaultAcsClient($iClientProfile);
$request = new live\Request\V20161101\DescribeLiveStreamsPublishListRequest();
$request ->setDomainName('live.yunlutong.com');
$request ->setAppName('yunlutong');
$request ->setStreamName('demo');
$request ->setStartTime('2020-03-01T19:00:00Z');
$request ->setEndTime('2020-03-29T19:00:00Z');
//针对阿里云进行请求
$response = $client->getAcsResponse($request);
exit(json_encode($response));
}
}
返回的数据
其他的接口请求也都类似这样,就不在这里一一列举,更多会用到的API,你是要查看API文档的,地址:
视频直播生成推流地址和播放地址的程序示例
直播推流地址和播放地址本身没有API接口,需要在客户端自行拼接地址,然后使用推流工具或者播放器对其推流或播放。
示例代码
<?php
function push_url($push_domain,$push_key,$expireTime,$appName,$streamName){
$push_url = '';
//未开启鉴权Key的情况下
if($push_key==''){
$push_url = 'rtmp://'.$push_domain.'/'.$appName.'/'.$streamName;
echo $push_url;
return;
}
$timeStamp = time() + $expireTime;
$sstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$push_key;
$md5hash = md5($sstring);
$push_url = 'rtmp://'.$push_domain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$md5hash;
echo $push_url;
echo PHP_EOL;
return;
}
function play_url($play_domain,$play_key,$expireTime,$appName,$streamName){
//未开启鉴权Key的情况下
if($play_key==''){
$rtmp_play_url = 'rtmp://'.$play_domain.'/'.$appName.'/'.$streamName;
$flv_play_url = '#39;.$play_domain.'/'.$appName.'/'.$streamName.'.flv';
$hls_play_url = '#39;.$play_domain.'/'.$appName.'/'.$streamName.'.m3u8';
}else{
$timeStamp = time() + $expireTime;
$rtmp_sstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$play_key;
$rtmp_md5hash = md5($rtmp_sstring);
$rtmp_play_url = 'rtmp://'.$play_domain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$rtmp_md5hash;
$flv_sstring = '/'.$appName.'/'.$streamName.'.flv-'.$timeStamp.'-0-0-'.$play_key;
$flv_md5hash = md5($flv_sstring);
$flv_play_url = '#39;.$play_domain.'/'.$appName.'/'.$streamName.'.flv?auth_key='.$timeStamp.'-0-0-'.$flv_md5hash;
$hls_sstring = '/'.$appName.'/'.$streamName.'.m3u8-'.$timeStamp.'-0-0-'.$play_key;
$hls_md5hash = md5($hls_sstring);
$hls_play_url = '#39;.$play_domain.'/'.$appName.'/'.$streamName.'.m3u8?auth_key='.$timeStamp.'-0-0-'.$hls_md5hash;
}
echo 'rtmp播放地址: '.$rtmp_play_url;
echo PHP_EOL;
echo 'flv播放地址: '.$flv_play_url;
echo PHP_EOL;
echo 'hls播放地址: '.$hls_play_url;
echo PHP_EOL;
return;
}
//推流域名
$push_domain = 'testpush.cn';
//推流域名配置的鉴权Key
$push_key = 'test_Push_key';
//生成随机的AppName
$appName = md5(uniqid(microtime(true),true));
//生成随机的StreamNmae
$streamName = md5(uniqid(microtime(true),true));
//配置过期时间为1小时
$expireTime = 3600;
//播放域名
$play_domain = 'testPlay.cn';
//播放域名配置的鉴权Key
$play_key = 'test_play_key';
push_url($push_domain,$push_key,$expireTime,$appName,$streamName);
play_url($play_domain,$play_key,$expireTime,$appName,$streamName);
?>
啥?swoole你还不会,小编这里准备swoole全套视频,免费分享!!
非常抱歉由于头条规则无法发放资料外链,领取方式:点赞关注小编后私信【资料】获取资料领取方式!
部分资料展示: