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

前端之家收集整理的这篇文章主要介绍了CentOS6下基于tengine搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

服务器版本CentOS6.5


Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。

下载地址http://tengine.taobao.org/

1先添加几个RPM下载源

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

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

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

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


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


[root@AY130611215205Z ~]# yum remove ffmpeg x264

2.3)先安装一些Mplayer编码库(仅限于64位系统)

http://www2.mplayerhq.hu/MPlayer/releases/codecs/essential-amd64-20071007.tar.bz2


[root@AY130611215205Z ~]# tar xvjf essential-amd64-20071007.tar.bz2
[root@AY130611215205Z ~]# mkdir /usr/local/lib/codecs
[root@AY130611215205Z ~]# cp -Rvp essential-amd64-20071007/* /usr/local/lib/codecs/


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

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


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

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


2.7)安装FFmpeg
http://ffmpeg.org/releases/ffmpeg-2.0.1.tar.bz2
[root@AY130611215205Z ~]# tar xvjf ffmpeg-2.0.1.tar.bz2
[root@AY130611215205Z ~]# cd ffmpeg-2.0.1
[root@AY130611215205Z 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@AY130611215205Z ffmpeg-2.0.1]# make && make install
[root@AY130611215205Z ffmpeg-2.0.1]# cd ~


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

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

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

3.安装配置Tengine(Nginx)

3.1)先安装各种依赖(Nginx需要pcre支持,yamdi用来为flv创建关键帧才能随意拖动)
[root@AY130611215205Z 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流播放
http://h264.code-shop.com/download/Nginx_mod_h264_streaming-2.2.7.tar.gz
[root@AY130611215205Z ~]# 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@AY130611215205Z ~]# wget -O Nginx-rtmp-module.ziphttps://github.com/arut/Nginx-rtmp-module/archive/master.zip
[root@AY130611215205Z ~]# unzip Nginx-rtmp-module.zip
下载清缓存的模块
[root@AY130611215205Z ~]# wget -O ngx_cache_purge.zip https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip
[root@AY130611215205Z ~]# unzip ngx_cache_purge.zip


3.3)安装tengine(Nginx)

tar zxvf tengine-2.1.2.tar.gz

cd tengine-2.1.2


./configure --user=daemon --group=daemon --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

make && make install

/**―――以下是安装了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@AY130611215205Z ~]# /usr/local/tengine/sbin/Nginx
然后用浏览器你的服务器IP,看到welcome就对了
或者到Nginx的sbin目录下,运行一下Nginx -V,看看列表出来的modules对不对


3.4)各种配置Nginx
编辑/usr/local/tengine/conf/Nginx.conf文件.
贴一下配置文件
―――――――――――――――――――――――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;

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;
listen 80;
server_name localhost;
root /mnt/wwwroot;
index index.html;
location ~ \.flv$ {
root /mnt/media/vod;
flv;
limit_conn addr 20;
limit_rate 200k;
location ~ \.mp4$ {
mp4;
location /hls {
# Serve HLS fragments
alias /mnt/media/app;
access_log logs/Nginxflv_access.log access;
}
――――――――――――――――――Nginx配置文件――――――――――――――

4.把自己的电影转换成mp4和flv格式来测试Nginx搭的环境

4.1)准备部电影,硬盘上随便找,
把电影重命名为 movie1.mp4并上传到服务器/opt/media/video下面,这里目录用来存放我们的原始视频。还有一个目录是/opt/media/vod 用来存放转换后的视频。
我这里的具体目录结构为:
/opt/media/video -> 存放原始视频
/opt/media/app -> 存放转成m3u8的视频,供http访问(HLS)
/opt/media/vod -> 存放转换后的flv和mp4视频,供http或rtmp访问

4.2)用ffmpeg转换mp4文件(ffmpeg不支持rmvb)
[root@AY130611215205Z ~]# cd /opt/media/video/
[root@AY130611215205Z 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@AY130611215205Z video]# cd ../vod
[root@AY130611215205Z vod]# mv movie1.flv movie1-src.flv
[root@AY130611215205Z vod]# yamdi -i movie1-src.flv -o movie1.flv
[root@AY130611215205Z vod]# rm -rf movie1-src.flv

4.3)用mencoder转换rmvb文件
4.4)把flv转换成hls的m3u8
下载安装segmenter

yum install -y curl curl-devel zlib-devel openssl-devel perl perl-devel cpio expat-devel gettext-devel git


[root@AY130611215205Z ~]# git clonehttps://github.com/johnf/m3u8-segmenter.git
[root@AY130611215205Z ~]# cd m3u8-segmenter/
[root@AY130611215205Z m3u8-segmenter]# gcc -Wall -g m3u8-segmenter.c -o segmenter -lavformat
[root@AY130611215205Z m3u8-segmenter]# cp segmenter /usr/bin/


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


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

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

猜你在找的CentOS相关文章