Transferred from: http://blog.csdn.net/cjsafty/article/details/9108587
Watch:
1. Detailed solution to the Nginx rtmp configuration process.
Before writing an nginx-based HLS on-demand system, essentially a media file into a m3u8 index, the corresponding files are done in advance on the server.
Nginx acts as an HTTP server and is based on Nginx because it can set the speed limit.
This paper mainly describes a live system, the core is m3u8 and inside for the TS link is real-time, can be refreshed. Similar to the live broadcast inside the CNTV.
This is divided into several parts in order: Software compilation, the provision of rtmp source, nginx configuration, HTML code modification, client playback.
1, software compilation:
Required Modules: Nginx-rtmp-module
GitHub
Https://github.com/arut/nginx-rtmp-module#example-nginxconf
This module to Nginx version seems to have no requirements, I use 1.2.2 is possible. Compilation method It's very clear on GitHub.
[Plain]View PlainCopy
- ./configure--add-module=<path-to-nginx-rtmp-module>
- Make
- Make install
1.3.14-1.5.0 version
[Plain]View PlainCopy
- ./configure--add-module=<path-to-nginx-rtmp-module>--with-http_ssl_module
Supply of 2,RTMP sources
One is the use of an existing media file, a type of camera and microphone acquisition.
For example:
[Plain]View PlainCopy
- Ffmpeg.exe-re-i sample.flv-vcodec copy-acodec copy-f flv rtmp://server-ip-address/hls/mystream
- Ffmpeg.exe-f dshow-i video= "USB2.0 Camera"-vcodec libx264-pix_fmt yuv420p-f flv rtmp://server-ip-address/hls/mystream
The first one is based on a media file that must be used with RE, which identifies native frame rate, which means to follow the framerate of the playback.
The second one is based on DShow, on Windows, encoded with x264, image with 420p,
Both methods are sent to the server with the RTMP protocol, where HLS and mystream each have meanings. HLS indicates that Application,mystream represents an instance. Explained later.
3,nginx Configuration
This nginx-rtmp-module contains a nginx.conf, located in the test directory, if you already have an Nginx configuration file, then only need to use
[Plain]View PlainCopy
- Include <path-to-nginx-rtmp-module>/test/nginx.conf;
To include this new configuration, the include must be at the same level as the existing configuration, that is, HTTP.
There are usually some problems here, such as rtmp not being recognized.
[Plain]View PlainCopy
- Unknown directive "? rtmp
- Unknown directive "rtmp" in/etc/nginx/conf.d/rtmp.conf:1
The solution is generally two, one is the new Conf code must be the same as the original, is generally ASCII, with the file command to know.
One is the re-compiled Nginx model is not loaded, you can try to stop nginx and start on the line.
[Plain]View PlainCopy
- rtmp {
- server {
- Listen 1935;
- Application MyApp {
- Live on;
- #record keyframes;
- #record_path/tmp;
- #record_max_size 128K;
- #record_interval 30s;
- #record_suffix. this.is.flv;
- #on_publish Http://localhost:8080/publish;
- #on_play Http://localhost:8080/play;
- #on_record_done Http://localhost:8080/record_done;
- }
- Application HLS {
- Live on;
- HLS on;
- Hls_path/tmp/app;
- Hls_fragment 5s;
- }
- }
- }
- HTTP {
- server {
- Listen 8080;
- Location/stat {
- Rtmp_stat all;
- Rtmp_stat_stylesheet stat.xsl;
- }
- location/stat.xsl {
- Root <path-to-nginx-rtmp-module>;
- }
- Location/control {
- Rtmp_control all;
- }
- #location/publish {
- # return 201;
- #}
- #location/play {
- # return 202;
- #}
- #location/record_done {
- # return 203;
- #}
- Location/rtmp-publisher {
- Root <path-to-nginx-rtmp-module>/test;
- }
- Location/hls {
- #server HLS Fragments
- types{
- Application/vnd.apple.mpegurl m3u8;
- VIDEO/MP2T ts;
- }
- Alias/tmp/app;
- expires-1;
- }
- Location/{
- Root <path-to-nginx-rtmp-module>/test/rtmp-publisher;
- }
- }
- }
Simple explanation: Application app is rtmp live, Flash is used. I have no use here. There is a player.html in the test directory for this service.
HLS is live in HLS. It's for me here.
/tmp/app is a directory that is used to store files within the m3u8 of a real-time refresh. The file refresh time is approximately 1 minutes. Old files are constantly being replaced with new files.
4,html Code Modification
Nginx-rtmp-module contains a test html,player.html, which is used to play flash. We are here to play HLS, can be easily modified as follows.
Named playhls.html
[Plain]View PlainCopy
- <! DOCTYPE html>
- <title>hls player</title>
- <body>
- <video height= "width=" 480 "controls>
- <source src= "http://server-ip-address:8080/hls/mystream.m3u8" type= "Application/vnd.apple.mpegurl"/>
- <p class= "Warning" >your browser does not support HTML5 video.</p>
- </video>
- </body>
5, client Playback
Browsers generally do not support m3u8 direct playback, because this is H5 only.
On the Android phone, we can use the latest version of the QQ browser to play the page.
On iOS devices, we can use Iphone,ipad to play, because this HLS is originally Apple's, so it's safari natural support
On the PC we can use Ffplayer to play.
Http://server-ip-address:8080/hls/mystream.m3u8
If it is available, the browser's address is
Http://server-ip-address:8080/hls/playhls.html
6, Status View
Http://server-ip-address:8080/stat
Reference page:
1,RTMP Configuration
http://yeyingxian.blog.163.com/blog/static/34471242012916050362/
2,ffmpeg gripping device
Http://ffmpeg.org/trac/ffmpeg/wiki/How%20to%20capture%20a%20webcam%20input
Http://ffmpeg.org/trac/ffmpeg/wiki/StreamingGuide
http://ffmpeg.gusari.org/viewtopic.php?f=11&t=841
Here, on the Windows7, the name of the sound device often has Chinese characters, such as "microphone (high Definition audio device)"
This Chinese can not be used in FFmpeg dshow. So I'm collecting pure video + audio (0 channels), which is a video with no sound.
Acquisition of audio
/dev/snd
Http://man.chinaunix.net/linux/how/Alsa-sound-5.html
Linux Audio Driver "
http://yiranwuqing.iteye.com/blog/1840176
Goto: Nginx-based HLS live system