Turn: The HLS live system based on Nginx

Source: Internet
Author: User

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
    1. ./configure--add-module=<path-to-nginx-rtmp-module>
    2. Make
    3. Make install

1.3.14-1.5.0 version

[Plain]View PlainCopy
    1. ./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
    1. Ffmpeg.exe-re-i sample.flv-vcodec copy-acodec copy-f flv rtmp://server-ip-address/hls/mystream
    2. 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
    1. 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
    1. Unknown directive "? rtmp
    2. 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
  1. rtmp {
  2. server {
  3. Listen 1935;
  4. Application MyApp {
  5. Live on;
  6. #record keyframes;
  7. #record_path/tmp;
  8. #record_max_size 128K;
  9. #record_interval 30s;
  10. #record_suffix. this.is.flv;
  11. #on_publish Http://localhost:8080/publish;
  12. #on_play Http://localhost:8080/play;
  13. #on_record_done Http://localhost:8080/record_done;
  14. }
  15. Application HLS {
  16. Live on;
  17. HLS on;
  18. Hls_path/tmp/app;
  19. Hls_fragment 5s;
  20. }
  21. }
  22. }
  23. HTTP {
  24. server {
  25. Listen 8080;
  26. Location/stat {
  27. Rtmp_stat all;
  28. Rtmp_stat_stylesheet stat.xsl;
  29. }
  30. location/stat.xsl {
  31. Root <path-to-nginx-rtmp-module>;
  32. }
  33. Location/control {
  34. Rtmp_control all;
  35. }
  36. #location/publish {
  37. # return 201;
  38. #}
  39. #location/play {
  40. # return 202;
  41. #}
  42. #location/record_done {
  43. # return 203;
  44. #}
  45. Location/rtmp-publisher {
  46. Root <path-to-nginx-rtmp-module>/test;
  47. }
  48. Location/hls {
  49. #server HLS Fragments
  50. types{
  51. Application/vnd.apple.mpegurl m3u8;
  52. VIDEO/MP2T ts;
  53. }
  54. Alias/tmp/app;
  55. expires-1;
  56. }
  57. Location/{
  58. Root <path-to-nginx-rtmp-module>/test/rtmp-publisher;
  59. }
  60. }
  61. }


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
    1. <! DOCTYPE html>
    2. <title>hls player</title>
    3. <body>
    4. <video height= "width=" 480 "controls>
    5. <source src= "http://server-ip-address:8080/hls/mystream.m3u8" type= "Application/vnd.apple.mpegurl"/>
    6. <p class= "Warning" >your browser does not support HTML5 video.</p>
    7. </video>
    8. </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

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.