Starting from scratch: A simple way to implement Alibaba Cloud live broadcast with PHP!

Insert image description here

1. Configure the push address and playback address of Alibaba Cloud Live

Before using the Alibaba Cloud live broadcast function, you first need to create a live broadcast application in the Alibaba Cloud console, and then obtain the push address and playback address.

The general format of the push address is:

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

in,

{Domain}Represents the push domain name of Alibaba Cloud Live Broadcast;

{AppName}Represents the application name, usually "live", which can also be customized;

{StreamName}Represents the stream name, which can be customized;

{AuthKey}Represents the authorization key;

{Timestamp}Represents the current timestamp;

{RandomNum}Represents a random number.

The general format of the playback address is:

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

{Domain}Represents the playback domain name of Alibaba Cloud Live Broadcast;

{AppName}Represents the application name, usually "live", which can also be customized;

{StreamName}Represents the stream name and can be customized.

Configure the obtained push address and playback address into the code. The code is as follows:

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;
    }
}

defines a series of variables inLiveAction, including the format of push address and playback address and some basic configuration information. It also defines two private methods for obtaining Push address and playback address.

IngetPushUrl method, first generate a six-digit random number and the current timestamp, then calculate the authorization key, and finally replace these parameters into the corresponding positions of the push address . Finally, a complete push address is returned.

IngetPlayUrl method, just replace the corresponding position of the playback address directly. Finally returns a complete playback address.

Insert image description here

2. Integrate the streaming function of Alibaba Cloud Live in ThinkPHP

In the ThinkPHP framework, you can use the Fmpeg library to implement the streaming function. Fmpeg is a very powerful audio and video processing tool. It can not only play and transcode audio and video, but also edit and edit audio and video, etc.

Before using Fmpeg, you need to install the Fmpeg library and configure its path to the environment variable.

code show as below:

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);
    }
}

Added a method inLiveAction, which uses the Fmpeg library to push the local test.flv file to Alibaba Cloud Live. push
Insert image description here

3. Integrate the playback function of Alibaba Cloud Live in ThinkPHP

In the ThinkPHP framework, you can use the Hls.js library to implement the live broadcast playback function. Hls.js is a JavaScript library based on HTML5, which can convert M3U8 format live streams into simulated FLV format and play them in real time.

code show as below:

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();
    }
}

Added a method inLiveAction, which obtains the playback address and assigns it to the template, and then passes the templateThe method is displayed on the page. playdisplay

You can use the Hls.js library to play live streams on the page.

The complete code is as follows:

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();
    }
}

Project attachment:​​Click here to download​​

Guess you like

Origin blog.csdn.net/CRMEB/article/details/134689231