Use nginx to build flv and mp4 streaming media servers based on rtmp or http

WBOY
Release: 2016-08-08 09:21:00
Original
1694 people have browsed it

http://itindex.net/detail/48702-nginx-rtmp-http

1. Streaming media playback method

1. HTTP method
This method downloads the FLV video file and plays it locally. Once the FLV video file is downloaded, it will not consume the server's resources and bandwidth, but the dragging function is not as powerful as the RTMP/RTMP streaming method. Many video websites are implemented using HTTP, such as: YouTube, Tudou, Ku6, etc.
2. RTMP/RTMP streaming method
This method does not need to download FLV video files to the local, and can play flv in real time You can drag and drop the file to play the progress bar, but it consumes more server resources.
2. Use nginx to build flv streaming media server
1. Install git
yum install git
2. Install dependency packages
yum -y install gcc glibc glibc-devel make nasm pkgconfig lib -devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64
3. Install ffmpeg and its dependency packages (my dependency packages are installed under /usr/local/src, but the installation The location can be determined according to personal habits)
The following installation packages can be installed with yum. Try to use yum to install them. If not, use wget to obtain them.
#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
#make
#make install
#cd ..
#git clone git://git.videolan.org/x264
#cd x264
#./configure --enable-shared
#make
#make install
#cd ..
#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 --enable -nasm
#make
#make install
#cd ..
#yum install libogg
Note: libogg is installed using the .tar.gz package according to various online tutorials. However, if you install libvorbis, libvpx, etc. after installation, you will find that the installation location of libogg cannot be found, causing the installation to fail. 
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz 
#tar xzvf libvorbis-1.3.3.tar.gz 
#cd libvorbis-1.3.3 
#./configure 
#make 
#make install 
#cd .. 
#git clone http://git.chromium.org/webm/libvpx.git 
#cd libvpx 
#./configure  --enable-shared 
#make 
#make install 
#cd .. 
#wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz 
#tar zxvf faad2-2.7.tar.gz 
#cd faad2-2.7 
#./configure 
#make 
#make install 
#cd .. 
#wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz 
#tar zxvf faac-1.28.tar.gz 
#cd faac-1.28 
#./configure 
#make 
#make install 
#cd .. 
#wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz 
#tar zxvf xvidcore-1.3.2.tar.gz 
#cd xvidcore/build/generic 
#./configure 
#make 
#make install 
#cd .. 
#git clone git://source.ffmpeg.org/ffmpeg 
#cd ffmpeg 
#./configure  --prefix=/opt/ffmpeg/ --enable-version3  --enable-libvpx --enable-libfaac --enable-libmp3lame  --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree  --enable-avfilter --enable-pthreads 
#make && make install 
#cd .. 
修改/etc/ld.so.conf如下: 
include ld.so.conf.d/*.conf 
/lib 
/lib64 
/usr/lib 
/usr/lib64 
/usr/local/lib 
/usr/local/lib64 
/opt/ffmpeg/lib 
三、安装nginx模块 
#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 
#git clone git://github.com/arut/nginx-rtmp-module.git 
#yum install pcre 
nginx没有找到地址,只能手动下载,然后上传了。
#tar zxvf nginx-1.2.6.tar.gz
#cd nginx-1.2.6
#./configure --prefix=/usr/local/nginx --add-module=.. /nginx_mod_h264_streaming-2.2.7 --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_mp4_module --add-module=../nginx-rtmp-module --add-module=../nginx-rtmp-module/hls --with-cc-opt=-I/opt/ffmpeg/include --with- ld-opt='-L/opt/ffmpeg/lib -Wl,-rpath=/opt/ffmpeg/lib'
#make && make install
Install yamdi
yadmi is used to add flv files Key frames can be used to realize drag and play
#Download yadmi
wget http://sourceforge.net/projects/yamdi/files/yamdi/1.4/yamdi-1.4.tar.gz/download
#Install yadmi
tar xzvf yamdi-1.4.tar.gz
cd yamdi-1.4
make && make install
Usage: yamdi -i input.flv -o out.flv
to input . Add key frames to the flv file, and the output is the out.flv file
IV. Modify the nginx main configuration file
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/; 
        limit_rate_after 5m;    ####在flv视频文件下载了5M以后开始限速 
         limit_rate 512k;        ####速度限制为512K 
        index   index.html; 
        charset utf-8; 
        location ~ .flv { 
            flv; 
        } 
        location ~ .mp4$ { 
                 mp4; 
        }​​​​}
}
}
5. Start nginx
Check nginx.conf file configuration before startup
/usr/local/nginx/sbin/nginx -t -c /usr/nginx/conf/nginx.conf
or
/usr/nginx/sbin/ nginx -t
If there is an error, correct it according to the number of error lines.
If an error is reported: [error]: invalid PID number "" in "/var/run/nginx.pid"
usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx .conf
nginx startup
/usr/local/nginx/sbin/nginx -s reload
test
http://192.168.1.105/player.swf?type=http&file=test1.flv
(where player.swf is the player and test1.flv is the video file)
The player should be placed together with the video file.
The video file location corresponds to the configuration location in nginx.conf.
Download attachments to this article: play.rar (89 KB)
nginx-0.8.55.tar.gz (632.6 KB)
The above introduces how to use nginx to build flv and mp4 streaming media servers based on rtmp or http, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template