从零开始:PHP实现阿里云直播的简单方法!

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

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

推流地址一般格式为:

ini 复制代码
rtmp://{Domain}/{AppName}/{StreamName}?auth_key={AuthKey}-{Timestamp}-{RandomNum}

其中,

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

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

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

{AuthKey}代表授权密钥;

{Timestamp}代表当前时间戳;

{RandomNum}代表随机数。

播放地址一般格式为:

arduino 复制代码
http://{Domain}/{AppName}/{StreamName}.m3u8

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

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

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

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

php 复制代码
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;
    }
}

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

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

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

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

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

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

代码如下:

php 复制代码
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);
    }
}

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

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

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

代码如下:

php 复制代码
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();
    }
}

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

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

完整代码如下:

php 复制代码
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();
    }
}

项目附件:​​点此下载​

相关推荐
jingwang-cs21 分钟前
内外网文件传输 安全、可控、便捷的跨网数据传输方案
人工智能·后端·安全
Aska_Lv1 小时前
Java8-Stream流-实际业务常用api案例
后端
Biehmltym1 小时前
【SpringMVC】概述 SSM:Spring + SpringMVC + Mybats
java·后端·spring
qw9492 小时前
SpringMVC
java·后端
customer082 小时前
【开源免费】基于SpringBoot+Vue.JS医疗报销系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
B站计算机毕业设计超人2 小时前
计算机毕业设计SpringBoot+Vue.jst房屋租赁系统(源码+LW文档+PPT+讲解)
vue.js·spring boot·后端·eclipse·intellij-idea·mybatis·课程设计
wapicn993 小时前
‌挖数据平台对接DeepSeek推出一键云端部署功能:API接口驱动金融、汽车等行业智能化升级
java·人工智能·python·金融·汽车·php
Hacker_Nightrain3 小时前
内网网络安全的解决之道
安全·web安全·php
m0_748248653 小时前
SpringBoot整合easy-es
spring boot·后端·elasticsearch
一个热爱生活的普通人3 小时前
golang的切片(Slice)底层实现解析
后端·go