Jade Dungeon

Nginx直播流

安装与配置

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev 

tar -xvf nginx-1.15.12.tar.gz  
tar -xvf nginx-rtmp-module-v1.2.1.tar.gz

cd nginx-release-1.15.12
auto/configure --prefix=/opt/nginx --with-http_ssl_module --add-module=../nginx-rtmp-module-1.2.1

如果要配置得更加详细,还可以加上参数:

--sbin-path=/usr/sbin/nginx 
--conf-path=/etc/nginx/nginx.conf 
--pid-path=/var/run/nginx.pid 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 

在make之前,删除掉Makefile中的-Werror,不然编译不通过:

# Makefile引用的是 obj/Makefile
vi obj/Makefile   # remove -Werror

然后编译安装

make

make install

指定直播项目

生成项目的目录/data/nginx/broadcast/

mkdir -p /data/nginx/broadcast/
mkdir -p /data/nginx/broadcast/html/hls

修改nginx配置/opt/nginx/conf/nginx.conf,添加网站:

rtmp {                              #添加RTMP协议服务

    server {
        listen 1935;                #//服务端口
        chunk_size 4096;            #//数据传输块的大小

        application hls { #第一处添加的直播字段
            record       off;
            live         on;
            hls          on;
            hls_path     /data/nginx/broadcast/html/hls;
            hls_fragment 5s;
        }
    }
    
}

http {

    # 在http协议下添加一个新的站点
    server {
        listen      8080;
        server_name localhost;
        error_log   logs/broadcase.err.log;
     
        index       index.html index.php;
        root        /data/nginx/broadcast/html;    
        
        location    /hls {   
            types {  # server HLS fragments
                text/html html htm;
                # application/vnd.apple.mpegurl m3u8;
                application/x-mpegurl m3u8;
                video/mp2t ts;
            }       
            root /data/nginx/broadcast/html;
            index index.html;
            expires -1;
        }       
    }

    # ..............

}

启动nginx:

sudo /opt/nginx/sbin/nginx

停止nginx:

sudo /opt/nginx/sbin/nginx -s stop

直播视频流

直播视频文件

ffmpeg -re -i example.02.mp4 -vcodec copy -acodec aac -b:a 192k -f flv "rtmp://elsa.jade-dungeon.net/hls/video01"

如需要视频放完后循环播放:

#!/bin/bash
while true
do
ffmpeg -re -i example.02.mp4 -vcodec copy -acodec aac -b:a 192k -f flv "rtmp://elsa.jade-dungeon.net/hls/video01"
done

收看视频直播

浏览观看:

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>HLS Player</title>
</head>
<body>
<video id="video" src="http://192.168.5.138:8080/hls/video01.m3u8" 
	width="100%" heigh="100%" autoplay="autoplay" controls="controls">
	不支持vide
</video>
</body>
</html> 

播放器观看(以vlc为例子):

“媒体” 》“打开网络流” 》http://192.168.5.138:8080/hls/video01.m3u8

以后如果有多个频道,还可以整合到一个m3u文件里:

#EXTM3U
#EXTINF:-1 tvg-logo="http://192.168.5.138:8080/imgs/channel01.png" tvg-name="HomeChannel-01",自家频道01
http://192.168.5.138:8080/hls/video01.m3u8
#EXTINF:-1 tvg-logo="http://192.168.5.138:8080/imgs/channel02.png" tvg-name="HomeChannel-02",自家频道02
http://192.168.5.138:8080/hls/video02.m3u8
#EXTINF:-1 tvg-logo="http://192.168.5.138:8080/imgs/channel03.png" tvg-name="HomeChannel-03",自家频道03
http://192.168.5.138:8080/hls/video03.m3u8

https://www.cnblogs.com/tocy/p/using-ffmpeg-build-hls-live-system.html