项目作者: SoloSynth1

项目描述 :
Documentation on how to set up a HLS stream server using ffmpeg & NGINX
高级语言:
项目地址: git://github.com/SoloSynth1/nginx-hls-server.git
创建时间: 2021-08-22T04:43:47Z
项目社区:https://github.com/SoloSynth1/nginx-hls-server

开源协议:

下载


nginx-hls-server

Table of Contents

Objective

To create a video stream and serve as a HLS endpoint via NGINX.

Prerequisites

The steps listed here should work for most *nix devices, as long as ffmpeg & nginx are supported & installed.

ffmpeg

  1. sudo apt-get install ffmpeg # Debain-based
  2. sudo pacman -S ffmpeg # Arch-based
  3. yum install ffmpeg # RHEL/Fedora
  4. brew install ffmpeg # macos

NGINX

As we need to use the RTMP Module in NGINX, which might not be present in certain build, we will clone the git repo and compile nginx:

  1. cd /path/to/build/dir
  2. git clone https://github.com/arut/nginx-rtmp-module.git
  3. git clone https://github.com/nginx/nginx.git
  4. cd nginx
  5. ./auto/configure --add-module=../nginx-rtmp-module
  6. make
  7. sudo make install

This will install NGINX in /usr/local/nginx.

Setup

1. Create ffmepg HLS stream

Here is an example to set up a HLS stream at /tmp/hls/ with following settings:

Input

  • Input device: /dev/video0
  • Resoltuion: 1280x720
  • Framerate: 30

HLS stream

  • Encoding preset: superfast
  • Target (average) bit rate: 5000k
  • Codec: h264
  • Segment time interval: 6 seconds
  • Playlist size: 10 segments
  • Wrap limit: 40 segements
  • HLS flag: delete_segments
  • Segement deletion threshold: 1
  1. ffmpeg -video_size 1280x720 \
  2. -framerate 30 \
  3. -i /dev/video0 \
  4. -f hls \
  5. -preset superfast \
  6. -c:v libx264 \
  7. -b:v 5000k \
  8. -hls_time 6 \
  9. -hls_list_size 10 \
  10. -hls_wrap 40 \
  11. -hls_delete_threshold 1 \
  12. -hls_flags delete_segments \
  13. /tmp/hls/stream.m3u8

You can run it with & or other tools (e.g. screen or nohup) to make it run in background.

2. Set up NGINX

Modify /usr/local/nginx/conf/nginx.conf so that it serves contents in /tmp/hls as HLS stream:

  1. worker_processes 1;
  2. user pi;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. default_type application/octet-stream;
  8. server {
  9. listen 80;
  10. location /live {
  11. alias /tmp/hls/;
  12. }
  13. }
  14. types {
  15. application/vnd.apple.mpegurl m3u8;
  16. video/mp2t ts;
  17. text/html html;
  18. }
  19. }

Test the config file by running:

  1. /usr/local/nginx/sbin/nginx -t

Start the nginx server:

  1. /usr/local/nginx/sbin/nginx

Note: You may need to run with root access. i.e. run it with sudo.

Test

Open VLC player and connect to http://<ip-address>/live/stream.m3u8.

Optional

Add RTMP functionality to the server so that it can accept RTMP streams and process into HLS stream.

  1. rtmp {
  2. server {
  3. listen 1935;
  4. application live {
  5. live on;
  6. interleave on;
  7. hls on;
  8. hls_path /tmp/hls/;
  9. hls_fragment 15s;
  10. }
  11. }
  12. }

Troubleshooting

Cannot find correct input device

If you have difficulties in finding the right device/config/parameters, you can view the details device info by using v4l2-ctl.

To use v4l2-ctl, you need to have v4l-utils installed. Please install it using package manager of your distro.

For example:

  1. sudo apt install v4l-utils

Then you can list all of the device details using the following command:

  1. v4l2-ctl --all

or

  1. v4l2-ctl --list-devices

ffmpeg cannot access video input device

  1. [video4linux2,v4l2 @ 0x5567bd2739c0] Cannot open video device /dev/video0: Operation not permitted
  2. /dev/video0: Operation not permitted

In some distros, there are restrictions on which devices certain softwares can access to. Thus in order to let ffmpeg to access to the camera e.g. /dev/video0, you will need to explicitly grant permission to ffmpeg

Here is an example on Ubuntu:

  1. snap connect ffmpeg:camera