flv,mp4,nginx,拖动
http://blog.csdn.net/php10086/article/details/6728013
目前,由于Flash的流行,网络上绝大多数的微视频网站都采用了Flv格式来播放视频。
在互联网上播放视频,有两种方式,一种是文件方式,即通过HTTP协议访问视频文件,这种方式的缺点是不能从特定的帧开始播放;另外一种就是采用专门的流媒体服务器,这种方式的缺点是要搭建复杂的流媒体服务器。不过,现在有了一种集合了以上两种方式优点的一种解决方案,即通过HTTP来实现伪流媒体。本文描述的就是这样的一种解决方案。
首先配置nginx服务支持flv模块功能
⑴查看你的nginx是否已支持flv功能模块
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V //查看nginx服务支持
结果:
nginx version: nginx/0.8.24
built by gcc 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module
分析:我的nginx版本0.8.24,http_flv_module已经开启,支持flv服务
⑵安装支持flv模块的nginx服务器:
#tar zxvf nginx-0.8.24.tar.gz
#cd nginx-0.8.24
#./configure --prefix=/usr/local/nginx --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module //开启flv模块
#make && make install
#/usr/local/nginx/sbin/nginx //启Nginx
⑶nginx服务器配置(nginx.conf文件)
server
{
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.flv {
flv;
limit_rate_after 10m;
limit_conn one 1;
limit_rate 85k;
}
}
使用location 将 .flv的文件指向flv模块即可。
以上使用了limit_rate 是为了限速,当 flv视频下载超过10M,则限速到85K,只允许用户开1个进程,也就是先快速缓存可以播放,后面的慢慢下载。
⑷安装支持flv拖放进度条的播放器
上面三步都是安装支持nginx服务支持flv功能的,现在需要支持flv拖放进度条的播放器.
你需要有一个能够播放Flv视频的播放器,很显然,它还需要能够嵌入到网页中。目前比较流行的,功能上也还比较完善的一个Flv播放器就是 JW FLV Media Player(也称为 jwplayer)。它的网址是:
http://www.longtailvideo.com/players/jw-flv-player/
这个播放器支持视频加入广告和视频节目单功能,
下载包里面有很全的实例和使用方式,
将包中的player.swf(如果是带有Viral插件的,就是player-viral.swf)放到你的网站内,这就是用来提供视频播放的播放器。至于包中其它的文件,都可以不要。
接下来,将播放器嵌入到你的视频播放网页中:
直接嵌入,使用Object/Embed代码嵌入:
<embed
type="application/x-shockwave-flash"
id="player2"
name="player2"
src="player.swf"
width="328"
height="200"
allowscriptaccess="always"
allowfullscreen="true"
flashvars="file=http://192.168.40.135/test.flv&image=http://192.168.40.135/preview.jpg&autostart=false&type=http&streamer=start"
> </embed>
file:"video.flv", // FLV视频地址
type:"http", // 数据类型,本文是基于http模式的,这个必须写滴
image:"preview.jpg",// 开始播放之前的预览图
autostart:"false", // 是否自动播放
streamer:"start", // 参数为 “start”,这个参数用于传递给服务器从特定的关键帧开始播放,nginx编译了 flv 模块 所以是支持的。。
⑸添加关键帧
如果一个Flv视频要能够被拖到特定点播放,该Flv需要在其metadata中有关键帧的信息。如果你的Flv视频制作的时候没有这些信息,也是不能拖放播放的。可以使用yamdi来为你的视频加上关键帧信息 :
安装:
1 [root@localhost ~]#wget http://sourceforge.net/projects/yamdi/files/yamdi/1.4/yamdi-1.4.tar.gz/download
2 [root@localhost ~]#tar zxvf yamdi-1.4.tar.gz
3 [root@localhost ~]#cd yamdi-1.4/
4 [root@localhost yamdi-1.4]#make && make install
使用方法:
yamdi -i source.flv -o dest.flv //原视频 输出视频
http://www.kissthink.com/archive/8728.html
nginx搭建flv、mp4流媒体服务器;nginx搭建支持flv mp4 seek 实现拖拽 .Nginx搭建flv可拖拽的流媒体服务器
一、FLV视频发布方式简介
FLV视频有两总发布方式
1、 HTTP方式
这种方式要下载FLV视频文件到本地播放,一旦FLV视频文件下载完成,就不会消耗服务器的资源和带宽,但是拖动功能没有RTMP/RTMP流媒体方式强大,很多视频网站都是用HTTP方式实现的,如:YouTube,土豆,酷6等
2、 RTMP/RTMP流媒体方式
这种方式不用下载FLV视频文件到本地,可以实时的播放flv文件,可以任意拖拽播放进度条,但是比较消耗服务器的资源,
二、使用nginx来搭建flv流媒体服务器
1、使用nginx来搭建flv流媒体服务器简介
nginx中的Flv Stream模块能实现flv流媒体的功能,而且支持flv视频进度条拖拽,另外nignx还可以作为方向代理服务器代理后端基于Flash Media Server或者Red5的RTMP/RTMP流媒体服务器
2、 下面我们就来搭建一个完整的nginx流媒体服务器
1)、Nginx服务器的安装
#安装zlib
tar xzvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure
make && make install
#安装pcre
tar zxvf pcre-7.9.tar.gz
cd pcre-7.9
./configure --prefix=/usr/local/pcre
make && make install
#添加mp4支持模块
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
tar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
#安装nginx
groupadd www
useradd -g www www
tar xzvf nginx-0.8.34.tar.gz
cd nginx-0.8.34
./configure --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_ssl_module --with-pcre=/root/zhang/nginx/pcre-7.9 --with-zlib=/root/zhang/nginx/zlib-1.2.3--user=www --group=www --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module
make && make install
2)、安装yamdi
yadmi的作用是为flv文件添加关键帧,才能实现拖动播放
#下载yadmi
wgethttp://sourceforge.net/projects/yamdi/files/yamdi/1.4/yamdi-1.4.tar.gz/download
#安装yadmi
tar xzvf yamdi-1.4.tar.gz
cd yamdi-1.4
make && make install
使用方法:yamdi -i input.flv -o out.flv
给input.flv文件 添加关键帧,输出为out.flv文件
3)、配置nginx
vi /usr/local/nginx/conf/nginx.conf 添加以下内容(根据自身情况修改):
user www www;
worker_processes 30;
error_log /usr/local/nginx/logs/error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
keepalive_timeout 60;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
access_log off;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
######################################################################
server {
listen 80;
server_name 192.168.1.105;
root /usr/local/nginx/html/flv_file/;
limit_rate_after 5m; ####在flv视频文件下载了5M以后开始限速
limit_rate 512k; ####速度限制为512K
index index.html;
charset utf-8;
location ~ \.flv {
flv;
}
location ~ \.mp4$ {
mp4;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
4)、基本上已经设置完毕,但是此时我们测试的时候还需要一个支持拖拽播放的flash播放器,开源的JW Player就可以实现这样的功能,我将编译的播放器上传上来,供大家下载:
下载链接:http://blogimg.chinaunix.net/blog/upfile2/100607142612.rar
下载播放器后,上传到上面设置的/usr/local/nginx/html/flv_file/目录下,闭关把flv视频文件也放到该目录下!
5)、启动nginx后测试:
http://192.168.1.105/player.swf?type=http&file=test1.flv
说明: #我的ip是192.168.1.105
#player.swf是我的JW Player播放器
#http是表示居于http分发方式
#test1.flv是我的flv视频文件
wget http://sourceforge.net/projects/pcre/files/pcre/8.12/pcre-8.12.zip/download
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gzwget http://nginx.org/download/nginx-1.0.0.tar.gz
wget http://sourceforge.net/projects/yamdi/files/yamdi/1.8/yamdi-1.8.tar.gz/download
tar zxvf yamdi-1.8.tar.gz
cd yamdi-1.8
gcc yamdi.c -o yamdi -O2 -Wall
mv yamdi /usr/bin/
注意我们编译的 yamdi 它起着重要的作用,因为一个FLV视频要能够拖拽播放,这个FLV在其 metadata中有关键桢的信息,但大部分FLV 是没有的。所以,我们要甬道开源的yamdi来为视频添加关键帧信息
命令为
yamdi -i input.flv -o out.flv
yamdi的参数:
-i 指定FLV源文件.
-o 指定输出文件,如果文件名为-,则输出到标准输出设备上,如果不指定也是
-x 插入的metadata信息XML文件。如果输出文件省略了,则只生成metadata信息.
-c 一个写入creator标签的字符串.
-l 添加 onLastSecond 行为.
-h 显示帮助信息.
安装nginx
安装nginx之前必须先安装pcre
pcre编译安装需要gcc gcc-c++
yum install gcc gcc-c++ openssl-devel zlib-devel
unzip pcre-8.12.zip
cd pcre-8.12
./configure
make && make install
tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
tar zxvf nginx-1.0.0.tar.gz
cd nginx-1.0.0
./configure --user=www --group=www --with-http_sub_module --with-http_flv_module --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module
make时会有报错如下:
make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1
make[1]: Leaving directory `/home/mock/BUILD_ROOT/BUILD/nginx-0.8.38′
make: *** [build] Error 2
解决办法:Vim nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c 将如下几行注释
/* TODO: Win32 */
if (r->zero_in_uri)
{
return NGX_DECLINED;
}
make && make install
nginx配置文件
#nginx.conf
worker_processes 1;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~ \.mp4$ {
mp4;
}
location ~ \.flv$ {
flv;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
命令测试:
curl -v http://xx.xx.com/resbase/2010/06/baihezhengdan.flv?start=10240000 -o /dev/null
找个支持seek(拖拽)的播放器JWplayer
http://www.longtailvideo.com/support/jw-player-setup-wizard
---------------------------------------------------------------------------------------------------------
nginx安装的文档网上有很多了,不再重复,这里只说明支持FLV拖动播放时需要的配置。
1、Nginx编译要使nginx支持flv,在编译时应添加编译参数,启用flv模块
--with-http_flv_module
如果你已经安装了nginx,但又不知道是否启用了flv模块,可以使用如下命令查看:
[root@yufei ~]# /usr/local/nginx/sbin/nginx -V
nginx: nginx version: nginx/1.0.4
nginx: built by gcc 4.1.2 20080704 (Red Hat 4.1.2-50)
nginx: TLS SNI support disabled
nginx: configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_sub_module --with-http_realip_module
可以看到我的nginx版本为 1.0.4,其中已经开启了 http_flv_module
2、Nginx配置
要使nginx可以支持flv拖动还需要在站点配置中开启,如下:
server
{
listen 80;
server_name media.linux.fei.cn;
root /www/users/media.linux.fei.cn;
charset utf-8;
limit_rate_after 10m;
limit_rate 512k;
location ~ \.flv
{
flv;
}
}
使用location 将 .flv的文件指向flv模块即可。
以上使用了limit_rate 是为了限速,当 flv视频下载超过10M,则限速到512K,也就是先快速缓存可以播放,后面的慢慢下载。
3、常见问题
经过以上配置之后也许你的FLV视频还不能正常播放,还有几个问题需要注意:
3.1、FLV没有metadata关键帧数据
可以使用yamdi或flvtool2为你的flv视频添加关键帧
yamdi -i source.flv -o dest.flv
或
flvtool2 -U source.flv
3.2、添加了关键帧却依然不能拖动,播放器设置
此时要看你使用的flv播放器是否支持,你应该选择一个支持拖动的flv播放器,如:jw player,如果你没有也可以从这里下载 jwplayer.zip
在配置播放器时有两个参数比较重要,如果配置不正确,也可能导致不能拖动:
file:"video.flv", // FLV视频地址
type:"http", // 数据类型,一定要配置正确,否则不能正常拖动,我就是这个参数没设置,郁闷了半天,无法拖动
image:"preview.jpg",// 开始播放之前的预览图
autostart:"false", // 是否自动播放
streamer:"start", // 参数为 “start”,这个参数用于传递给服务器从特定的关键帧开始播放,nginx编译了 flv 模块 所以是支持的。。
至此,我在配置FLV视频拖放遇到的问题,都解决了。可以爽爽的拖动了。希望你也一样!
内容来自网络
http://hi.baidu.com/y_x_b_s/item/154f0d9a70484632326eeb2f
http://hdu104.com/294
CentOS6下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具)
1.先添加几个RPM下载源
1.1)安装RPMforge的CentOS6源
[root@AY130611215205Z ~]# wget -c http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
[root@AY130611215205Z ~]# rpm –import http://apt.sw.be/RPM-GPG-KEY.dag.txt
[root@AY130611215205Z ~]# rpm -i rpmforge-release-0.5.3-1.el6.rf.*.rpm
1.2)安装epel源
[root@AY130611215205Z ~]# wget -c 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 ~]# wget http://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 ~
2.2)卸载系统原有的ffmgeg和x.264,没有则跳过
[root@AY130611215205Z ~]# yum remove ffmpeg x264
2.3)先安装一些Mplayer编码库(仅限于64位系统)
[root@AY130611215205Z ~]# wget -c http://www2.mplayerhq.hu/MPlayer/releases/codecs/essential-amd64-20071007.tar.bz2
上面这一步可能在部分VPS上无法下载,比如我的阿里云VPS就提示连接失败,但是可以用本机浏览器下载后上传到VPS的/root根目录去,然后继续下面的操作
[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 ~]# wget ftp://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
[root@AY130611215205Z ~]# wget 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
[root@AY130611215205Z ~]# wget 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.安装配置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流播放
[root@AY130611215205Z ~]# wget 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.zip https://github.com/arut/nginx-rtmp-module/archive/master.zip
[root@AY130611215205Z ~]# unzip nginx-rtmp-module.zip
下载清缓存的模块
[root@AY130611215205Z ~]# wget -O ngx_cache_purge.ziphttps://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip
[root@AY130611215205Z ~]# unzip ngx_cache_purge.zip
3.3)下载安装nginx
[root@AY130611215205Z ~]# wget http://nginx.org/download/nginx-1.2.9.tar.gz
[root@AY130611215205Z ~]# tar zxvf nginx-1.2.9.tar.gz
[root@AY130611215205Z ~]# cd nginx-1.2.9
[root@AY130611215205Z 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@AY130611215205Z 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@AY130611215205Z ~]# /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@AY130611215205Z ~]# cd /mnt/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
接下来测试的话,下载VLC Player到本地测试(VLC有时候会花屏,用JWPlayer就好了),或者不行麻烦的话装一个apache服务器并下载JWPlayer来测试。
测试播放地址为:
HTTP形式访问: http://121.199.47.208/movie1.flv
RTMP形式访问: rtmp://121.199.47.208/vod/movie1.flv
4.3)用mencoder转换rmvb文件
4.4)把flv转换成hls的m3u8
下载安装segmenter
[root@AY130611215205Z ~]# yum install -y curl curl-devel zlib-devel openssl-devel perl perl-devel cpio expat-devel gettext-devel git
[root@AY130611215205Z ~]# git clone https://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 -u http://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
注:安装日志记录于半年前的evernote,现在才贴出来的,所以文中的视频访问地址都已经失效了。
nginx 是个很优秀的http服务器,ucweb就在用这个。
nginx可以支持flv mp4的流媒体播放。
考虑编译安装下nginx 。
参考 http://blog.csdn.net/sunvince/article/details/6426967
===================
1. 安装过程为
(1)编译安装pcre。 make make install 即可。 记得加入 --with-pcre-jit 否则会报错。
(2)编译安装yamdi-1.8 (Yet Another Metadata Injector for FLV),make make install 即可。
(3)下载源码
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
(4) 配置nginx,
其中,264_mod 、 zlib 、 niginx_acesskey 、openssl 、pcre 源码 使用绝对路径。
配置选项为:
./configure --add-module=/home/zhangbin/mserver/nginx/nginx_mod_h264_streaming-2.2.7 --with-pcre=/home/zhangbin/mserver/nginx/pcre-8.31 --with-pcre-jit --with-zlib=/home/zhangbin/mserver/nginx/zlib/1.2.3 --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-http_mp4_module --add-module=/home/zhangbin/mserver/nginx/nginx-accesskey-2.0.3 --with-http_ssl_module --with-openssl=/home/zhangbin/mserver/nginx/openssl-1.0.1c --with-cc-opt='-O3
会给
CFLAGS中加入-O3 :
CC = gcc
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -g -O3 -D_LARGEFILE_SOURCE -DBUILDING_NGINX
CPP = gcc -E
LINK = $(CC)
(5) 编译
1 )编译遇到了Werror的问题,参考nginx 1.3.6 编译报错 [-Werror=unused-but-set-variable]的解决 http://blog.csdn.net/commshare/article/details/8051060解决。
2 ) 需要指明源码路径:
--with-http_ssl_module --with-openssl=/home/zhangbin/mserver/nginx/openssl-1.0.1c
否则报错:
准备软件:
nginx-1.6.0.tar.gz
yamdi-1.9.tar.gz
yamdi 是渐进式流支持模块,抓取视频资源关键帧实现播放时的随意拖动效果
下载地址:http://ncu.dl.sourceforge.net/project/yamdi/yamdi/1.9/yamdi-1.9.tar.gz
flv视频可以通过http或rtmp方式发布。这里用http方式。
安装nginx及mp4支持模块和flv模块:
nginx支持mp4模块下载地址:http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
下载后解压。
安装编译环境:
1
2
3
4
|
[root@localhost nginx-1.6.0]# yum install -y zlib zlib-devel pcre pcre-devel [root@localhost nginx-1.6.0]# useradd -s /sbin/nologin www [root@localhost nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_ssl_module --user=www --group=www --with-http_flv_module --with-http_stub_status_module [root@localhost nginx-1.6.0]# make && make install |
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.6.0′
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -D_LARGEFILE_SOURCE -DBUILDING_NGINX -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules -I src/mail \
-o objs/addon/src/ngx_http_h264_streaming_module.o \
../nginx_mod_h264_streaming-2.2.7/src/ngx_http_h264_streaming_module.c
In file included from ../nginx_mod_h264_streaming-2.2.7/src/ngx_http_h264_streaming_module.c:2:
../nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c: In function ‘ngx_streaming_handler’:
../nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158: error: ‘ngx_http_request_t’ has no member named ‘zero_in_uri’
make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1
make[1]: Leaving directory `/root/nginx-1.6.0′
make: *** [build] Error 2
出现一个错误,解决方法:
编辑/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c
[root@localhost src]# vim ngx_http_streaming_module.c
注释掉:
/* TODO: Win32 */
if (r->zero_in_uri)
{
return NGX_DECLINED;
}
重新make通过。
nginx配置文件:
[root@localhost nginx-1.6.0]# grep -v “#” /usr/local/nginx/conf/nginx.conf | grep -v “^$”
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /usr/local/nginx/html;
location ~\.flv {
flv;
}
location ~\.mp4$ {
mp4;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
创建两个视频文件夹/usr/local/nginx/html/flv、/usr/local/nginx/html/mp4 存放视频文件。
安装yamdi实现拖动效果:
[root@localhost yamdi-1.9]# make && make install #两步即可
给视频添加帧:
[root@localhost flv]# yamdi -i 1.flv -o test.flv #给1.flv添加关键帧输出为test.flv
找一个flash播放器,下载地址:http://chinaapp-wordpress.stor.sinaapp.com/uploads/2013/08/player.swf 推荐wget方式下载。
测试:
浏览器访问: http://192.168.0.172/player.swf?type=http&file=flv/test.flv
192.168.0.172 #服务器ip地址
player.swf #flash播放器程序
http #用http方式发布
flv #为路径
test.flv #为视频文件
原创文章,转载请注明。本文链接地址: http://www.rootop.org/pages/3212.html
1、Nginx编译
--with-http_flv_module
[root@yufei ~]# /usr/local/nginx/sbin/nginx -V nginx: nginx version: nginx/1.0.4 nginx: built by gcc 4.1.2 20080704 (Red Hat 4.1.2-50) nginx: TLS SNI support disabled nginx: configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_sub_module --with-http_realip_module
2、Nginx配置
server { listen 80; server_name media.linux.fei.cn; root /www/users/media.linux.fei.cn; charset utf-8; limit_rate_after 10m; limit_rate 512k; location ~ \.flv { flv; } }
3、常见问题
3.1、FLV没有metadata关键帧数据
yamdi -i source.flv -o dest.flv
flvtool2 -U source.flv
3.2、添加了关键帧却依然不能拖动,播放器设置
file:"video.flv", // FLV视频地址 type:"http", // 数据类型,一定要配置正确,否则不能正常拖动,我就是这个参数没设置,郁闷了半天,无法拖动 image:"preview.jpg",// 开始播放之前的预览图 autostart:"false", // 是否自动播放 streamer:"start", // 参数为 “start”,这个参数用于传递给服务器从特定的关键帧开始播放,nginx编译了 flv 模块 所以是支持的。。
apt-get改编译nginx增加flv模块
keminar 发表于 2011-12-04 09:52:00. 发表在: Web技术
debian系统版本
cat /etc/debian_version
6.0
为保险起见下载和apt-get安装的一样的版本
nginx -v
nginx: nginx version: nginx/1.0.6
查看apt-get安装的编译参数
nginx -V
nginx: configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module
其实结果里还有两个add_module 参数由于所指的目录在机器上不存在,就去掉了
下载相同版本的源码包
wget http://www.nginx.org/download/nginx-1.0.6.tar.gz tar zxf nginx-1.0.6.tar.gz cd nginx-1.0.6
把刚才的configure参数放进来并增加新的http_flv_module
./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --with-http_flv_module
编译
make
卸载apt-get安装的nginx
apt-get remove nginx nginx-full nginx-common
安装nginx
make install
做软链接
ln -s /etc/nginx/sbin/nginx /usr/bin/
启动nginx
nginx
这样原来的配置文件都不用动,比较方便。
在configure时可能会报一些错误,简列如下
1. rewrite需要pcre支持
./configure: error: the HTTP rewrite module requires the PCRE library.
apt-get install libpcre3 libpcre3-dev
2. http cache需要openssl
./configure: error: the HTTP cache module requires md5 functions from OpenSSL library.
apt-get install libssl-dev openssl
3. 安装libxslt
./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries.
apt-get install libxslt-dev
4. 缺少GD library
./configure: error: the HTTP image filter module requires the GD library.
apt-get install libgd2-xpm libgd2-xpm-dev
5. 安装GeoIP library
./configure: error: the GeoIP module requires the GeoIP library.
apt-get install libgeoip-dev