D
D
dotrem2018-09-10 14:54:54
Nginx
dotrem, 2018-09-10 14:54:54

How to relay a webcam through nginx?

I installed nginx and rtmp module on Debian 9.
Now I want to relay my webcam through this server. I use this config for this:

user www-data;
worker_processes 1;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;


rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        application myapp {
            live on;
            exec /usr/bin/ffmpeg -f mjpeg -i video="http://<cam-ip>:8080/mobile?channelid=<channel-id>&fps=30&login=user&password=pass&is_ajax=true&withcontenttype=true"
     -c:v libx264 -f flv "rtmp://<my-ip>:1935/myapp/mystream live=1" 2>>/tmp/ffmpeg-$name.log;
        }
    }
}

http {

    server {
        listen 8080;
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location stat.xsl {
           root /stat;
        }
    }
}

I start nginx, and I start the vlc player, I insert the link rtmp://my-ip:1935/myapp/mystream into it, but nothing plays.
In the examples, people focus more on twitch streams, and I can't figure out what I missed.
Can you tell me what is wrong in my config?
In the ffmpeg log I see
ffmpeg version 3.2.12-1~deb9u1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
  configuration: --prefix=/usr --extra-version='1~deb9u1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libebur128 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 34.101 / 55. 34.101
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.101 / 57. 56.101
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libavresample   3.  1.  0 /  3.  1.  0
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
Input #0, mpjpeg, from 'http://<cam-ip>:8080/mobile?channelid=<channel-id>&fps=30&login=user&password=pass&is_ajax=true&withcontenttype=true':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 352x288 [SAR 1:1 DAR 11:9], 25 tbr, 25 tbn, 25 tbc
[flv @ 0x55c9709ca700] Video codec mjpeg not compatible with flv
Could not write header for output file #0 (incorrect codec parameters ?): Function not implementedStream mapping:
  Stream #0:0 -> #0:0 (copy)
    Last message repeated 1 times
'/usr/bin/ffmpeg' '-i' 'http://<cam-ip>:8080/mobile?channelid=<channel-id>&fps=30&login=user&password=pass&is_ajax=true&withcontenttype=true' '-c' 'copy' '-f' 'flv' 'rtmp://my-ip:1935/myapp/mystream'

How to choose the right codec?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dotrem, 2018-09-10
@dotrem

Unfortunately I can not update the question, so I will publish the continuation in the answers.
UPDATE
Started using this line

exec_pull /usr/bin/ffmpeg -f mjpeg -i video="http://<cam-ip>:8080/mobile?channelid=<channel-id>&fps=30&login=user&password=pass&is_ajax=true&withcontenttype=true"
     -f flv -vcodec flv "rtmp://my-ip/myapp/mystream live=1" 2>>/tmp/ffmpeg-$name.log;

And the video started playing. True, it takes 5 minutes to wait for a segment of 4 seconds, but it is clear that the video plays very quickly and all objects move rapidly. Maybe there is a more optimal set for ffmpeg?
Now in the log I see:
Input #0, mpjpeg, from 'http://<cam-ip>:8080/mobile?channelid=<channel-id>&fps=30&login=user&password=pass&is_ajax=true&withcontenttype=true':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 352x288 [SAR 1:1 DAR 11:9], 25 tbr, 25 tbn, 25 tbc
[swscaler @ 0x5561ee63c4e0] deprecated pixel format used, make sure you did set range correctly
Output #0, flv, to 'rtmp://my-ip:1935/myapp/mystream':
  Metadata:
    encoder         : Lavf57.56.101
    Stream #0:0: Video: flv1 (flv) ([2][0][0][0] / 0x0002), yuv420p, 352x288 [SAR 1:1 DAR 11:9], q=2-31, 200 kb/s, 25 fps, 1k tbn, 25 tbc
    Metadata:
      encoder         : Lavc57.64.101 flv
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> flv1 (flv))
Press [q] to stop, [?] for help
frame=    3 fps=1.7 q=2.0 size=      53kB time=00:00:00.08 bitrate=5389.7kbits/s speed=0.0452x    
frame=    4 fps=1.1 q=2.0 size=      66kB time=00:00:00.12 bitrate=4489.3kbits/s speed=0.032x    
frame=    5 fps=0.9 q=3.4 size=      76kB time=00:00:00.16 bitrate=3871.0kbits/s speed=0.0278x    
frame=    6 fps=0.8 q=5.8 size=      81kB time=00:00:00.20 bitrate=3300.7kbits/s speed=0.0258x    
frame=    7 fps=0.7 q=8.5 size=      84kB time=00:00:00.24 bitrate=2838.4kbits/s speed=0.0246x

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question