【实战教程】PHP如何轻松对接阿里云直播?-LMLPHP

1. 配置阿里云直播的推流地址和播放地址

使用阿里云直播功能前,首先需要在阿里云控制台中创建直播应用,然后获取推流地址和播放地址。

推流地址一般格式为:

rtmp://{Domain}/{AppName}/{StreamName}?auth_key={AuthKey}-{Timestamp}-{RandomNum}

Copy

其中,

{Domain}代表阿里云直播的推流域名;

{AppName}代表应用名称,一般为“live”,也可以自定义;

{StreamName}代表流名称,可以自定义;

{AuthKey}代表授权密钥;

{Timestamp}代表当前时间戳;

{RandomNum}代表随机数。

播放地址一般格式为:

http://{Domain}/{AppName}/{StreamName}.m3u8

Copy

{Domain}代表阿里云直播的播放域名;

{AppName}代表应用名称,一般为“live”,也可以自定义;

{StreamName}代表流名称,可以自定义。

把获取到的推流地址和播放地址配置到代码中,代码如下:

class LiveAction extends Action {
    // 推流地址
    private $pushUrl = 'rtmp://{Domain}/{AppName}/{StreamName}?auth_key={AuthKey}-{Timestamp}-{RandomNum}';

    // 播放地址
    private $playUrl = 'http://{Domain}/{AppName}/{StreamName}.m3u8';

    // 阿里云直播的推流域名
    private $pushDomain = 'xxx.xxx.com';

    // 阿里云直播的播放域名
    private $playDomain = 'xxx.xxx.com';

    // 应用名称
    private $appName = 'live';

    // 流名称
    private $streamName = 'test';

    // 授权密钥
    private $authKey = '1234567890';

    // 获取推流地址
    private function getPushUrl() {
        $randomNum = rand(100000, 999999);
        $timestamp = time();
        $authKey = md5($this->authKey . $this->appName . $this->streamName . $timestamp . $randomNum);
        $pushUrl = str_replace(array('{Domain}', '{AppName}', '{StreamName}', '{AuthKey}', '{Timestamp}', '{RandomNum}'), array($this->pushDomain, $this->appName, $this->streamName, $authKey, $timestamp, $randomNum), $this->pushUrl);
        return $pushUrl;
    }

    // 获取播放地址
    private function getPlayUrl() {
        $playUrl = str_replace(array('{Domain}', '{AppName}', '{StreamName}'), array($this->playDomain, $this->appName, $this->streamName), $this->playUrl);
        return $playUrl;
    }
}

Copy

LiveAction中定义了一系列变量,包括推流地址和播放地址的格式和一些基本的配置信息,同时定义了两个私有方法,分别用于获取推流地址和播放地址。

getPushUrl方法中,先生成一个六位的随机数和当前时间戳,然后计算出授权密钥,最后将这些参数替换到推流地址的相应位置。最终返回一个完整的推流地址。

getPlayUrl方法中,直接将播放地址的相应位置替换即可。最终返回一个完整的播放地址。

【实战教程】PHP如何轻松对接阿里云直播?-LMLPHP

2. 在ThinkPHP中集成阿里云直播的推流功能

在ThinkPHP框架中,可以使用Fmpeg库来实现推流的功能。Fmpeg是一个非常强大的音视频处理工具,它不仅可以播放、转码音视频,还可以进行音视频的编辑、剪辑等等。

在使用Fmpeg之前,需要先安装Fmpeg库,并把它的路径配置到环境变量中。

代码如下:

class LiveAction extends Action {
    // 推流地址
    private $pushUrl = 'rtmp://{Domain}/{AppName}/{StreamName}?auth_key={AuthKey}-{Timestamp}-{RandomNum}';

    // 阿里云直播的推流域名
    private $pushDomain = 'xxx.xxx.com';

    // 应用名称
    private $appName = 'live';

    // 流名称
    private $streamName = 'test';

    // 授权密钥
    private $authKey = '1234567890';

    // 获取推流地址
    private function getPushUrl() {
        $randomNum = rand(100000, 999999);
        $timestamp = time();
        $authKey = md5($this->authKey . $this->appName . $this->streamName . $timestamp . $randomNum);
        $pushUrl = str_replace(array('{Domain}', '{AppName}', '{StreamName}', '{AuthKey}', '{Timestamp}', '{RandomNum}'), array($this->pushDomain, $this->appName, $this->streamName, $authKey, $timestamp, $randomNum), $this->pushUrl);
        return $pushUrl;
    }

    // 推流
    public function push() {
        $pushUrl = $this->getPushUrl();
        $command = 'ffmpeg -re -i test.flv -vcodec copy -acodec aac -f flv ' . $pushUrl;
        exec($command);
    }
}

Copy

LiveAction中添加了一个push方法,该方法利用Fmpeg库将本地的test.flv文件推流到阿里云直播中。

【实战教程】PHP如何轻松对接阿里云直播?-LMLPHP

3. 在ThinkPHP中集成阿里云直播的播放功能

在ThinkPHP框架中,可以使用Hls.js库来实现直播的播放功能。Hls.js是一个基于HTML5的JavaScript库,它能够将M3U8格式的直播流实时转换成模拟的FLV格式并播放。

代码如下:

class LiveAction extends Action {
    // 播放地址
    private $playUrl = 'http://{Domain}/{AppName}/{StreamName}.m3u8';

    // 阿里云直播的播放域名
    private $playDomain = 'xxx.xxx.com';

    // 应用名称
    private $appName = 'live';

    // 流名称
    private $streamName = 'test';

    // 获取播放地址
    private function getPlayUrl() {
        $playUrl = str_replace(array('{Domain}', '{AppName}', '{StreamName}'), array($this->playDomain, $this->appName, $this->streamName), $this->playUrl);
        return $playUrl;
    }

    // 播放
    public function play() {
        $playUrl = $this->getPlayUrl();
        $this->assign('playUrl', $playUrl);
        $this->display();
    }
}

Copy

LiveAction中添加了一个play方法,该方法获取播放地址并分配给模板,然后通过模板display方法展示到页面上。

在页面上可以使用Hls.js库来播放直播流。

完整代码如下:

class LiveAction extends Action {
    // 推流地址
    private $pushUrl = 'rtmp://{Domain}/{AppName}/{StreamName}?auth_key={AuthKey}-{Timestamp}-{RandomNum}';


    // 阿里云直播的推流域名
    private $pushDomain = 'xxx.xxx.com';


    // 播放地址
    private $playUrl = 'http://{Domain}/{AppName}/{StreamName}.m3u8';


    // 阿里云直播的播放域名
    private $playDomain = 'xxx.xxx.com';


    // 应用名称
    private $appName = 'live';


    // 流名称
    private $streamName = 'test';


    // 授权密钥
    private $authKey = '1234567890';


    // 获取推流地址
    private function getPushUrl() {
        $randomNum = rand(100000, 999999);
        $timestamp = time();
        $authKey = md5($this->authKey . $this->appName . $this->streamName . $timestamp . $randomNum);
        $pushUrl = str_replace(array('{Domain}', '{AppName}', '{StreamName}', '{AuthKey}', '{Timestamp}', '{RandomNum}'), array($this->pushDomain, $this->appName, $this->streamName, $authKey, $timestamp, $randomNum), $this->pushUrl);
        return $pushUrl;
    }


    // 获取播放地址
    private function getPlayUrl() {
        $playUrl = str_replace(array('{Domain}', '{AppName}', '{StreamName}'), array($this->playDomain, $this->appName, $this->streamName), $this->playUrl);
        return $playUrl;
    }


    // 推流
    public function push() {
        $pushUrl = $this->getPushUrl();
        $command = 'ffmpeg -re -i test.flv -vcodec copy -acodec aac -f flv ' . $pushUrl;
        exec($command);
    }


    // 播放
    public function play() {
        $playUrl = $this->getPlayUrl();
        $this->assign('playUrl', $playUrl);
        $this->display();
    }
}
12-03 21:46