Opencv + nginx RTMP module for image streaming

Keywords: Nginx sudo OpenCV Linux

This scheme realizes RTMP streaming after single frame image processing

Reference resources Nginx and nginx RTMP module building RTMP video live and on demand servers,<jkuri
,Compiling and installing ffmpeg under Linux (modification - simple and complex final version)

Setting up nginx server

download nginx1.8.1,nginx-rtmp-module

Install nginx dependency Library

sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev

Compile and install nginx with nginx RTMP module

cd nginx-1.8.1
./configure  --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module
sudo make install

Run at this time


You can run nginx server
Open the browser, fill in localhost in the address bar, and there is

Configure live service

Enter directory / usr/local/nginx/conf

Modify file nginx.conf by

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;

rtmp {                
    server {
        listen 1935;  
        chunk_size 4096;   
        application mylive{ 
        live on; #live on means live mode is enabled
        record off;

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;

        location /pop/video {
                alias /var/video;
        location /info {
               rtmp_stat all;
               rtmp_stat_stylesheet stat.xsl;
        location /stat.xsl {
                root path-to/nginx-rtmp-module/;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;



Note = = "location"/ stat.xsl ”==You need to fill in the absolute address of nginx RTMP module

Restart nginx server

sudo ./sbin/nginx -s reload

Open the browser and fill in the address bar localhost:8080/info , yes

Note "mylive" in the figure, indicating that the configuration has taken effect

Compile and install ffmpeg

Installation dependency

sudo apt-get install autoconf automake build-essential libass-dev libfreetype6-dev  libtheora-dev libtool libvorbis-dev pkg-config texinfo zlib1g-dev unzip cmake yasm libx264-dev libmp3lame-dev libopus-dev libsdl1.2-dev libva-dev libvdpau-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev libfaac* libopenjpeg * libv4l-dev libvpx-dev libssl-dev

download ffmpeg4.0.2 (it is also OK to test ffmpeg3)
Build install

./configure --prefix=/usr/local/ffmpeg/ --enable-shared --enable-gpl --enable-libx264

sudo make install

Push flow

reference resources <jkuri
RTMP of- stream.cpp

be careful
1) bitrate=300000 means the speed of video transmission is 300kb/s, and if the speed is low, the phenomenon of video mosaic will be very serious

2) On line 156

std::vector<uint8_t> imgbuf(height * width * 3 + 16);

It's a very mysterious line. It doesn't make any sense, but if the program is commented out, it will probably crash in the following sentence

sws_scale(swsctx, &, stride, 0, image.rows, frame->data, frame->linesize);

Of course, even if it is added, it may collapse, but the probability is lower
It is estimated that it is the BUG of the library. Try the latest ffmpeg 4.2.3. The problem has not been solved

Posted by naskar on Fri, 29 May 2020 09:06:38 -0700