支付宝赞助帐号:service@kuitao8.com 

centos7下安装直播服务器shell [原创]

Created2017-10-26   Views 129    Author 懒人程序
#!/bin/bash -x
export nginx_version=1.9.9
mkdir -p /live/hls 
mkdir -p /live/log 
mkdir -p /live/html
mkdir -p /live/rec 
mkdir -p /live/hls
mkdir -p /live/show
chmod -R 777 /live 
# get latest rtmp mod
mkdir /usr/local/src
cd /usr/local/src
git clone git://github.com/arut/nginx-rtmp-module.git

# get nginx
wget http://nginx.org/download/nginx-${nginx_version}.tar.gz
tar xzf nginx-${nginx_version}.tar.gz
rm -rf nginx-${nginx_version}.tar.gz
cd nginx-${nginx_version}

./configure --with-http_v2_module --with-http_flv_module --with-http_mp4_module --add-module=/usr/local/src/nginx-rtmp-module --with-http_ssl_module 
make
make install

cp /usr/local/src/nginx-rtmp-module/stat.xsl /usr/local/nginx
chmod uga+rw /usr/local/nginx/stat.xsl

# backup config
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.backup

# new config
cat > /usr/local/nginx/conf/nginx.conf << "EOF"
#user  nobody;
worker_processes  1;

error_log  logs/error.log debug;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /live/html/;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
    server {
    listen 8080;

    # This URL provides RTMP statistics in XML
    location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            root /usr/local/nginx/;
        }
    # rtmp control
    location /control {
        rtmp_control all;
    }

    location / {
        root   /live/html/;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location /hls {
        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }

        root /live/;
        add_header Cache-Control no-cache; # Prevent caching of HLS fragments
        add_header Access-Control-Allow-Origin *; # Allow web player to access our playlist
    }
}
}
env PATH;
# error_log  /live/log/error.log;
# error_log  /live/log/error.log  notice;
error_log  /live/log/error.log  info;
user  root;
rtmp {
    server {
        listen 1935;
        chunk_size 4000;

        # This application is to accept incoming stream
        application live {
            # enable live streaming
            live on;

            # auth on publish
            #on_publish http://localhost:8855/auth;

            # push rtmp://localhost/rec/;

            # Once receive stream, transcode for adaptive streaming
            # This single ffmpeg command takes the input and transforms
            # the source into 4 different streams with different bitrate
            # and quality. P.S. The scaling done here respects the aspect
            # ratio of the input.
            exec ffmpeg -i rtmp://localhost/live/$name -async 1 -vsync -1 -c:v libx264 -c:a aac -b:v 256k -b:a 32k -vf "scale=480:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/show/$name_low -c:v libx264 -c:a aac -b:v 768k -b:a 96k -vf "scale=720:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/show/$name_mid -c:v libx264 -c:a aac -b:v 1024k -b:a 128k -vf "scale=960:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/show/$name_high -c:v libx264 -c:a aac -b:v 1920k -b:a 128k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/show/$name_hd720 -c copy -f flv rtmp://localhost/show/$name_src -c copy -f flv rtmp://localhost/rec/$name >/live/log/ffmpeg-$name.log 2>&1 ;
            # exec_record_done ffmpeg -y -i $path -codec copy -movflags +faststart $path.mp4;

        }

        # This application is for splitting the stream into HLS fragments
        application show {
            live on; # Allows live input from above

            hls on; # Enable HTTP Live Streaming
            # Pointing this to an SSD is better as this involves lots of IO
            hls_path /live/hls/;
            hls_fragment 3;
            hls_playlist_length 60;

            # Instruct clients to adjust resolution according to bandwidth
            hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution
            hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution
            hls_variant _high BANDWIDTH=1152000; # High bitrate, higher-than-SD resolution
            hls_variant _hd720 BANDWIDTH=2048000; # High bitrate, HD 720p resolution
            hls_variant _src BANDWIDTH=4096000; # Source bitrate, source resolution
        }
         application hls {
            live on;
            hls on;
            hls_path /live/show;
            hls_fragment 5s; 
        }
        application rec {
            live on; # Allows live input from above

            record all;
            record_path /live/rec/;
            # record_suffix all.flv;
            # record_unique on;
            record_append on;
            # exec_record_done ffmpeg -y -i $path -codec copy -movflags +faststart $path.mp4;
            exec_record_done ffmpeg -y -i $path -acodec aac -ar 44100 -ac 2 -vcodec libx264 -movflags frag_keyframe+empty_moov $dirname/$basename.mp4;
        }
}
}
EOF
cat > /live/html/index.html << "EOF"
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>live⭐kira</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="http://vjs.zencdn.net/5.11/video-js.min.css" rel="stylesheet">
    &lt;script src="http://vjs.zencdn.net/5.11/video.min.js"&gt;&lt;/script&gt;
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/5.3.0/videojs-contrib-hls.min.js"&gt;&lt;/script&gt;   
</head>

<body>

    <video id="example-video" width=600 height=300 class="video-js vjs-default-skin">
        <source src="http://192.168.0.188:8080/hls/app.m3u8" type="application/x-mpegURL">
    </video>

    <div>
        StreamName: <input id="stream_name" type="text"><button id="play">play</button>
    </div>
    &lt;script&gt;
        document.querySelector('#play').addEventListener('click', function(){
            let stream_name = document.querySelector('#stream_name').value;
            let url = "http://192.168.0.188:8080/hls/"+stream_name+".m3u8";
            console.log("video src: "+url)
            document.querySelector('source').src = url;
            var player = videojs('example-video', {'controls':true});
            player.play(); 
        })
    &lt;/script&gt;
</body>

</html>
EOF

# stop firewall
systemctl stop firewalld
systemctl disable firewalld

# start nginx
/usr/local/nginx/sbin/nginx

# stop nginx
#/usr/local/nginx/sbin/nginx -s quit

# stream video file to nginx
#ffmpeg -re -i test.mp4 -c copy -f flv rtmp://192.168.207.11/dev_publish/lv_12345

# play stream
##ffplay rtmp://192.168.207.11/dev_live/lv_12345
#ffmpeg -re -i test.mp4 -c copy -f flv  rtmp://192.168.0.188/live/app?key=dpy1123

#rtmp://192.168.0.188/dev_live/lv_12345

#ffmpeg -i test.mp4 -codec:v libx264 -codec:a mp3 -map 0 -f ssegment -segment_format mpegts -segment_list playlist.m3u8 -segment_time 10 out%03d.ts


#ffmpeg -i test.mp4  -strict -2 -c:v libx264 -c:a aac -f hls /live/hls/test.m3u8


#ffmpeg -re -i map.mp4 -c copy -f flv  rtmp://192.168.0.188/live/map?key=dpy1123


#ffmpeg -i map.mp4  -strict -2 -c:v libx264 -c:a aac -f hls /live/hls/test.m3u8

#ffmpeg -re -i map.mp4 -vcodec copy -f flv rtmp://192.168.0.188/hls/movie

下载地址

上一篇: linux安装时出现./configure: error: C compiler cc is not found
下一篇: centos安装rtmp服务器的方法
支持键盘 ← →

邮件订阅

订阅我们的精彩内容