title: How to publish selfmade stream with ffmpeg and c++ to rtmp server?
body: <p>Have a nice day to you, people!</p>
  
  <p>I am writing an application for Windows that will capture the screen and send the stream to Wowza server by rtmp (for broadcasting). My application use ffmpeg and Qt.
  I capture the screen with WinApi, convert a buffer to YUV444(because it's simplest) and encode frame as described at the file decoding_encoding.c (from FFmpeg examples):</p>
  
  <pre><code>///////////////////////////
  //Encoder initialization
  ///////////////////////////
  avcodec_register_all();
  codec=avcodec_find_encoder(AV_CODEC_ID_H264);
  c = avcodec_alloc_context3(codec);
  c-&gt;width=scr_width;
  c-&gt;height=scr_height;
  c-&gt;bit_rate = 400000;
  int base_num=1;
  int base_den=1;//for one frame per second
  c-&gt;time_base= (AVRational){base_num,base_den};
  c-&gt;gop_size = 10;
  c-&gt;max_b_frames=1;
  c-&gt;pix_fmt = AV_PIX_FMT_YUV444P;
  av_opt_set(c-&gt;priv_data, "preset", "slow", 0);
  
  frame = avcodec_alloc_frame();
  frame-&gt;format = c-&gt;pix_fmt;
  frame-&gt;width  = c-&gt;width;
  frame-&gt;height = c-&gt;height;
  
  for(int counter=0;counter&lt;10;counter++)
  {
  ///////////////////////////
  //Capturing Screen
  ///////////////////////////
      GetCapScr(shotbuf,scr_width,scr_height);//result: shotbuf is filled by screendata from HBITMAP
  ///////////////////////////
  //Convert buffer to YUV444 (standard formula)
  //It's handmade function because of problems with prepare buffer to swscale from HBITMAP
  ///////////////////////////
      RGBtoYUV(shotbuf,frame-&gt;linesize,frame-&gt;data,scr_width,scr_height);//result in frame-&gt;data
  ///////////////////////////
  //Encode Screenshot
  ///////////////////////////
      av_init_packet(&amp;pkt);
      pkt.data = NULL;    // packet data will be allocated by the encoder
      pkt.size = 0;
      frame-&gt;pts = counter;
      avcodec_encode_video2(c, &amp;pkt, frame, &amp;got_output);
      if (got_output) 
      {
          //I think that  sending packet by rtmp  must be here!
          av_free_packet(&amp;pkt);             
  
      }
  
  }
  // Get the delayed frames
  for (int got_output = 1,i=0; got_output; i++)
  {
      ret = avcodec_encode_video2(c, &amp;pkt, NULL, &amp;got_output);
      if (ret &lt; 0)
          {
              fprintf(stderr, "Error encoding frame\n");
              exit(1);
          }
          if (got_output)
          {
          //I think that  sending packet by rtmp  must be here!
          av_free_packet(&amp;pkt);      
          }
  }
  
  ///////////////////////////
  //Deinitialize encoder
  ///////////////////////////
  avcodec_close(c);
  av_free(c);
  av_freep(&amp;frame-&gt;data[0]);
  avcodec_free_frame(&amp;frame);
  </code></pre>
  
  <p>I need to send video stream generated by this code to RTMP server.
  In other words, I need c++/c analog for this command:</p>
  
  <pre><code>ffmpeg -re -i "sample.h264" -f flv rtmp://sample.url.com/screen/test_stream
  </code></pre>
  
  <p>It's useful, but I don't want to save stream to file, I want to use ffmpeg libraries for realtime encoding screen capture and sending encoded frames to RTMP server inside my own application.
  Please give me a little example how to initialize AVFormatContext properly and to send my encoded video AVPackets to server.</p>
  
  <p>Thanks.</p>