ustoopia

nginx.conf example adaptive bitrate streaming

Oct 25th, 2020 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. worker_processes auto;
  2. events {
  3. worker_connections 768; # Max clients = worker_processes * worker_connections. In this example: 1 * 768 = 768 max clients.
  4. # multi_accept on; # "Off" will accept 1 new connection at a time. "On" will accept all new connections. Default is off.
  5. }
  6.  
  7. rtmp {
  8. server {
  9. listen 1935; # This is the listening port. Open it on your router and/ or allow it in your firewall. These variables are possible:
  10. # (addr[:port]|port|unix:path) [bind] [ipv6only=on|off] [so_keepalive=on|off|keepidle:keepintvl:keepcnt|proxy_protocol].
  11. chunk_size 4096; # Max chunk size for multiplexing. The bigger this value, the lower CPU overhead. Cannot be < 128. Default=4096.
  12. application live { # Name it whatever you prefer. You will need at least one application, but you can have many more applications.
  13. live on; # on|off. Enables this application and allowing live streaming to it. Default=on.
  14.  
  15. ## TRANSCODING USING FFMPEG EXEC ##
  16. ## The following lines will take our incoming RTMP stream and transcode it to several different HLS streams with variable bitrates ##
  17. ## Once receive stream, transcode for adaptive streaming. This single ffmpeg command takes the input and transforms the source into ##
  18. ## 4 or 5 different streams with different bitrate and quality. P.S. The scaling done here respects the aspect ratio of the input. ##
  19. ## EXEC - Many things are possible using exec. To learn more visit https://github.com/arut/nginx-rtmp-module/wiki/Directives#exec ##
  20.  
  21. exec ffmpeg -i rtmp://localhost/$app/$name -async 1 -vsync -1
  22. -c:v libx264 -c:a libvo_aacenc -b:v 256k -b:a 32k -vf "scale=480:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/hls/$name_low
  23. -c:v libx264 -c:a libvo_aacenc -b:v 768k -b:a 96k -vf "scale=720:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/hls/$name_mid
  24. -c:v libx264 -c:a libvo_aacenc -b:v 1024k -b:a 128k -vf "scale=960:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/hls/$name_high
  25. -c:v libx264 -c:a libvo_aacenc -b:v 1920k -b:a 128k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/hls/$name_ higher
  26. -c copy -f flv rtmp://localhost/hls/$name_src;
  27. }
  28.  
  29. application hls { # This application is for splitting the stream into HLS fragments
  30. live on; # on|off. Enables this application and allowing live streaming to it. Default=on.
  31. hls on; # on|off. Toggles HLS on or off for this application. Enables HTTP Live Streaming.
  32. hls_path /tmp/hls; # Location to store the video fragment files. Will be created if it doesn't exist.
  33.  
  34. ## HLS_VARIANT - Used for variable bitrate streaming. Please read: https://github.com/arut/nginx-rtmp-module/wiki/Directives#hls_variant ##
  35. ## When hls_variant suffix is matched on stream name then variant playlist is created for the current stream with all entries specified by hls_variant
  36. ## directives in current application. Stripped name without suffix is used as variant stream name. The original stream is processed as usual.
  37. ## Optional parameters following the suffix are appended to EXT-X-STREAM-INF in m3u8 playlist. See HLS spec 3.3.10. EXT-X-STREAM-INF for full list.
  38.  
  39. hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution
  40. hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution
  41. hls_variant _high BANDWIDTH=1152000; # Higher-than-SD resolution
  42. hls_variant _higher BANDWIDTH=2048000; # High bitrate, HD 720p resolution
  43. hls_variant _src BANDWIDTH=4096000; # Source bitrate, source resolution
  44. }
  45. }
  46. }
  47.  
  48. http {
  49. sendfile off; # on|off. Toggles the use of sendfile. Default=off. For optimal HLS delivery disable this.
  50. tcp_nodelay on; # on|off. Forces a socket to send the data in its buffer, whatever the packet size. Default=on.
  51. tcp_nopush on; # on|off. Sends the response header and beginning of a file in one packet. Default=off.
  52. server_tokens off; # on|off|build. Toggles showing nginx version in the response header field. Default=on.
  53. default_type application/octet-stream; # Emit this MIME type for all requests.
  54.  
  55. server {
  56. listen 80;
  57. gzip off;
  58.  
  59. server_name localhost;
  60. index index.html index.htm index.nginx-debian.html;
  61.  
  62. location / {
  63. root /var/www/html/;
  64. add_header Strict-Transport-Security "max-age=63072000;";
  65. }
  66.  
  67. location /hls {
  68. expires -1;
  69. autoindex on;
  70.  
  71. types {
  72. application/vnd.apple.mpegurl m3u8;
  73. text/html html;
  74. }
  75. # root /tmp/;
  76. alias /tmp/hls;
  77.  
  78. add_header Cache-Control no-cache; # Prevent caching of HLS fragments
  79. add_header Access-Control-Allow-Origin *; # Allow web player to access our playlist
  80. }
  81. }
  82. }
Add Comment
Please, Sign In to add comment