CentOS6下基于Nginx搭建mp4/flv流媒体服务器

1.先添加几个RPM下载源

2.安装转码工具Mencoder及ffmpeg(约定:每个小点操作之前先回到用户主目录,即cd ~)

    3.4)各种配置nginx
      编辑/usr/local/nginx/conf/nginx.conf文件,最好用sftp软件(如windows下的flashfxp/Mac下的tramnsmit)下载过来本地编辑。
    贴一下我的配置文件:(目录需要自己改动,我用的是阿里云的数据盘,所以到/mnt/里面去了)
      ———————————————————————nginx配置文件—————————————————
    #filename:nginx.conf
    #user  nobody;
    worker_processes  1;

    error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    pid        logs/nginx.pid;

    events {
        use epoll;
        worker_connections  1024;
    }

    rtmp {
        server {
            listen 1935;
            chunk_size 4000;

            # video on demand
            application vod {
                play /mnt/media/vod;
            }

            # HLS
            # HLS requires libavformat & should be configured as a separate
            # NGINX module in addition to nginx-rtmp-module:
            # ./configure … –add-module=/path/to/nginx-rtmp-module/hls …
            # For HLS to work please create a directory in tmpfs (/tmp/app here)
            # for the fragments. The directory contents is served via HTTP (see
            # http{} section in config)
            #
            # Incoming stream must be in H264/AAC/MP3. For iPhones use baseline H264
            # profile (see ffmpeg example).
            # This example creates RTMP stream from movie ready for HLS:
            #
            # ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264
            #    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
            #    -f flv rtmp://localhost:1935/hls/movie
            #
            # If you need to transcode live stream use ‘exec’ feature.
            #
            application hls {
                hls on;
                hls_path /mnt/media/app;
                hls_fragment 10s;
            }
        }
    }

    http {

        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        keepalive_timeout 65;
        gzip on;
       
          #log format

        log_format  access  ‘$remote_addr – $remote_user [$time_local] “$request” ‘
                ‘$status $body_bytes_sent “$http_referer” ‘
                ‘”$http_user_agent” $http_x_forwarded_for’;   
       
          #定义一个名为addr的limit_zone,大小10M内存来存储session
        limit_conn_zone $binary_remote_addr zone=addr:10m;   

        server {
            listen 8080;
              server_name localhost;

              # HTTP can be used for accessing RTMP stats
            # This URL provides RTMP statistics in XML
            location /stat {
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
            }
            location /stat.xsl {
                root /mnt/soft/nginx-rtmp-module-master;
            }
              location /control {
                rtmp_control all;
            }
            location / {
                root /mnt/soft/nginx-rtmp-module-master/test/rtmp-publisher;
            }
        }
       
          server {
            listen 80;
              server_name localhost;
             
            location / {
                    root /mnt/wwwroot;
                    index index.html;
                  }       

              location ~ \.flv$ {
                    root /mnt/media/vod;
                  flv;
                  limit_conn addr 20;
                  limit_rate 200k;
            }
            location ~ \.mp4$ {
                  root /mnt/media/vod;
                  mp4;
                  limit_conn addr 20;
                  limit_rate 200k;
            }

              location /hls {
                # Serve HLS fragments
                alias /mnt/media/app;
            }

              access_log  logs/nginxflv_access.log access;
        }
       
           
    }
      ——————————————————nginx配置文件——————————————
   
4.把自己的电影转换成mp4和flv格式来测试nginx搭的环境

    4.1)准备两部电影,硬盘上随便找,我找了“谍影重重A.mp4”和“鹿鼎记033.rmvb”,尽量找小一点十来分钟的,等下我们还要看完测试一下转换的结果有没有音影不同步的情况。
    我把两部电影重命名为 movie1.mp4和 movie2.rmvb,并上传到服务器/mnt/media/video下面,这里目录用来存放我们的原始视频。还有一个目录是/mnt/media/vod 用来存放转换后的视频。
    我这里的具体目录结构为:
      /mnt/media/video -> 存放原始视频
    /mnt/media/app  -> 存放转成m3u8的视频,供http访问(HLS)
      /mnt/media/vod  -> 存放转换后的flv和mp4视频,供http或rtmp访问

    注:安装日志记录于半年前的evernote,现在才贴出来的,所以文中的视频访问地址都已经失效了。

猜你喜欢

转载自www.linuxidc.com/Linux/2015-08/121324.htm