ubuntu下使用nginx搭建流媒体服务器,实现视频点播

为了实现视频点播和直播,整了一天,看了N多博客,终于有了不小进展,在这里分享给大家。
什么是视频点播?可能你对这个名词不熟悉,但是爱奇艺,腾讯视频等这些视频播放软件想必你肯定知道。没错,视频点播就是在线观看某个视频。额,视频直播?这个就不解释了吧,美女直播,游戏直播等等,哈哈。

首先我们看如何实现视频点播,视频点播支持flv文件及H264编码视频,ACC编码音频的mp4文件:
第一步,创建单独的目录(因为软件较多,容易混乱),下载需要的软件:
我们需要下载nginx,pcre,zlib,openssl以及nginx-rtmp-module

mkdir work

cd work

wget http://nginx.org/download/nginx-1.10.3.tar.gz

wget http://zlib.net/zlib-1.2.11.tar.gz

wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz

wget https://www.openssl.org/source/openssl-1.0.2k.tar.gz

wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

第二步,分别解压这四个文件:

tar -zxvf 文件名

第三步,编译安装nginx:

./configure --prefix=/usr/local/nginx --with-debug --with-pcre=../pcre-8.40 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.2k --add-module=../nginx-rtmp-module-master

make

sudo make install

第四步,进入 /usr/local/nginx/conf,配置nginx.conf文件,在文件末尾添加如下内容:

rtmp {
    server {
        listen 1935;

        application vod {
            play /var/flvs; #指定存放视频文件的路径
        }

        application vod_http {
            #myserver.com及服务器地址,如果只是本地播放,填写127.0.0.1:端口号 就行,端口好看配置文件中http监听的端口下同
            play http://myserver.com/vod; 
        }

        application vod_mirror {
            play /var/local_mirror http://myserver.com/vod;
        }
    }
}

示例完整版:


#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 {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;

    application vod {
            play /video_test;
    }

    application vod_http {
            play http://192.168.1.102:8080/vod;
    }

    application vod_mirror {
            # try local location first, then access remote location
        play /var/local_mirror http://192.168.1.102:8080/vod;
    }
    }
}

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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

然后,进入与conf文件夹同级的sbin目录下,运行:

sudo ./nginx

至此,服务器搭建完成,可以实现视频点播了。
视频点播测试:
播放位于 /video_test 下的file.flv视频文件:

ffplay rtmp://localhost/vod//file.flv

可以使用VLC这个播放器或者ffplay(需要安装ffmpeg),VLC自行百度安装即可。
ffmpeg安装:

sudo apt-add-repository ppa:jon-severinsson/ffmpeg
sudo apt-get update
sudo apt-get install ffmpeg
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Nginx可以作为流媒体服务器搭建实现录播(直播回放)功能。具体的步骤如下: 1. 安装Nginx服务器:首先需要在服务器上安装Nginx软件。可以通过命令行或软件包管理工具来进行安装,并配置Nginx的基本设置。 2. 配置Nginx的rtmp模块:Nginx的rtmp模块是用于支持流媒体功能的关键模块。需要在Nginx的配置文件中启用rtmp模块,并进行相关的配置,如监听端口、应用名称等。 3. 创建应用和推流地址:通过配置Nginx的rtmp模块,可以创建一个或多个应用,并为每个应用指定一个推流地址。推流地址是用来上传视频内容的,可以通过专业的采集设备、手机等设备来进行推流。 4. 录制推流内容:Nginx的rtmp模块支持将推流内容实时录制为存储文件。只需在相应的应用配置中启用录制功能,指定存储路径和文件格式等。 5. 提供直播回放功能:通过配置Nginx的rtmp模块,可以将已经录制的内容作为直播回放资源进行提供。可以将存储文件的路径配置成回放地址,供观众通过播放器来进行回放操作。 总的来说,使用Nginx搭建流媒体服务器实现录播功能的过程比较简单。只需安装Nginx服务器并配置相关的rtmp模块,就可以创建应用、推流和录制内容,并通过回放地址提供直播回放功能。这样,用户即可以进行直播,也可以在回放时随时观看之前的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值