SoFunction
Updated on 2025-04-05

Sample code for implementing video on demand by nginx-rtmp-module module

Preset conditions

  • Configure on-demand server192.168.246.102, configure the network card to enable it to access the Internet, install nginx and nginx-rtmp-module modules, open port 80, 1935.
  • Configure a remote server192.168.246.103, configure the network card so that it can access the Internet, install the nginx module, and open port 80.
  • Playback test tool: VLC media player

Nginx on-demand basic configuration

Configure on-demand server192.168.246.102

# Configure the worker user# If this item is not configured, the default is nobody, and the video cannot be played due to permission problems.user www;
...
rtmp {
    server {
        listen 1935;
    
        application vod {
            play /var/rec;
        }
        
    }
}

Add www users

# groupadd www
# useradd -g www www

Restart Nginx

# systemctl restart nginx

create/var/recTable of contents

# mkdir /var/rec

Place the file flv or mp4 file in/var/recIn the directory and modify permissions

# chown www:www /var/rec -R
# cd /var/rec
# ls 
abcd-1703474265-2023-12-25-11_17_45.flv  abcd-1703474338-2023-12-25-11_18_58.flv

Using VLC media player

Click the menu "Media" - "Open Network Streaming", enter the URL

rtmp://192.168.246.102/vod/abcd-1703474265-2023-12-25-11_17_45.flv

Note: If/var/recThere are lower directories, and you need to use two slashes to play the URL

For example, to play/var/rec/flvThe flv file under

Enter the URL asrtmp://192.168.246.102/vod//flv/

Remote files on demand

On-demand servers can also play remote server resources

Configure a remote server192.168.246.103

http {
  	... 
  	server {
        listen 8081;
        server_name localhost;
        
        location / {
            root /www;
        }
    }
}

Open port 8081

# firewall-cmd --zone=public --add-port=8081/tcp --permanent
# firewall-cmd --reload

Create a directory/www/rec, and place the video file in this directory

# mkdir /www/rec
# ... The steps to move files are omitted here# cd /www/rec
# ls
abcd-1703474051-2023-12-25-11_14_11.flv  abcd-1703474066-2023-12-25-11_14_26.flv

Configure on-demand server192.168.246.102

rtmp {
    server {
        listen 1935;
    
        application vod2 {
            play http://192.168.246.103:8081/rec;
        }
    }
}

Using VLC media player

Click the menu "Media" - "Open Network Streaming", enter the URL

rtmp://192.168.246.102/vod2/abcd-1703474066-2023-12-25-11_14_26.flv

Note: If/www/recThere are lower directories, and you need to use two slashes to play the URL

For example, to play/var/rec/flvThe flv file under

Enter the URL asrtmp://192.168.246.102/vod2//flv/

Specify multiple playback locations

Configure on-demand server192.168.246.102

rtmp {
    server {
        listen 1935;
    
        application vod3 {
      		# /var/abc directory does not exist            play /var/abc /var/rec http://192.168.246.103:8081/rec;
        }
    }
}

Play with VLC media playerrtmp://192.168.246.102/vod3/abcd-1703474338-2023-12-25-11_18_58.flv

  • Due to the directory/var/abcNot exist, try to play the directory/var/recthe file below;
  • Can successfully play the directory/var/recFiles underabcd-1703474338-2023-12-25-11_18_58.flv, so no longer look for the files to be played from the back position.

refer to

/arut/nginx-rtmp-module

This is the article about the implementation of video on demand for nginx-rtmp-module module. For more related nginx-rtmp-module video on demand, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!