Use Nginx+ffmpeg to build HLS live Transcoding server

Source: Internet
Author: User

Objective: To enable the Nginx support RTMP protocol to push the stream, and support HLS distribution function and FFmpeg transcoding multi-bit rate function.

First, the preparatory work
Module: Nginx-rtmp-module-master (rtmp protocol support)
:
http://nginx.org
Https://github.com/arut/nginx-rtmp-module

1. Install the dependent package:
#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 gcc-c++

2. Install the Git tool:
#mkdir Soft-source
#cd Soft-source
#wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
#tar XZVF git-latest.tar.gz
#cd git-2013-02-04
#autoconf
#./configure
#make && make Install
# git--version
git version 1.8.1.GIT
#cd:

"Error Handling"

If the git-latest.tar.gz size is 0, please download GIT-LATEST-TAR.XZ

Then xz-d git-latest.tar.xz decompression to. Tar

Then tar xvf Git-latest.tar


3. Install ffmpeg and its dependent packages:
++++++++yasm+++++++++++
#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:
++++++++x264+++++++++++
#git Clone git://git.videolan.org/x264
#cd x264
#./configure--enable-shared
#make
#make Install
#cd:

++++++++lame+++++++++++
#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:
++++++++libogg+++++++++++
#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
#tar XZVF libogg-1.3.0.tar.gz
#cd libogg-1.3.0
#./configure
#make
#make Install
#cd:
++++++++libvorbis+++++++++++
#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:


If the hint Ogg is not installed then download OGG.

libogg-1.3.1.tar.gz
url:http://www.xiph.org/downloads/

If not, then Run:export pkg_config_path= $PKG _config_path:/usr/local/lib/pkgconfig

Re./configure

++++++++libvpx+++++++++++
#git Clone Http://git.chromium.org/webm/libvpx.git
#cd LIBVPX
#./configure--enable-shared
#make
#make Install
#cd:
++++++++faad2+++++++++++
#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:
++++++++faac+++++++++++
#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:

"Error Handling"

Error encountered while compiling FAAC-1.28:

mpeg4ip.h:126:error: new declaration ' char* strcasestr (const char*, const char*) '

Workaround:

Start modifying this file from 123 lines to the end of line 129 mpeg4ip.h.
Before modification:
#ifdef __cplusplus
extern "C" {
#endif
char *strcasestr (const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif

After modification:
#ifdef __cplusplus
extern "C + +" {
#endif
Const Char *strcasestr (const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif


++++++++xvid+++++++++++
#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.. /.. /.. /


++++++++ffmpeg+++++++++++
#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:
"Error Handling"

If prompted LIBVPX decoder version must be >=0.91, please search for libvpx-v1.1.0.tar.bz download from Baidu.

Bzip2-d libvpx-v1.1.0.tar.bz2

Tar xvf libvpx-v1.1.0.tar.bz2

CD libvpx-v1.1.0

./configure--enable-shared--ENABLE-VP8

Make

Make install


Modify the/etc/ld.so.conf as follows:
Include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib
#ldconfig

Description

Dynamic loader finds shared libraries to rely on two files-/etc/ld.so.conf and/etc/ld.so.cache.

After the installation is complete, FFmpeg is located in the /opt/ffmpeg/bin directory.

Second, the installation of Nginx related modules

1. Environmental preparedness

Yum Install–y pcre Pcre-devel

Yum install–y zlib Zlib-devel

2. Download Nginx and rtmp module

wget http://nginx.org/download/nginx-1.6.2.tar.gz

Tar xzvf nginx_1.6.2.tar.gz

git clone git://github.com/arut/nginx-rtmp-module.git

3. Compiling nginx-rtmp

./configure--prefix=/usr/local/nginx--add-module=.. /nginx-rtmp-module--with-http_stub_status_module

Make

Make install

After the installation is complete, Nginx is located in the/usr/local/nginx/sbin directory, the configuration file nginx.conf in the/usr/local/nginx/conf directory

++++++++ test if Nginx is installed correctly +++++++++++

#cd/usr/local/nginx

#./sbin/nginx-c./conf/nginx.conf

Open the Web page http://localhost, if the display welcome means the installation is correct, if not shown, please check the Nginx log.

++++++++ Test rtmp+++++++++++

Modify the contents of the/usr/local/nginx/conf/nginx.conf as follows:

#debug
Daemon off;
Master_process off;


Error_log./error.log Debug;
events{
Worker_connections 1024;
}


rtmp{
server {
Listen 1935;
Chunk_size 4000;

#live
Application MyApp {
Live on;
}

}

Download an rtmp push flow tool from the Internet, I use the OBS (Open broadcaster Software), start pushing the stream rtmp://your_ip/myapp/test, using the player (http://www.cutv.com/demo/ LIVE_TEST.SWF) to see if it is OK.

++++++++ test HLS slicing function +++++++++++

Modify the contents of the/usr/local/nginx/conf/nginx.conf as follows:

#debug
Daemon off;
Master_process off;


Error_log./error.log Debug;
events{
Worker_connections 1024;
}


rtmp{
server {
Listen 1935;
Chunk_size 4000;

#live
Application MyApp {
Live on;

HLS on;
Hls_path/tmp/hls;
hls_fragment 2s;
Hls_playlist_length 6s;

}
}
}

#HTTP
http{
server {
Listen 80;

#welcome
Location/{
root HTML;
Index index.html index.htm;
}

#hls
Location/hls {
Types {
APPLICATION/VND.APPLE.MPEGUSR m3u8;
VIDEO/MP2T ts;
}
root/tmp;
Add_header Cache-control No-cache;
}
}
}

Use the player on the VLC or ipad to view the http://yourip/hls/test.m3u8.


++++++++ testing ffmpeg transcoding function +++++++++++

Modify the contents of the/usr/local/nginx/conf/nginx.conf as follows:

#debug
Daemon off;
Master_process off;


Error_log./error.log Debug;
events{
Worker_connections 1024;
}


rtmp{
server {
Listen 1935;
Chunk_size 4000;

#live
Application MyApp {
Live on;

Exec/opt/ffmpeg/bin/ffmpeg-i rtmp://localhost/myapp/$name
-c:a copy-c:v libx264-b:v 512k-g 30-f flv rtmp://localhost/hls/$name _low;
}

Application HLS {

Live on;

HLS on;
Hls_path/tmp/hls;
Hls_nested on;
hls_fragment 2s;
Hls_playlist_length 6s;

Hls_variant _hi bandwidth=640000;

}
}
}

#HTTP
http{
server {
Listen 80;

        #welcome
        location/{
            root   html;
            index  index.html index.htm;
       }

#hls
Location/hls {
Types {
APPLICATION/VND.APPLE.MPEGUSR m3u8;
VIDEO/MP2T ts;
}
root/tmp;
Add_header Cache-control No-cache;
}
}
}

When using FFmpeg transcoding,

Exec/opt/ffmpeg/bin/ffmpeg-i rtmp://localhost/myapp/$name
-c:a copy-c:v libx264-b:v 512k-g 30-f flv rtmp://localhost/hls/$name _low;

Only transcoding the video, not processing the audio, while the stream from MyApp to HLS, Hls_variant will generate a multi-bitrate m3u8 file, while the tile file into the Test_low directory, access to multi-bitrate, direct access to Http://yourip/hls /test.m3u8, the corresponding bitstream is accessed based on the content implemented in this m3u8, in this case the actual stream URL is http://yourip/hls/test_low/index.m3u8



Use Nginx+ffmpeg to build HLS live Transcoding server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.