如今的直播平台到处都是了,自己动手用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放入项目文件夹中
![63b8f6f73d99a5510fc7e4869430f893.png](https://i-blog.csdnimg.cn/blog_migrate/e2186bff5109348e0d65de68d1311530.jpeg)
![748d0dbced49f229f02208b79e6d8256.png](https://i-blog.csdnimg.cn/blog_migrate/649225ae19a956b6aa08833621e5fca0.jpeg)
![63cef5e026cb360e01b90f580dda0251.png](https://i-blog.csdnimg.cn/blog_migrate/a0020fc77c53f15f570da86dd897e6a2.jpeg)
核心就是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 liveRequestV20161101DescribeLiveStreamsPublishListRequest();$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 liveRequestV20161101DescribeLiveStreamsPublishListRequest(); $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)); }}
返回的数据
![f960e1019ef15dc66bc85ff7c4713cb9.png](https://i-blog.csdnimg.cn/blog_migrate/7613d27c7aa02d23348c0e91bcca51d0.jpeg)
其他的接口请求也都类似这样,就不在这里一一列举,更多会用到的API,你是要查看API文档的,地址:https://help.aliyun.com/document_detail/48207.html
视频直播生成推流地址和播放地址的程序示例
直播推流地址和播放地址本身没有API接口,需要在客户端自行拼接地址,然后使用推流工具或者播放器对其推流或播放。
示例代码
<?phpfunction 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 = 'http://'.$play_domain.'/'.$appName.'/'.$streamName.'.flv'; $hls_play_url = 'http://'.$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 = 'http://'.$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 = 'http://'.$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全套视频,免费分享!!
非常抱歉由于头条规则无法发放资料外链,领取方式:点赞关注小编后私信【资料】获取资料领取方式!
部分资料展示:
![0be021b60b2df8f08b3c0e0edb9f431f.gif](https://i-blog.csdnimg.cn/blog_migrate/1ab344cacfdb0e9418b38ad661ed1547.gif)
![fc5d2916a341d47253e5c22423560a10.gif](https://i-blog.csdnimg.cn/blog_migrate/f8c9d408458a198d6a324d7294e722bc.gif)
![fff4d1be809f9651729e288a1739bc3a.png](https://i-blog.csdnimg.cn/blog_migrate/3530153719d00ac5e600066c575e9718.jpeg)
![02de08f715b25a54ee003bdbc144d539.png](https://i-blog.csdnimg.cn/blog_migrate/3918ff69c87ff88ad4a7087e95dae99d.jpeg)
![43e2aaa6e4ffbbd7232dda819816aa0b.png](https://i-blog.csdnimg.cn/blog_migrate/84d8e27c079c83a86cef8c5bb65bf072.jpeg)