搭建自媒体服务器

1ngnix+rtmpModule+ffmpeg安装配置

1.1、安装环境

    CentOS6.5

    ngnix1.12.2 下载地址:http://nginx.org/en/download.html

    ngnix-rtmp-module 下载地址:https://github.com/arut/nginx-rtmp-module

1.2ngnix安装

   

    ngnix必要库安装

        yum -y install openssl-devel pcre pcre-devel zlib zlib-devel gcc gcc-c++ glibc glibc-devel

    其他缺少的库根据编译错误提示安装

    ngnix安装和配置

    1、解压ngnix1.12.2

        tar -zxvf ngnix1.12.2.tar.gz

        unzip ngnix-rtmp-module.zip

    2、配置安装

        cd ngnix1.12.2

        ./configure –add-module=/homt/root/nginx-rtmp-module

        make

        make install

    3、修改配置文件

  vi conf/ngnix.conf

#在最外层增加如下配置

rtmp {

    server {

        listen 1935;

       #rtmp配置

        application live{

            live on;

            #开始推流回调,url地址为java接口

            on_publish http://192.168.1.62:9001/hello;

            #推流停止回调,url地址为java接口

            on_done http://192.168.1.62:9001/hello;

        }

        #hls配置

        application hls {

            live on;

            hls on;

            hls_path /tmp/hls;

            #开始推流回调,url地址为java接口

            on_publish http://192.168.1.62:9001/hello;

            #推流停止回调,url地址为java接口

            on_done http://192.168.1.62:9001/hello;

        }

    }

}       

1.3java后台

    只做了个简单测试,可根据这些参数完善后台,实现用户推流开始把用户ID和推流地址存入redis或其他高性能存储,前台展示当前直播的用户,点击用户返回推流地址,实现直播,测试代码如下,用spring boot写的

@RestController

@SpringBootApplication

public class DemoApplication {

    @RequestMapping("/hello")

    public String hello(HttpServletRequest request) {

        Map<String, String[]> parameterMap = request.getParameterMap();

        System.out.println("参数数量:"+parameterMap.size());

        /**这三个参数是比较有用的参数

             tcurl:rtmp地址

             name:"rtmp://ngnix服务器地址/live/id"地址中去除推流地址的最后一个参数,可传入直播用户ID

             type:推流类型

        */

        System.out.println("tcurl:"+request.getParameter("tcurl"));

        System.out.println("name:"+request.getParameter("name"));

        System.out.println("type:"+request.getParameter("type"));

        //打印所有回调传过来的参数

        for(Entry<String, String[]> entry : parameterMap.entrySet()) {

            System.out.println(entry.getKey() + "====================");

            for(String str : entry.getValue()) {

                System.out.println("\t"+str);

            }

        }

        return "hello world2";

    }

    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(DemoApplication.class);

        app.setBanner(new BannerInfo());

        app.run(args);

    }

}

   1.4、测试

1.4.1、使用ffmpeg把一个文件转为rtmp

    ffmpeg -re -i F:\1.wmv -c:v libx264 -preset veryfast -maxrate 3000k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv rtmp://ngnix服务器地址/live/1

java后台打印结果

参数数量:9

tcurl:rtmp://192.168.1.10:1935/live

name:1

type:live

app====================

    live

flashver====================

    FMLE/3.0 (compatible; Lavf58.5.

swfurl====================

tcurl====================

    rtmp://192.168.1.10:1935/live

pageurl====================

addr====================

    192.168.1.62

clientid====================

    410

call====================

    publish

name====================

    

1.4.2、然后使用vlc打开,测试成功

这里写图片描述

1.5、结尾

到此一个简单的直播平台就可以实现了,虽然不是一个完整的例子,但是核心就是以上这些,大家可以在次基础上扩展,可以使用到一些对直播要求不太高的场景,至于后期的如何处理延时问题,还需要进一步的实现研究学习。

猜你喜欢

转载自www.cnblogs.com/riverone/p/11690097.html