title: Processing Camera stream in Opencv, pushing it over RTMP (NGINX RTMP Module) using FFMPEG
body: <p>Output video:
  <a href="https://youtu.be/VxfoBQjoY6E" rel="nofollow">https://youtu.be/VxfoBQjoY6E</a></p>
  
  <p><strong>Explanation:</strong></p>
  
  <p>I want to: Process camera stream in Opencv and push it over to RTMP server. I already have NGINX (RTMP module) set up and I have tested streaming videos with both RTMP (Flash Player) and HLS.</p>
  
  <p>I am reading the frames in a loop and using 'subprocess' in python to execute ffmpeg command. Here's the command I am using:</p>
  
  <pre><code>command = [ffmpeg,
      '-y',
      '-f', 'rawvideo',
      '-vcodec','rawvideo',
      '-pix_fmt', 'bgr24',
      '-s', dimension,
      '-i', '-',
      '-c:v', 'libx264',
      '-pix_fmt', 'yuv420p',
      '-preset', 'ultrafast',
      '-f', 'flv',
      'rtmp://10.10.10.80/live/mystream']
  
  
  import subprocess as sp
  ...
  proc = sp.Popen(command, stdin=sp.PIPE,shell=False)
  ...
  proc.stdin.write(frame.tostring()) #frame is read using opencv
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <p>I can see the stream fine but it freezes and resumes frequently. Here's the output of FFMPEG terminal log:</p>
  
  <pre><code>Stream mapping:
    Stream #0:0 -&gt; #0:0 (rawvideo (native) -&gt; h264 (libx264))
  frame=  117 fps= 16 q=22.0 size=     344kB time=00:00:04.04 bitrate= 697.8kbits/s speed=0.543x   
  </code></pre>
  
  <p>It mentions speed at the end. I believe it should be close to 1x. I am not sure how to achieve that.</p>
  
  <p>And I am on the same network as server, I can post my python code if required. Need some ffmpeg guru to give me some advise.  </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>My input fps is actually ~3.
  With <code>'-use_wallclock_as_timestamps', '1'</code> I can see in the log that speed is close to 1x.
  But HLS is not streaming live there's ~2 min delay, it halts and . Chris's advise partially worked. I am not sure where exactly is the problem, I am starting to believe it has something to do with nginx-rtmp module.</p>
  
  <p>Here's the final output, on left it's flash and on right it's hls. I am showing the ffmpeg options at the end.
  <a href="https://youtu.be/jsm6XNFOUE4" rel="nofollow">https://youtu.be/jsm6XNFOUE4</a></p>