vcloud.163.com网易视频云:用Nginx搭建flv,mp4,hls流媒体服务器网易视频云是网易倾力打造的一款基于云计算的分布式多媒体处理集群和专业音视频技术,提供稳定流畅、低时延、高并发的视频直播、录制、存储、转码及点播等音视频的PAAS服务,在线教育、远程医疗、娱乐秀场、在线金融等各行业及企业用户只需经过简单的开发即可打造在线音视频平台。今天,网易视频云就给大家分享关于用Nginx搭建flv,mp4,hls流媒体服务器的技术干货!模块:nginx_mod_h264_streaming(支持h264编码MP4格式的视频)模块:http_flv_module(支持flv)模块:http_mp4_module(支持mp4)模块:nginx-rtmp-module(支持rtmp协议,也支持HLS)(1)模块下载和解压wget://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gzwget://zlib.net/zlib-1.2.8.tar.gzvcloud.163.comwget://github.com/arut/nginx-rtmp-module/archive/master.zipunzipnginx-rtmp-module.ziptar-zxvfnginx-1.6.0.tar.gztar-zxvfnginx_mod_h264_streaming-2.2.7.tar.gztar-zxvfpcre-8.35.tar.gztar-zxvfzlib-1.2.8.tar.gztar-zxvfopenssl-1.0.1g.tar.gz(2)配置命令,会生成makefile文件./configure\--prefix=/usr/local/nginx\--add-module=../nginx_mod_h264_streaming-2.2.7\--add-module=../nginx-rtmp-module\--with-http_flv_module\--with-http_mp4_module\--with-http_stub_status_module\--with-http_ssl_module\vcloud.163.com--with-pcre=../pcre-8.35\--with-zlib=../zlib-1.2.8\--with-debug(3)编译和安装makemakeinstall(4)问题解决【1】如果在configure过程中出现以下错误:/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:Infunction‘ngx_streaming_handler’:/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158:error:‘ngx_http_request_t’hasnomembernamed‘zero_in_uri’make[1]:***[objs/addon/src/ngx_http_h264_streaming_module.o]Error1make[1]:Leavingdirectory`/root/nginx-0.8.54'make:***[build]Error2那么将src/ngx_http_streaming_module.c文件中以下代码删除或者是注释掉就可以了:vcloud.163.com/*TODO:Win32*/if(r-zero_in_uri){returnNGX_DECLINED;}如果你没有对这个文件做个更改,那么应该在源码的第157-161行。这个问题是由于版本原因引起,在此不再讨论。修改完之后,记得先执行makeclean,然后再进行重新执行configure、make,最后makeinstall。【2】如果在编译过程中出现以下错误:cc1:warningsbeingtreatedaserrors那么修改/nginx-1.6.0/objs/Makefile文件CFLAGS=-pipe-O-W-Wall-Wpointer-arith-Wno-unused-parameter-Werror-g-D_LARGEFILE_SOURCE-DBUILDING_NGINX-I../nginx-rtmp-module-master把上面的-Werror去掉,不把warnning当作error处理(5)Nginx的配置vcloud.163.com#usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events{worker_connections1024;}rtmp{server{listen1935;chunk_size4000;#videoondemandforflvfilesapplicationvod{play/usr/local/nginx/html/flv;}#videoondemandformp4filesvcloud.163.comapplicationvod2{play/usr/local/nginx/html/mp4;}applicationhls{liveon;hlson;hls_path/tmp/hls;}#MPEG-DASHissimilartoHLSapplicationdash{liveon;dashon;dash_path/tmp/dash;}}}http{includemime.types;vcloud.163.comdefault_typeapplication/octet-stream;#log_formatmain'$remote_addr-$remote_user[$time_local]$request'#'$status$body_bytes_sent$http_referer'#'$http_user_agent$http_x_forwarded_for';#access_loglogs/access.logmain;sendfileon;#tcp_nopushon;#keepalive_timeout0;keepalive_timeout65;#gzipon;server{#incasewehaveanotherwebserveronport80listen8080;server_namelocalhost;#charsetkoi8-r;#access_loglogs/host.access.logmain;location/{roothtml;vcloud.163.comindexindex.htmlindex.htm;}#error_page404/404.html;#redirectservererrorpagestothestaticpage/50x.html#error_page500502503504/50x.html;location=/50x.html{roothtml;}location~\.mp4${mp4;}location~\.flv${flv;}#ThisURLprovidesRTMPstatisticsinXMLlocation/stat{rtmp_statall;vcloud.163.comrtmp_stat_stylesheetstat.xsl;}location/stat.xsl{#XMLstylesheettoviewRTMPstats.#Copystat.xslwhereveryouwant#andputthefulldirectorypathhereroot/var/}location/hls{#ServeHLSfragmentstypes{application/vnd.apple.mpegurlm3u8;video/mp2tts;}#wherethem3u8andtsfilesarealias/usr/local/nginx/html/hls;#livestreamingsetting#root/tmp;vcloud.163.com#add_headerCache-Controlno-cache;}location/dash{#ServeDASHfragmentsroot/tmp;add_headerCache-Controlno-cache;}}}(6)用ffmpeg生成测试序列【1】对于mp4文件,生成moov信息前移的mp4格式,适合流媒体播放。ffmpeg-i/home/administrator/Videos/Amelia_720p.mp4-c:vlibx264-c:alibvo_aacenc-fmp4-movflagsfaststart/home/administrator/Videos/moovfront.mp4【2】对于flv文件,用flvmeta工具在metadata中注入关键帧的信息,支持随意拖动播放。ffmpeg-i/home/administrator/Videos/Baroness.mp4-vcodeclibx264-acodeclibvo_aacenc-b:a128k-ar44100-ac2-fflv/home/administrator/Videos/Baroness.flvvcloud.163.comflvmeta-U-m-k/home/administrator/Videos/Baroness.flv/home/administrator/Videos/Baroness_meta.flv【3】对于flv的播放,或者直接生成f4v格式的文件。ffmpeg-i/home/administrator/Videos/sample/vc1_1080p.mkv-acodeclibfdk_aac-ac2-b:a128k-ar48000-vcodeclibx264-pix_fmtyuv420p-profile:vmain-level32-b:v1000K-r29.97-g30-s960x540-ff4v/home/administrator/Videos/hddvd_1000k.f4v(7)Nginx启动,重启,关闭命令startnginx开启nginx-sstop快速关闭nginx-squit完全关闭nginx-sreload修改过配置文件,快速关闭旧的,开启新服务nginx-sreopen重新打开日志文件[停止操作]停止操作是通过向nginx进程发送信号来进行的步骤1:查询nginx主进程号vcloud.163.comps-ef|grepnginx在进程列表里面找master进程,它的编号就是主进程号了。步骤2:发送信号从容停止Nginx:kill-QUIT主进程号快速停止Nginx:kill-TERM主进程号强制停止Nginx:pkill-9nginx另外,若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送