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

时间:2022-05-06 06:05:04

CentOS6下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具)

1.先添加几个RPM下载源

1.1)安装RPMforge的CentOS6源
[root@linuxidc ~]# wget -c http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
[root@linuxidc ~]# rpm –import http://apt.sw.be/RPM-GPG-KEY.dag.txt
[root@linuxidc ~]# rpm -i rpmforge-release-0.5.3-1.el6.rf.*.rpm

1.2)安装epel源
[root@linuxidc ~]# wget -c http://download.Fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
[root@linuxidc ~]# rpm -Uvf epel-release-6-8.noarch.rpm

安装完成,查看一下源列表,看到rpmforge和epel字样即可
[root@linuxidc ~]# ls -lh /etc/yum.repos.d

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

2.1)安装一些基础包,不用担心重复,已经存在的会自动忽略或升级
[root@linuxidc ~]# yum install gcc make automake bzip2 unzip patch subversion libjpeg-devel
[root@linuxidc ~]# wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
[root@linuxidc ~]# tar zxvf yasm-1.2.0.tar.gz
[root@linuxidc ~]# cd yasm-1.2.0
[root@linuxidc yasm-1.2.0]# ./configure
[root@linuxidc yasm-1.2.0]# make && make install
[root@linuxidc yasm-1.2.0]# cd ~

2.2)卸载系统原有的ffmgeg和x.264,没有则跳过
[root@linuxidc ~]# yum remove ffmpeg x264

2.3)先安装一些Mplayer编码库(仅限于64位系统)
[root@linuxidc ~]# wget -c http://www2.mplayerhq.hu/MPlayer/releases/codecs/essential-amd64-20071007.tar.bz2
上面这一步可能在部分VPS上无法下载,比如我的阿里云VPS就提示连接失败,但是可以用本机浏览器下载后上传到VPS的/root根目录去,然后继续下面的操作
[root@linuxidc ~]# tar xvjf essential-amd64-20071007.tar.bz2
[root@linuxidc ~]# mkdir /usr/local/lib/codecs
[root@linuxidc ~]# cp -Rvp essential-amd64-20071007/* /usr/local/lib/codecs/

编辑下面文件
[root@linuxidc ~]# vim /etc/ld.so.conf
添加以下两行到上面的文件里
/usr/lib
/usr/local/lib

2.4)再安装一些格式转换常用的编码库
[root@linuxidc ~]# yum install faac-devel lame-devel amrnb-devel opencore-amr-devel amrwb-devel libvorbis-devel libtheora-devel xvidcore-devel

2.5)安装x.264
[root@linuxidc ~]# wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/last_stable_x264.tar.bz2
[root@linuxidc ~]# tar xvjf last_stable_x264.tar.bz2
[root@linuxidc ~]# cd x264-snapshot-20140219-2245-stable/ (这可能日期有区别,自己ls一下)
[root@linuxidc x264-snapshot-20140219-2245-stable]# ./configure –enable-shared –enable-pic
[root@linuxidc x264-snapshot-20140219-2245-stable]# make && make install
[root@linuxidc x264-snapshot-20140219-2245-stable]# cd ~

2.6)安装libvpx
[root@linuxidc ~]# wget http://webm.googlecode.com/files/libvpx-v1.2.0.tar.bz2
[root@linuxidc ~]# tar xvjf libvpx-v1.2.0.tar.bz2
[root@linuxidc ~]# cd libvpx-v1.2.0
[root@linuxidc libvpx-v1.2.0]# ./configure –enable-shared –enable-pic
[root@linuxidc libvpx-v1.2.0]# make && make install
[root@linuxidc libvpx-v1.2.0]# cd ~

2.7)安装FFmpeg
[root@linuxidc ~]# wget http://ffmpeg.org/releases/ffmpeg-2.0.1.tar.bz2
[root@linuxidc ~]# tar xvjf ffmpeg-2.0.1.tar.bz2
[root@linuxidc ~]# cd ffmpeg-2.0.1
[root@linuxidc ffmpeg-2.0.1]# ./configure –enable-gpl –enable-version3 –enable-shared –enable-nonfree –enable-postproc –enable-libfaac –enable-libmp3lame –enable-libopencore-amrnb –enable-libopencore-amrwb –enable-libtheora –enable-libvorbis –enable-libvpx –enable-libx264 –enable-libxvid
[root@linuxidc ffmpeg-2.0.1]# make && make install
[root@linuxidc ffmpeg-2.0.1]# cd ~

让动态链接库被系统共享
[root@linuxidc ~]# ldconfig

2.8)最后安装mencoder
[root@linuxidc ~]# yum install mplayer mencoder flvtool2

查看一下已经安装好的音频和视频编码器
查看所有所支持的音频编码
[root@linuxidc ~]# mencoder -oac help
查看所有所支持的视频编码
[root@linuxidc ~]# mencoder -ovc help
具体结果可参考http://www.mplayerhq.hu/DOCS/HTML/zh_CN/menc-feat-selecting-codec.html

3.安装配置Nginx

3.1)先安装各种依赖(nginx需要pcre支持,yamdi用来为flv创建关键帧才能随意拖动)
[root@linuxidc ffmpeg-2.0.1]# yum install gcc gcc-c++ openssl-devel zlib-devel pcre pcre-devel yamdi

3.2)下载所需的nginx模块
第一个是nginx_mod_h264_streaming,让nginx支持flv/mp4流播放
[root@linuxidc ~]# wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
[root@linuxidc ~]# tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
注意:先要修改一下这家伙的源码,注释掉nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c的158到161行
/* TODO: Win32 */
//if (r->zero_in_uri)
// {
// return NGX_DECLINED;
// }

第二个是nginx-rtmp-module,让nginx支持rtmp/hls协议
[root@linuxidc ~]# wget -O nginx-rtmp-module.zip https://github.com/arut/nginx-rtmp-module/archive/master.zip
[root@linuxidc ~]# unzip nginx-rtmp-module.zip
下载清缓存的模块
[root@linuxidc ~]# wget -O ngx_cache_purge.zip https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip
[root@linuxidc ~]# unzip ngx_cache_purge.zip

3.3)下载安装nginx
[root@linuxidc ~]# wget http://nginx.org/download/nginx-1.2.9.tar.gz
[root@linuxidc ~]# tar zxvf nginx-1.2.9.tar.gz
[root@linuxidc ~]# cd nginx-1.2.9
[root@linuxidc nginx-1.2.9]# ./configure –user=daemon –group=daemon –prefix=/usr/local/nginx/ –add-module=../nginx-rtmp-module-master –add-module=../ngx_cache_purge-master –add-module=../nginx_mod_h264_streaming-2.2.7 –with-http_stub_status_module –with-http_ssl_module –with-http_sub_module –with-http_gzip_static_module –with-http_flv_module
[root@linuxidc nginx-1.2.9]# make && make install
PS:若提示make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1,请参考3.3.2小节的注意点,然后重新运行./configure这一段

/**———以下是安装了Tengine后重新编译的代码,模块的路径自己对一下———**//
./configure –user=www –group=www –prefix=/usr/local/tengine/ –add-module=../../../nginx-rtmp-module-master –add-module=../../../ngx_cache_purge-master –add-module=../../../nginx_mod_h264_streaming-2.2.7 –with-http_stub_status_module –with-http_ssl_module –with-http_sub_module –with-http_gzip_static_module –with-http_flv_module –with-http_concat_module=shared –with-http_sysguard_module=shared –with-google_perftools_module –with-http_image_filter_module

运行一下nginx
[root@linuxidc ~]# /usr/local/nginx/sbin/nginx
然后用浏览器你的服务器IP,看到welcome就对了
或者到nginx的sbin目录下,运行一下nginx -V,看看列表出来的modules对不对

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访问

4.2)用ffmpeg转换mp4文件(ffmpeg不支持rmvb)
[root@linuxidc ~]# cd /mnt/media/video/
[root@linuxidc video]# ffmpeg -i movie1.mp4 -y -vcodec libx264 -vf scale=”640:-1″ -r 15 -acodec libfaac ../vod/movie1.flv
-y:文件覆盖,-vf scale=”640:-1”:尺寸调整为宽度640高度自动,-r 15:帧数15fps,这里用libfaac音频编码防止转成ts格式时iPhone没有声音

添加关键帧用来支持拖动播放
[root@linuxidc video]# cd ../vod
[root@linuxidc vod]# mv movie1.flv movie1-src.flv
[root@linuxidc vod]# yamdi -i movie1-src.flv -o movie1.flv
[root@linuxidc vod]# rm -rf movie1-src.flv

接下来测试的话,下载VLC Player到本地测试(VLC有时候会花屏,用JWPlayer就好了),或者不行麻烦的话装一个apache服务器并下载JWPlayer来测试。
测试播放地址为(地址已失效,请勿访问):
HTTP形式访问: http://www.linuxidc.com/movie1.flv
RTMP形式访问: rtmp://www.linuxidc.com/vod/movie1.flv

4.3)用mencoder转换rmvb文件

4.4)把flv转换成hls的m3u8
下载安装segmenter
[root@linuxidc ~]# yum install -y curl curl-devel zlib-devel openssl-devel perl perl-devel cpio expat-devel gettext-devel git
[root@linuxidc ~]# git clone https://github.com/johnf/m3u8-segmenter.git
[root@linuxidc ~]# cd m3u8-segmenter/
[root@linuxidc m3u8-segmenter]# gcc -Wall -g m3u8-segmenter.c -o segmenter -lavformat
[root@linuxidc m3u8-segmenter]# cp segmenter /usr/bin/

转换成ts,片源大小及清晰度等取决于flv文件,所以转成flv的时候一定要统一起来
[root@linuxidc vod]# cd /mnt/media/vod
[root@linuxidc vod]# mkdir /mnt/media/app/movie1/
[root@linuxidc vod]# ffmpeg -y -i movie1.flv -f mpegts -c:v copy -c:a copy -vbsf h264_mp4toannexb /mnt/media/app/movie1/main.ts

切片
[root@linuxidc movie1]# cd /mnt/media/app/movie1/
[root@linuxidc movie1]# segmenter -i main.ts -d 10 -p movie1 -m movie1.m3u8 -u http://www.linuxidc.com/hls/movie1/
-d 10:每个切片为10秒,-p movie1:切片的名字的前缀
-u URL地址:m3u8中播放列表的地址前缀,自己cat一下生成的movie1.m3u8就知道什么用 了

测试一下,用iPhone的Safari访问一下http://www.linuxidc.com/hls/movie1/movie1.m3u8

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

更多CentOS相关信息见CentOS 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=14

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-08/121324.htm