欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ffmpeg+nginx 的直播(1,直播播放的视频文件)

程序员文章站 2022-07-08 16:45:51
...
64位操作系统centos7
############
1.一键安装

http://jingyan.baidu.com/article/22a299b5cf7ea79e19376ad1.html

安装EPEL Release,因为安装需要使用其他的repo源,所以需要EPEL支持
yum install -y epel-release 

#如果出现缺少Code提示,可以:
sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

#安装完成之后,可以查看是否安装成功
yum repolist
安装Nux-Dextop源
#导入一个Code
sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro

#安装nux-dextop 源
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm

#查看repo源是否安装成功
yum repolist



#########################
yum安装ffmpeg
yum install -y ffmpeg

#安装完成后检查ffmpeg 版本
ffmpeg -version

安装播放器
yum install vlc
yum install mplayer

随便下载个mp4文件
mplayer emacs.mp4



2.安装rtmp:
https://github.com/arut/nginx-rtmp-module/wiki/Getting-started-with-nginx-rtmp


cd /opt/nginx
git clone https://github.com/killinux/nginx-rtmp-module
./configure --add-module=/opt/nginx/nginx-rtmp-module --prefix=/usr/local/nginx

make
make install


注意:★★★★★★
mime.types文件里加两行
application/octet-stream              ts; 
application/x-mpegURL                 m3u8; 

否则不支持m3u8
apple的safari会加载不了视频文件
nginx.conf的配置文件修改如下
###################
worker_processes  1;
error_log  logs/error.log debug;
events {
    worker_connections  1024;
}
rtmp {
    server {
        listen 1935;
        application myapp {
            live on;
            #record keyframes;
            #record_path /tmp;
            #record_max_size 128K;
            #record_interval 30s;
            #record_suffix .this.is.flv;
            #on_publish http://localhost:8080/publish;
            #on_play http://localhost:8080/play;
            #on_record_done http://localhost:8080/record_done;
        }
        application hls {  
             live on;  
             hls on;  
             hls_path /tmp/app;  
             hls_fragment 5s;  
       }  
    }
}
http {
    autoindex on;
    server {
        listen      8080;
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            root /opt/nginx/nginx-rtmp-module/;
        }
        location /control {
            rtmp_control all;
        }
        #location /publish {
        #    return 201;
        #}
        #location /play {
        #    return 202;
        #}
        #location /record_done {
        #    return 203;
        #}
        location /rtmp-publisher {
            root /opt/nginx/nginx-rtmp-module/test;
        }
        location /hls {  
           #server hls fragments  
           types{  
             application/vnd.apple.mpegurl m3u8;  
             video/mp2t ts;  
           }  
        alias /tmp/app;  
        expires -1;  
        }  
        location / {
            root /opt/nginx/nginx-rtmp-module/test/www;
        }
    }
}

###################

3.iphone的服务端和浏览器客户端测试
如果是用ios的safari打开
服务端启动
ffmpeg -re -i ./emacs.mp4 -vcodec copy -acodec copy -f flv rtmp://192.168.139.117/hls/mystream


客户端用
##########################
<html>
<head>
<title>HTTP Live Streaming Example</title>
</head>
<body>
<video src="http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8" height="300" width="400">
</video>
<video src="http://192.168.139.117:8080/hls/mystream.m3u8" height="300" width="400">
</video>

-----------------
</body>
</html>

#########################
4.windows的服务端和浏览器chrome测试

如果是用windows的浏览器打开

服务端用
ffmpeg -loglevel verbose -re -i ./emacs.mp4  -f flv rtmp://192.168.139.117/myapp/mystream

参考nginx-rtmp-module/test/www/ffstream.sh
客户端参考
参考nginx-rtmp-module/test/www/index.html
##################
<b>Play</b> | <a href="record.html">Record</a>
<br/>
<script type="text/javascript" src="/jwplayer/jwplayer.js"></script>

<div id="container">Loading the player ...</div>
    <script type="text/javascript">
        jwplayer("container").setup({
        sources: [
            {
                file: "rtmp://192.168.139.117/myapp?carg=1/mystream?sarg=2"
            }
        ],
        image: "bg.jpg",
        autostart: false,
        width: 640,
        height: 480,
        primary: "flash"
});
</script>

###################
5.用linux客户端ffplay打开
如果是用客户端打开
参考
nginx-rtmp-module/test/www/play.sh
ffplay -loglevel verbose "rtmp://192.168.139.117/myapp/mystream"
ffplay -loglevel verbose "rtmp://192.168.139.117/hls/mystream"


查看视频状态使用
http://192.168.139.117:8080/stat

参考
https://github.com/killinux/nginx-rtmp-module
http://blog.csdn.net/cjsafty/article/details/9108587
http://haoningabc.iteye.com/blog/1559086