您的位置:首页 > 运维架构 > Nginx

使用nginx+ffmpeg搭建hls服务器

2014-03-21 12:27 489 查看
使用nginx+ffmpeg搭建hls服务器

1,实验环境的准备

1)下载vmware,当然破解版本

2)安装ubuntu 64bit版本,老的电脑可能需要bios里面设置,设置方法百度去。

3)执行sudo apt-get update & sudo apt-get install yum

4)执行sudo apt-get install g++;编译安装pcre的时候需要

5)sudo apt-get install git-core

2,编译ffmpeg

1) 安装依赖库

sudo apt-get update

sudo apt-get -y install autoconf automake build-essential libass-dev libgpac-dev \

libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libx11-dev \

libxext-dev libxfixes-dev pkg-config texi2html zlib1g-dev

mkdir ~/ffmpeg_sources

2) 在home个人目录下,新建三个目录

ffmpeg_sources:所有需要的源代码全部放入该目录

ffmpeg_build:编译过程中间文件

Bin 存放编译之后的文件

3) 安装freetype

在ffmpeg_sources下载freetype-2.4.9.tar.gz

tar freetype-2.4.9.tar.gz

wget http://savannah.nongnu.org/download/freetype/freetype-2.4.9.tar.gz
cd freetype-2.4.9

./configure

make

make install

4) 安装fontconfig

在ffmpeg_sources下载fontconfig-2.11.1.tar.gz

wget http://www.fontconfig.org/release/fontconfig-2.11.1.tar.gz
tar xvffontconfig-2.11.1.tar.gz

./configure --enable-libxml2 --enable-static

make

make install

5) 安装fribidi

在ffmpeg_sources下载fribidi-0.19.6.tar.bz2

wget wget http://fribidi.org/download/fribidi-0.19.6.tar.bz2
tar xvf fribidi-0.19.6.tar.bz2

./configure

make

make install

6) 安装libtheora-1.1.1.tar.gz

在ffmpeg_sources下载libtheora-1.1.1.tar.gz
http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.xz
tar libtheora-1.1.1.tar.gz

./configure

make

make install

7) 安装Libass

cd ~/ffmpeg_sources

tar xzvf libass-0.10.2.tar.gz

cd libass-0.10.2.tar.gz

./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"

make

make install

make distclean

export "PATH=$PATH:$HOME/bin"

8) 安装yasm

cd ~/ffmpeg_sources

wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar xzvf yasm-1.2.0.tar.gz

cd yasm-1.2.0

./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"

make

make install

make distclean

export "PATH=$PATH:$HOME/bin"

9) 安装libx264

cd ~/ffmpeg_sources

wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar xjvf last_x264.tar.bz2

cd x264-snapshot*

./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static

make

make install

make distclean

10) 安装libfdk-aac

cd ~/ffmpeg_sources

wget -O fdk-aac.zip https://github.com/mstorsjo/fdk-aac/zipball/master
unzip fdk-aac.zip

cd mstorsjo-fdk-aac*

autoreconf -fiv

./configure --prefix="$HOME/ffmpeg_build" --disable-shared

make

make install

make distclean

11) 安装libmp3lame

sudo apt-get install nasm

cd ~/ffmpeg_sources

wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar xzvf lame-3.99.5.tar.gz

cd lame-3.99.5

./configure --prefix="$HOME/ffmpeg_build" --enable-nasm --disable-shared

make

make install

make distclean

12) 安装libopus

cd ~/ffmpeg_sources

wget http://downloads.xiph.org/releases/opus/opus-1.1.tar.gz
tar xzvf opus-1.1.tar.gz

cd opus-1.1

./configure --prefix="$HOME/ffmpeg_build" --disable-shared

make

make install

make distclean

13) 安装libvpx

cd ~/ffmpeg_sources

wget http://webm.googlecode.com/files/libvpx-v1.3.0.tar.bz2
tar xjvf libvpx-v1.3.0.tar.bz2

cd libvpx-v1.3.0

./configure --prefix="$HOME/ffmpeg_build" --disable-examples

make

make install

make clean

最后编译ffmpeg

cd ~/ffmpeg_sources

wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
tar xjvf ffmpeg-snapshot.tar.bz2

cd ffmpeg

PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"

export PKG_CONFIG_PATH

./configure --prefix="$HOME/ffmpeg_build" --extra-cflags="-I$HOME/ffmpeg_build/include" \

--extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --extra-libs="-ldl" --enable-gpl \

--enable-libass --enable-libfdk-aac --enable-libmp3lame --enable-libopus --enable-libtheora \

--enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-x11grab

make

make install

make distclean

hash -r

3,,编译m3u8_segment

git clone https://github.com/johnf/m3u8-segmenter.git
使用正常的./configure等编译老是出错误

gcc -Wall -g m3u8-segmenter.c -o segmenter -lavformat -lavcodec -lavutilm3u8-segmenter.c: In function ‘add_output_stream’:

m3u8-segmenter.c:58: warning: implicit declaration of function ‘avformat_new_stream’

m3u8-segmenter.c:58: warning: assignment makes pointer from integer without a cast

m3u8-segmenter.c: In function ‘main’:

m3u8-segmenter.c:343: warning: implicit declaration of function ‘avformat_find_stream_info’

m3u8-segmenter.c:380: error: ‘AVFMT_FLAG_IGNDTS’ undeclared (first use in this function)

m3u8-segmenter.c:380: error: (Each undeclared identifier is reported only once

m3u8-segmenter.c:380: error: for each function it appears in.)

m3u8-segmenter.c:390: warning: implicit declaration of function ‘avcodec_open2’

m3u8-segmenter.c:397: error: ‘AVIO_FLAG_WRITE’ undeclared (first use in this function)

在网上查资料,特别是参考xjd_1985的文章,m3u8-segmenter工程编译和使用

./configure FFMPEG_CFLAGS=-I../ffmpeg-2.1.1 FFMPEG_LIBS='-L ../ffmpeg-2.1.1/libavformat -L../ffmpeg-2.1.1/libavcodec -L../ffmpeg-2.1.1/libavutil -pthread -lavformat -lavcodec -lavutil -lm -lz'

ffmpeg-2.1.1是我使用ffmpeg的版本和目录

4, 编译nginx

/configure --add-module=../nginx_mod_h264_streaming-2.2.7 --with-pcre=../pcre-8.32 --with-zlib=../zlib-1.2.3 --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-http_mp4_module --with-cc-opt='-O3' --with-openssl=../openssl-1.0.1f

Make

继续编译该软体使用make命令,哈哈报错啦!(有error信息打印不见的是一件坏事哦)报错信息如下:

make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1

make: *** [build] Error 2

解决方法:

进入支持MP4格式播放的库/nginx/nginx_mod_h264_streaming-2.2.7/src修改ngx_http_h264_streaming_module.c该文件。修改内容如下所示:

将如下几行注释

/* TODO: Win32 */

if (r->zero_in_uri)

{

return NGX_DECLINED;

}后我们再次make clean && make一下nginx,呵呵这次终于成功了。

Sudo make install

注意,所有其他软件包都必须使用zip或者tar命令解开,而且注意configure的时候路径一致。

5, 部署和配置nginx

sudo gedit /usr/local/nginx/conf/nginx.conf

修改为

#user nobody;

#user videoapp video; ##管理用户

worker_processes 8;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;

pid /usr/local/nginx/logs/nginx.pid;

events {

use epoll;

worker_connections 1024;

}

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 801;

server_name localhost;

#charset koi8-r;

limit_rate_after 5m;

limit_rate 512k;

charset utf-8;

#access_log logs/host.access.log main;

location / {

root html;

index index.html index.htm;

}

location ~ \.flv$ {

flv;

}

location ~ \.mp4$ {

mp4;

}

location ~(favicon.ico) {

log_not_found off;

expires 30d;

break;

}

#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;

# server_name localhost;

# ssl on;

# ssl_certificate cert.pem;

# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;

# ssl_ciphers HIGH:!aNULL:!MD5;

# ssl_prefer_server_ciphers on;

# location / {

# root html;

# index index.html index.htm;

# }

#}

}

启动nginx服务器:Sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

6, 转化视频文件

1)建立convert脚本,放在/usr/local/nginx/html下面

#!/bin/sh

file=$1

file_name=`echo ${file} | awk -F '.' '{ print $1 "" }'`

ffmpeg -i $1 -f mpegts -acodec libmp3lame -ar 48000 -ab 128k -vcodec libx264 -b 96k -flags +loop -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate 96k -bufsize 96k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 320:240 -g 30 -async 2 ${file_name}.ts

./segmenter -i ${file_name}.ts -n 20 -p hls -m ${file_name}.m3u8 -u http://192.168.27.128:801/
2)Copyconvert,并和segmenter和视频文件放在/usr/local/nginx/html目录下文件

3)使用方法:

./convert 视频文件名

注:会产生较多的.ts文件和一个.m3u8文件。

7,测试

主机端打开vlc的网络串流,则对话框输入
http://192.168.27.128:801/视频文件名.m3u8
点击播放,则可以正常的播放视频。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: